gpt4 book ai didi

Python 独有的 XML 规范化 (xml-exc-c14n)

转载 作者:数据小太阳 更新时间:2023-10-29 02:23:58 25 4
gpt4 key购买 nike

在 Python 中,我需要规范化 (c14n) XML 字符串。

我可以为此使用哪个模块/包?我应该怎么做?

(我更喜欢使用默认的 python 2.7 模块,无需额外安装或补丁。)

引用:http://www.w3.org/TR/xml-exc-c14n/

最佳答案

来自 http://www.decalage.info/en/python/lxml-c14n

lxml provides a very easy way to do c14n in python. <..>

Here is an example showing how to perform C14N using lxml 2.1:

import lxml.etree as ET
et = ET.parse('file.xml')
output = StringIO.StringIO()
et.write_c14n(output)
print output.getvalue()

来自 lxml 文档:

write_c14n(self, file, exclusive=False, with_comments=True, compression=0, inclusive_ns_prefixes=None)

C14N write of document. Always writes UTF-8.

<..>

还有libxml2:

XML C14N version 1.0 provides two options which make four possibilities (see http://www.w3.org/TR/xml-c14n and http://www.w3.org/TR/xml-exc-c14n/):

  • Inclusive or Exclusive C14N
  • With or without comments

libxml2 gives access to these options in its C14N API: http://xmlsoft.org/html/libxml-c14n.html

虽然必须检查这两个库中的版本更改。

关于Python 独有的 XML 规范化 (xml-exc-c14n),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com