Create xml with elementtree ET and xml escaping

nenad.cikic at nenad.cikic at
Tue Dec 11 18:47:26 CET 2012

Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here.

I am new to xml processing in python.
I am looking to create XML. Xml is not too difficult so I thought to create it manually using ElementTree.
First I noted that ET.toString does escape <>& but not " and '
Is that normal?

Since I have also the need to sign the XML I need the ability to create xml but without xml escaping (unescaped data are signed).
If i do ET.toString(root,'utf8',text') i do not get the xml tags and if I do ET.toString(root,'utf8') I get escaped chars.
For example:

outputs to
"<?xml version='1.0' encoding='utf8'?>\n<a><b>\xc5\xa1\xc4\x91<>&</b></a>"

outputs to

and I need before singing
and after signing

Is there some way other than string replace?

More information about the Python-list mailing list