本文整理汇总了Python中xml.etree.ElementTree.tounicode方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.tounicode方法的具体用法?Python ElementTree.tounicode怎么用?Python ElementTree.tounicode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.tounicode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: element_to_unicode
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import tounicode [as 别名]
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")
示例2: element
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import tounicode [as 别名]
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)
示例3: fix_kml
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import tounicode [as 别名]
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)