2016-11-09 46 views

回答

0

假設你有一個包含XML數據的字符串變量,你可以做到以下幾點:

with open("output.xml", "w") as f: 
    f.write(xmlstr) 
0

如果使用Elementtree

import xml.etree.cElementTree as ET 
root = ET.Element("root") 
doc = ET.SubElement(root, "doc") 

ET.SubElement(doc, "field1", name="blah").text = "some value1" 
ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2" 

tree = ET.ElementTree(root) 
tree.write("filename.xml")