[BangPypers] escaping for xml

Shivaraj M S shivraj.ms at gmail.com
Mon Oct 26 16:03:41 CET 2009


This may be a solution if I am not getting you wrong.
1.write the whole string to a file 
2.feed that to xml.dom.minidom.parse 
3.retrieve whole or childnodes from toxml



BhaskarJain wrote:
> 
> 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.
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
> 
> 

-- 
View this message in context: http://www.nabble.com/escaping-for-xml-tp26054890p26061412.html
Sent from the BangPypers - Bangalore Python Users Group mailing list archive at Nabble.com.



More information about the BangPypers mailing list