转义字符串以在XML中使用 [英] Escaping strings for use in XML
本文介绍了转义字符串以在XML中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Python的 xml.dom.minidom
创建XML文档。 (逻辑结构-> 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?
推荐答案
您是说要执行以下操作:
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)
然后,您将得到很好的转义XML字符串:
Then you will get nicely escaped XML string:
>>> e.toxml()
'<p><bar><a/><baz spam="eggs"> & blabla &entity;</></p>'
这篇关于转义字符串以在XML中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文