Fixing the XML batteries

Stefan Behnel stefan_ml at behnel.de
Tue Dec 13 09:59:52 EST 2011


Serhiy Storchaka, 13.12.2011 15:27:
> 13.12.11 14:32, Stefan Behnel написав(ла):
>> I stripped my name from the quoted context because I didn't say this.
>
> I am glad to hear this. ;)

It matches my opinion though.


>>> I use xml.dom.minidom for XML canonization and convertion:
>> Do you mean "canonicalisation"? I.e. C14N? That's not what the code
>> below is doing, not at all.
>
> No, only converting to certain textual representation. Converting entities
> to text (if possible), converting "'" to '"' for quoting, sorting
> attributes, etc.

Yes, that's what C14N is there for, typically used for cryptography, 
hashing, etc. However, MiniDOM doesn't implement that standard, so you're 
on your own here.


>>> How to do this with xml.etree.ElementTree?
>> In Python 2.7/3.2, ElementTree has support for C14N serialisation, just
>> pass the option method="c14n".
>
> Thanks, I will try this. But I need 2.6+ compatibility.

The ET module is actually quite short (<1700 lines), so you can just copy 
the Py2.7 version into your sources and optionally import it on older 
Python releases. Since you only seem to depend on the serialiser (which is 
worth using anyway because it is much faster in the Py2.7 version), older 
platform versions of cET should also work just fine with that module copy, 
so you can basically just import everything from xml.etree.cElementTree and 
use the ElementTree class and the tostring() function from your own local 
version if the platform version is too old.

Note that ET is also still available as a separately installable package, 
may or may not be simpler to use for you.

Stefan




More information about the Python-list mailing list