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?
XML escape characters There are only five: " " ' ' < < > > & & Escaping characters depends on where the special character is used. The examples can be validated at the W3C Markup Validation Service.
If the delimiter for the attribute value is the apostrophe, then the quote character is legal but the apostrophe character is not, because it would signal the end of the attribute value. If an apostrophe is needed, the character entity ' must be used.
Something like this?
>>> from xml.sax.saxutils import escape >>> escape("< & >") '< & >'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With