首页 > 解决方案 > How to write unescaped string to a XML element with ElementTree?

问题描述

I have a string variable contents with following value:

<ph type="0" x="1"></ph>

I try to write it to a XML element as follows:

elemen_ref.text = contents

After I write XML tree to a file and check it with Notepad++, I see following value written to the XML element:

&lt;ph type="0" x="1"&gt;&lt;/ph&gt;

How do I write unescaped string? Please note that this value is copied from another XML element which remains intact after writing tree to a file, so the issue is with assigning value to a text attribute.

标签: pythonxml-parsingelementtree

解决方案


您正在尝试这样做:

import xml.etree.ElementTree as ET

root = ET.Element('root')
content_str = '<ph type="0" x="1"></ph>'
root.text = content_str

print(ET.tostring(root))
#  <root>&lt;ph type="0" x="1"&gt;&lt;/ph&gt;</root>

这本质上是将 XML“注入”到元素的文本属性中。这不是正确的方法。

相反,您应该将content字符串转换为可以附加到现有 XML 节点的实际 XML 节点。

import xml.etree.ElementTree as ET

root = ET.Element('root')
content_str = '<ph type="0" x="1"></ph>'
content_element = ET.fromstring(content_str)
root.append(content_element)

print(ET.tostring(root))
#  <root><ph type="0" x="1" /></root>

如果你坚持,你可以使用unescape

import xml.etree.ElementTree as ET
from xml.sax.saxutils import unescape

root = ET.Element('root')
content_str = '<ph type="0" x="1"></ph>'
root.text = content_str

print(unescape(ET.tostring(root).decode()))
#  <root><ph type="0" x="1"></ph></root>

推荐阅读