在Python中,我需要规范化(c14n)XML字符串。
我可以使用哪个模块/软件包?我该怎么做呢?
(我更喜欢使用默认的python 2.7模块,而无需额外的安装或补丁。)
有关参考,请参见:http : //www.w3.org/TR/xml-exc-c14n/
来自http://www.decalage.info/en/python/lxml-c14n
lxml提供了一种非常简单的方法来在python中执行c14n。<..>
这是显示如何使用lxml 2.1执行C14N的示例:
import lxml.etree as ET
et = ET.parse('file.xml')
output = StringIO.StringIO()
et.write_c14n(output)
print output.getvalue()
Run Code Online (Sandbox Code Playgroud)
来自lxml文档:
write_c14n(自身,文件,exclusive = False,with_comments = True,压缩= 0,inclusive_ns_prefixes =无)
C14N写文件。始终写入UTF-8。
<..>
还有libxml2:
XML C14N 1.0版提供了两个选项,它们提供了四种可能性(请参阅http://www.w3.org/TR/xml-c14n和 http://www.w3.org/TR/xml-exc-c14n/):
- 包含或排除的C14N
- 有无评论
libxml2允许在其C14N API中访问这些选项:http ://xmlsoft.org/html/libxml-c14n.html
虽然必须检查这两个库中的版本更改。