【问题标题】:Write ElementTree as canonicalized XML (c14n)将 ElementTree 编写为规范化的 XML (c14n)
【发布时间】:2017-04-20 19:12:09
【问题描述】:

Python xml.etree 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 文件?

【问题讨论】:

    标签: python xml fedora-25 c14n


    【解决方案1】:

    C14N 序列化只是作为 Fredrik Lundh 开发的原始 ElementTree 独立库的实验性扩展实现的,从未作为正式版本的一部分。

    当库的 1.3 分支导入 Python 标准库时,包含了可选库的支持代码,但 C14N 序列化从未包含。稍后在可选组件导入代码was removed altogether 中,但代码中仍然有几个提到 C14N。但是,文档从未提及 C14N,其余提及的内容应该从 Python 中删除。

    但是,如果你必须让它工作,你总是可以download the original ElementC14N.py module,并更新它以使用当前的 ElementTree 库。这确实需要一些工作;代码库是 Python 2 特定的,需要更新以处理 Unicode。

    【讨论】:

      猜你喜欢
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      相关资源
      最近更新 更多