xml.etree
Python API 中的 ElementTree 类有一个write()
记录可选method
参数的方法:
*method* -- either "xml" (default), "html, "text", or "c14n"
至少在 Python 3.5(在 Fedora 25 上)使用这种方法编写
import xml.etree.ElementTree as ET
# ... create some elements ...
tree = ET.ElementTree(root)
tree.write(filename, method='c14n')
只是抛出一个ValueError:
File "/usr/lib64/python3.5/xml/etree/ElementTree.py", line 751, in write
raise ValueError("unknown method %r" % method)
ValueError: unknown method 'c14n'
ElementTree 代码包含以下注释:
_serialize = {
"xml": _serialize_xml,
"html": _serialize_html,
"text": _serialize_text,
# this optional method is imported at the end of the module
# "c14n": _serialize_c14n,
}
这到底是什么意思?
如何在 Python 中将 ElementTree 序列化为 c14n XML 文件?