用于 Python 的 XML 编写工具

问题描述:

我目前正在尝试 ElementTree,它看起来不错,它可以转义 HTML 实体等等.我是否错过了一些我从未听说过的真正美妙的东西?

I'm currently trying ElementTree and it looks fine, it escapes HTML entities and so on and so forth. Am I missing something truly wonderful I haven't heard of?

这和我实际做的很相似:

This is similar to what I'm actually doing:

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
<html><head><script type="text/javascript">var a = 'I love &amp;aacute;
letters'</script></head><body><h1>And I like the fact that 3 &gt; 1</h1>
</body></html>

我假设您实际上正在创建一个 XML DOM 树,因为您想验证进入该文件的内容是否是有效的 XML,否则您将只需将静态字符串写入文件即可.如果验证您的输出确实是您的目标,那么我建议

I assume that you're actually creating an XML DOM tree, because you want to validate that what goes into this file is valid XML, since otherwise you'd just write a static string to a file. If validating your output is indeed your goal, then I'd suggest

from xml.dom.minidom import parseString

doc = parseString("""<html>
    <head>
        <script type="text/javascript">
            var a = 'I love &amp;aacute; letters'
        </script>
    </head>
    <body>
        <h1>And I like the fact that 3 &gt; 1</h1>
    </body>
    </html>""")

with open("foo.xhtml", "w") as f:
    f.write( doc.toxml() )

这让您只需编写要输出的 XML,验证它是否正确(因为 parseString 会在无效时引发异常)并使您的代码看起来更漂亮.

This lets you just write the XML you want to output, validate that it's correct (since parseString will raise an exception if it's invalid) and have your code look much nicer.

大概您不只是每次都编写相同的静态 XML 并且想要一些替换.在这种情况下,我会有像

Presumably you're not just writing the same static XML every time and want some substitution. In this case I'd have lines like

var a = '%(message)s'

然后使用 % 运算符进行替换,例如

and then use the % operator to do the substitution, like

</html>""" % {"message": "I love &amp;aacute; letters"})