[BangPypers] escaping for xml

bhaskar jain bhaskar.jain2002 at gmail.com
Mon Oct 26 06:57:41 CET 2009


Hello,

  I am stuck while escaping "<" and ">" in the xml file using
xml.dom.minidom.
  I tried to get the unicode hex value and use that instead  (
http://slayeroffice.com/tools/unicode_lookup/)
  Tried to use the standard "&lt;" and "&gt;" but still with no success.
  I saw similar bugs in python bugzilla -
http://bugs.python.org/issue5752and some others but not sure.


>>> from xml.dom.minidom import Document
>>> doc = Document()
>>> e = doc.createElement("abc")
>>> s1 = '<hello>bhaskar</hello>'
>>> text = doc.createTextNode(s1)
>>> e.appendChild(text)
<DOM Text node "<hello>bha...">

>>> e.toxml()
'<abc>&lt;hello&gt;bhaskar&lt;/hello&gt;</abc>'

same result with writexml()


>>> from xml.dom.minidom import Document
>>> doc = Document()
>>> e = doc.createElement("abc")
>>> s1 = u'&lt;hello&gt;bhaskar&lt;/hello&gt;'
>>> text = doc.createTextNode(s1)
>>> e.appendChild(text)
<DOM Text node "&lt;hello&...">

>>> e.toxml()
u'<abc>&amp;lt;hello&amp;gt;bhaskar&amp;lt;/hello&amp;gt;</abc>'


Tried other ways but with same results.
Only way is to override the writer and replace like shown here -
http://www.velocityreviews.com/forums/t330646-a-simple-xmldomminidom-question.html
.

Has anybody solved this?

I want to have a text node with contents as   "ds:X509Certificate>-----BEGIN
CERTIFICATE----- </ds:X509Certificate>".
Creating an element as "ds:x509Certificate" and then creating a text node
with the certificate and appending it to the element works but a third
party-library fails to parse it.
So would like to have a single text node with the contents.

Thanks.

--Bhaskar.


More information about the BangPypers mailing list