python - 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:
<ph type="0" x="1"></ph>
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.
解决方案
您正在尝试这样做:
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><ph type="0" x="1"></ph></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>
推荐阅读
- ios - 您如何以这样一种方式存储数据,使它们在删除然后安装 ios 后不会丢失?
- javascript - 原子中的 HTML JavaScript 错误
- c# - xamarin ios, MVVM light 加载页面时不绑定数据
- java - 在 Eclipse 中合理地打破 Java 异常
- jquery-ui-datepicker - 从 jQuery UI datepicker 设置 JavaScript 日期
- matlab - Matlab - 在具有某种颜色的复杂平原上绘制点
- spring-mvc - 如果用户具有特定角色,如何附加 CSS 类?
- sql-server - SQL 游标 SUM VARCHAR 重复
- rundeck - Rundeck 隐式变量
- javascript - 回调并将变量传递给第二个函数