Have you looked at libkml and its python bindings?

   Josh

On Mar 28, 2010 6:32 AM, "Tyler Erickson" <[email protected]> wrote:

Thanks for the ideas, which led me to
http://effbot.org/zone/element-namespaces.htm . This page notes that the
standard serializer passes through element names that use the prefix:tag
notation.  So I think a good approach will be to write elements with the
prefix:tag notation, then iterate through the tree to find out what prefixes
I have used, and then set the appropriate namespaces on the root element of
the tree.

Once I get around to parsing and modifying KML documents, it looks like I
will have to implement a custom serializer.

- Tyler



#===================================
import xml.etree.cElementTree as ET
ns_map = {'kml': 'http://www.opengis.net/kml/2.2',
          'atom': 'http://www.w3.org/2005/Atom',
          'gx': 'http://www.google.com/kml/ext/2.2'}

# create a simple element
name_element = ET.Element('atom:name')


name_element.text = 'John Smith'
# wrap the elements in a KML element


kml_element = ET.Element("kml")
kml_element.append(name_element)

# get the set of namespace prefixes used in the tree
ns_set = set()
for elem in kml_element.getiterator():
    part = elem.tag.split(':')
    if len(part)>1:
        ns_set.add(part[0])

# set the namespaces for the root element


kml_element.set('xmlns','http://www.opengis.net/kml/2.2')
for ns in ns_set:
    kml_element.set('xmlns:' + ns, ns_map[ns])



print 'ET.tostring(name_element, encoding="UTF-8")='
print ET.tostring(name_element, encoding="UTF...

print 'ET.tostring(kml_element, encoding="UTF-8")='
print ET.tostring(kml_element, encoding="UTF-8")...

To unsubscribe from this group, send email to python-gis-sig+
unsubscribegooglegroups.com or reply to...

To unsubscribe from this group, send email to 
python-gis-sig+unsubscribegooglegroups.com or reply to this email with the 
words "REMOVE ME" as the subject.

Reply via email to