In Python, I need to Canonicalize (c14n) an XML string.
Which module/package can I use for this? And how should I do this?
(I prefer to use default python 2.7 modules, without extra installs or patches.)
For reference see: http://www.w3.org/TR/xml-exc-c14n/
from 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()
from lxml docs:
write_c14n(self, file, exclusive=False, with_comments=True, compression=0, inclusive_ns_prefixes=None)
C14N write of document. Always writes UTF-8.
<..>
Also there is 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
Though obligatory check for version changes in these two libs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With