python 构造xml
原文链接: python 构造xml
代码123456789101112131415161718import xml.etree.cElementTree as ETimport xml.etree.ElementTree as ElementTreefrom xml.dom.minidom import parseStringroot = ET.Element("root")doc = ET.SubElement(root, "doc")ET.SubElement(doc, "field1", name="k1").text = "1<2"ET.SubElement(doc, "field2", name="k2").text = "2>1"tree = ET.ElementTree(root)tree.write("filename.xml")xml = ElementTree.tostring(root)print(xml)prettyxml = parseString(xml).toprettyxml()print(prettyxml)
输出12345678b'<root><doc><field1 name="k1">1<2</field1><field2 name="k2">2>1</field2></doc></root>'<?xml version="1.0" ?><root> <doc> <field1 name="k1">1<2</field1> <field2 name="k2">2>1</field2> </doc></root>
|
|