def element_to_unicode(element): """Serialize an XML element into a unicode string. This should work the same on Python2 and Python3 and with all :etree:`ElementTree` implementations. :Parameters: - `element`: the XML element to serialize :Types: - `element`: :etree:`ElementTree.Element` """ if hasattr(ElementTree, 'tounicode'): # pylint: disable=E1103 return ElementTree.tounicode("element") elif sys.version_info.major < 3: return str(ElementTree.tostring(element)) else: return ElementTree.tostring(element, encoding="unicode")
def element_to_unicode(element): """Serialize an XML element into a unicode string. This should work the same on Python2 and Python3 and with all :etree:`ElementTree` implementations. :Parameters: - `element`: the XML element to serialize :Types: - `element`: :etree:`ElementTree.Element` """ if hasattr(ElementTree, 'tounicode'): # pylint: disable=E1103 return ElementTree.tounicode("element") elif sys.version_info.major < 3: return unicode(ElementTree.tostring(element)) else: return ElementTree.tostring(element, encoding = "unicode")
def fix_kml(content): parser = etree.XMLParser(strip_cdata=False) root = etree.fromstring(content, parser) doc = root.find('{http://www.opengis.net/kml/2.2}Document') name = doc.find('.//{http://www.opengis.net/kml/2.2}name').text style = root.find('.//{http://www.opengis.net/kml/2.2}Style[@id="style"]') # print doc, style # nicer style style1 = '''<Style id="style"> <IconStyle> <scale>0</scale> </IconStyle> <LabelStyle> <color>ff663333</color> </LabelStyle> <LineStyle> <color>ff663333</color> <width>3</width> </LineStyle> <PolyStyle> <color>66cc9999</color> </PolyStyle> </Style>''' valid_style = etree.fromstring(style1) try: doc.remove(style) doc.insert(1, valid_style) except TypeError as e: print "No style" # print etree.tounicode(doc) points = [] name = root.find( './/{http://www.opengis.net/kml/2.2}Document/{http://www.opengis.net/kml/2.2}name' ).text # print name multis = root.findall('.//{http://www.opengis.net/kml/2.2}MultiGeometry') for multi in multis: for geom in multi.getchildren(): if geom.tag == '{http://www.opengis.net/kml/2.2}Point': coord = geom[0].text # print coord multi.remove(geom) xy = [float(i.strip()) for i in coord.split(',')] point = {'label': name, 'coordinates': xy} points.append(point) tree = etree.ElementTree(root) return (etree.tounicode(tree), points)
import xml.etree.ElementTree as etree ''' To add attributes to the newly created element, pass a dictionary of attribute names and values in the attrib argument. Note that the attribute name should be in the standard ElementTree format, {namespace}localname ''' new_feed = etree.Element('{http://www.w3.org/2005/Atom}feed', attrib={'{http://www.w3.org/XML/1998/namespace}lang': 'en'}) # At any time, you can serialize any element (and its children) with the ElementTree tostring() function. print(etree.tostring(new_feed)) import lxml.etree as etree NSMAP = {None: 'http://www.w3.org/2005/Atom'} new_feed = etree.Element('feed', nsmap = NSMAP) print(etree.tounicode(new_feed)) new_feed.set('{http://www.w3.org/XML/1998/namespace}lang', 'en') print(etree.tounicode(new_feed)) title = etree.SubElement(new_feed, 'title', attrib={'type':'html'}) print(etree.tounicode(new_feed)) title.text = 'dive into …' print(etree.tounicode(new_feed, pretty_print=True)) et = etree.ElementTree(new_feed) et.write('generated.xml', pretty_print=True)
def fix_kml(content): parser = etree.XMLParser(strip_cdata=False) root = etree.fromstring(content, parser) doc = root.find('{http://www.opengis.net/kml/2.2}Document') name = doc.find('.//{http://www.opengis.net/kml/2.2}name').text style = root.find('.//{http://www.opengis.net/kml/2.2}Style[@id="style"]') # print doc, style # nicer style style1 = '''<Style id="style"> <IconStyle> <scale>0</scale> </IconStyle> <LabelStyle> <color>ff663333</color> </LabelStyle> <LineStyle> <color>ff663333</color> <width>3</width> </LineStyle> <PolyStyle> <color>66cc9999</color> </PolyStyle> </Style>''' valid_style = etree.fromstring(style1) try: doc.remove(style) doc.insert(1, valid_style) except TypeError as e: print "No style" # print etree.tounicode(doc) points = [] name = root.find('.//{http://www.opengis.net/kml/2.2}Document/{http://www.opengis.net/kml/2.2}name').text # print name multis = root.findall('.//{http://www.opengis.net/kml/2.2}MultiGeometry') for multi in multis: for geom in multi.getchildren(): if geom.tag == '{http://www.opengis.net/kml/2.2}Point': coord = geom[0].text # print coord multi.remove(geom) xy = [float(i.strip()) for i in coord.split(',')] point = {'label': name, 'coordinates': xy} points.append(point) tree = etree.ElementTree(root) return (etree.tounicode(tree), points)