Python的XML写入工具

24 浏览
0 Comments

Python的XML写入工具

我目前正在尝试使用ElementTree,它看起来不错,可以转义HTML实体等等。我是否错过了一些我不知道的真正精彩的东西?

这与我实际正在做的类似:

import xml.etree.ElementTree as ET
root = ET.Element('html')
head = ET.SubElement(root,'head')
script = ET.SubElement(head,'script')
script.set('type','text/javascript')
script.text = "var a = 'I love á letters'"
body = ET.SubElement(root,'body')
h1 = ET.SubElement(body,'h1')
h1.text = "And I like the fact that 3 > 1"
tree = ET.ElementTree(root)
tree.write('foo.xhtml')
# more foo.xhtml

And I like the fact that 3 > 1

0