Bob*_*Bob 6 python xml elementtree
我用这样的东西创建了一个xml树
top = Element('top')
child = SubElement(top, 'child')
child.text = 'some text'
Run Code Online (Sandbox Code Playgroud)
如何将其转储到XML文件中?我试过了top.write(filename),但方法不存在.
ale*_*cxe 10
您需要实例化一个ElementTree对象并调用write()方法:
import xml.etree.ElementTree as ET
top = ET.Element('top')
child = ET.SubElement(top, 'child')
child.text = 'some text'
tree = ET.ElementTree(top)
tree.write('output.xml')
Run Code Online (Sandbox Code Playgroud)
output.xml运行代码后的内容:
<top><child>some text</child></top>
Run Code Online (Sandbox Code Playgroud)