Fixing the XML batteries

Stefan Behnel stefan_ml at behnel.de
Tue Dec 13 07:32:53 EST 2011


Serhiy Storchaka, 13.12.2011 13:17:
> 09.12.11 17:09, Dirkjan Ochtman wrote:
>> An at least somewhat informed +1 from me. The ElementTree API is a
>> very good way to deal with XML from Python, and it deserves to be
>> promoted over the included alternatives.
>>
>> Let's deprecate the NiCad batteries and try to guide users toward the
>> Li-Ion ones.

I stripped my name from the quoted context because I didn't say this.


> 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.


> doc = xml.dom.minidom.parse( io.BytesIO( data0 ) )
> encoding = forceEncoding or doc.encoding or str( 'UTF-8' )
> writer = io.BytesIO()
> writer = codecs.getwriter( encoding )( writer, 'xmlcharrefreplace' )
> doc.writexml( writer, encoding = encoding )
> data = writer.getvalue()
>
> 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".

Stefan




More information about the Python-list mailing list