Escaping strings for use in XML

Posted on

Question :

Escaping strings for use in XML

I’m using Python’s xml.dom.minidom to create an XML document. (Logical structure -> XML string, not the other way around.)

How do I make it escape the strings I provide so they won’t be able to mess up the XML?

Answer #1:

Do you mean you do something like this:

from xml.dom.minidom import Text, Element

t = Text()
e = Element('p')

t.data = '<bar><a/><baz spam="eggs"> & blabla &entity;</>'
e.appendChild(t)

Then you will get nicely escaped XML string:

>>> e.toxml()
'<p>&lt;bar&gt;&lt;a/&gt;&lt;baz spam=&quot;eggs&quot;&gt; &amp; blabla &amp;entity;&lt;/&gt;</p>'
Answered By: Andrey Vlasovskikh

Answer #2:

Something like this?

>>> from xml.sax.saxutils import escape
>>> escape("< & >")   
'&lt; &amp; &gt;'
Answered By: mbarkhau

Answer #3:

xml.sax.saxutils does not escape quotation characters (“)

So here is another one:

def escape( str ):
str = str.replace("&", "&amp;")
str = str.replace("<", "&lt;")
str = str.replace(">", "&gt;")
str = str.replace(""", ""&
quot
)

Leave a Reply

Your email address will not be published. Required fields are marked *