There are 5 escape characters which need to be encoded:
" -> "
' -> '
< -> <
> -> >
& -> &
So I created a really simple python function which I added to my create RSS feed program.
# encode xml escape characters
def encodeXMLText(text):
text = text.replace("&", "&")
text = text.replace("\"", """)
text = text.replace("'", "'")
text = text.replace("<", "<")
text = text.replace(">", ">")
return text
def encodeXMLText(text):
text = text.replace("&", "&")
text = text.replace("\"", """)
text = text.replace("'", "'")
text = text.replace("<", "<")
text = text.replace(">", ">")
return text
Which is called by passing the text I need to make xml safe:
xmlText = encodeXMLText("Some text with escape characters & " ' < > you want to make xml safe")
No comments:
Post a Comment
Note: only a member of this blog may post a comment.