关闭 lxml 中没有文本的标签
我正在尝试使用 Python 和 lxml 输出 XML 文件
但是,我注意到一件事,如果标签没有文本,它不会自行关闭。一个例子是:
root = etree.Element('document')
rootTree = etree.ElementTree(root)
firstChild = etree.SubElement(root, 'test')
其输出是:
<document>
<test/>
</document
我希望输出是:
<document>
<test>
</test>
</document>
所以基本上我想关闭一个没有文本但用于属性值的标签。我该怎么做?还有,这样的标签叫什么?我本来想用谷歌搜索它,但我不知道如何搜索它。
I am trying to output a XML file using Python and lxml
However, I notice one thing that if a tag has no text, it does not close itself. An example of this would be:
root = etree.Element('document')
rootTree = etree.ElementTree(root)
firstChild = etree.SubElement(root, 'test')
The output of this is:
<document>
<test/>
</document
I want the output to be:
<document>
<test>
</test>
</document>
So basically I want to close a tag which has no text, but is used to the attribute value. How do I do that? And also, what is such a tag called? I would have Googled it, but I don't know how to search for it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意,
和
的含义完全相同。您想要的是测试标签实际上有一个由单个换行符组成的文本。然而,没有文本的空标签通常写为
并且坚持将其显示为 是没有意义的。
。Note that
<test></test>
and<test/>
mean exactly the same thing. What you want is for the test-tag to actually do have a text that consists in a single linebreak. However, an empty tag with no text is usually written as<test/>
and it makes very little sense to insist on it to appear as<test></test>
.为了澄清@ymv的答案,以防对其他人有帮助:
To clarify @ymv answer in case it might be of help to others:
使用 lxml.html.tostring 序列化为 HTML
Use lxml.html.tostring to serialize to HTML
使用空字符串 '' 像这样:
Use empty string '' like this: