我在使用xml.dom.minidom在xml文件中转义"<"和">"时遇到困难.我试图获取unicode十六进制值并使用它而不是
http://slayeroffice.com/tools/unicode_lookup/
试图使用标准"<"和">",但仍然没有成功.
from xml.dom.minidom import Document
doc = Document()
e = doc.createElement("abc")
s1 = '<hello>bhaskar</hello>'
text = doc.createTextNode(s1)
e.appendChild(text)
e.toxml()
'<abc><hello>bhaskar</hello></abc>'
Run Code Online (Sandbox Code Playgroud)
与writexml()相同的结果还尝试通过在toxml()writexml()调用中指定编码'UTF-8','utf-8','utf'但结果相同.
from xml.dom.minidom import Document
doc = Document()
e = doc.createElement("abc")
s1 = u'<hello>bhaskar</hello>'
text = doc.createTextNode(s1)
e.appendChild(text)
e.toxml()
u'<abc>&lt;hello&gt;bhaskar&lt;/hello&gt;</abc>'
Run Code Online (Sandbox Code Playgroud)
试过其他方法,但结果相同.只有这样才能解决问题
import xml.dom.minidom as md
# XXX Hack to handle '<' and '>'
def wd(writer, data):
data = data.replace("<", "<").replace(">", ">")
writer.write(data)
md._write_data = wd
Run Code Online (Sandbox Code Playgroud)
编辑 - 这是代码.
import xml.dom.minidom as md
doc = md.Document()
entity_descr = …Run Code Online (Sandbox Code Playgroud)