Python 专属 XML 规范化 (xml-exc-c14n) [英] Python Exclusive XML Canonicalization (xml-exc-c14n)

查看:26
本文介绍了Python 专属 XML 规范化 (xml-exc-c14n)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Python, I need to Canonicalize (c14n) an XML string.

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

Which module/package can I use for this? And how should I do this?

(我更喜欢使用默认的 python 2.7 模块,没有额外的安装或补丁.)

(I prefer to use default python 2.7 modules, without extra installs or patches.)

参考:http://www.w3.org/TR/xml-exc-c14n/

推荐答案

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

lxml 提供了一种在 python 中执行 c14n 的非常简单的方法.<...>

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

以下示例展示了如何使用 lxml 2.1 执行 C14N:

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 文档:

from lxml docs:

write_c14n(自我、文件、独占=False, with_comments=True,压缩=0,inclusive_ns_prefixes=无)

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

C14N 写入文档.始终写入 UTF-8.

C14N write of document. Always writes UTF-8.

<...>

还有 libxml2:

Also there is libxml2:

XML C14N 1.0 版提供了两个选项,使四个可能性(见 http://www.w3.org/TR/xml-c14nhttp://www.w3.org/TR/xml-exc-c14n/):

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/):

  • 包容性或专属性 C14N
  • 有或没有评论

libxml2 允许在其 C14N API 中访问这些选项:http://xmlsoft.org/html/libxml-c14n.html

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

尽管必须检查这两个库中的版本更改.

Though obligatory check for version changes in these two libs.

这篇关于Python 专属 XML 规范化 (xml-exc-c14n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆