Escaping strings for use in XML
Solution 1:
Something like this?
>>> from xml.sax.saxutils import escape
>>> escape("< & >")
'< & >'
Solution 2:
xml.sax.saxutils does not escape quotation characters (")
So here is another one:
def escape( str ):
str = str.replace("&", "&")
str = str.replace("<", "<")
str = str.replace(">", ">")
str = str.replace("\"", """)
return str
if you look it up then xml.sax.saxutils only does string replace
Solution 3:
xml.sax.saxutils.escape
only escapes &
, <
, and >
by default, but it does provide an entities
parameter to additionally escape other strings:
from xml.sax.saxutils import escape
def xmlescape(data):
return escape(data, entities={
"'": "'",
"\"": """
})
xml.sax.saxutils.escape
uses str.replace()
internally, so you can also skip the import and write your own function, as shown in MichealMoser's answer.
Solution 4:
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><bar><a/><baz spam="eggs"> & blabla &entity;</></p>'