也许我忽略了,但我在documentation中没有找到这个.
在Python的ElementTree中解析xml时,如何检测element|tag|node是否为self-closing
(或未配对,即以 />
结尾)或不?
创建 xml 文件时,如何明确声明我是否支持 self-closing
( />
) 或 explicitly closed
(即 </example>
)xml 标签?
如果 ElementTree 没有,其他 python 解析器是否能更好地处理这个问题?
最佳答案
此问题已于 2014 年 3 月由 python 3.4 解决。它添加了 short_empty_elements
ElementTree
的所有序列化函数的参数。
>>> from xml.etree import ElementTree as ET
>>> msg = ET.Element('msg',{'x': 'y'})
>>> ET.tostring(msg)
b'<msg x="y" />'
>>> ET.tostring(msg, short_empty_elements=False)
b'<msg x="y"></msg>'
关于python - 检测并控制 Python 中自关闭 xml 元素的创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14773668/