关闭 lxml 中没有文本的标签

发布于 2024-08-31 12:10:08 字数 482 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放赐 2024-09-07 12:10:08

请注意, 的含义完全相同。您想要的是测试标签实际上有一个由单个换行符组成的文本。然而,没有文本的空标签通常写为 并且坚持将其显示为 是没有意义的。

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>.

又爬满兰若 2024-09-07 12:10:08

为了澄清@ymv的答案,以防对其他人有帮助:

from lxml import etree

root = etree.Element('document')
rootTree = etree.ElementTree(root)
firstChild = etree.SubElement(root, 'test')

print(etree.tostring(root, method='html'))
### b'<document><test></test></document>'

To clarify @ymv answer in case it might be of help to others:

from lxml import etree

root = etree.Element('document')
rootTree = etree.ElementTree(root)
firstChild = etree.SubElement(root, 'test')

print(etree.tostring(root, method='html'))
### b'<document><test></test></document>'
拥抱没勇气 2024-09-07 12:10:08

使用 lxml.html.tostring 序列化为 HTML

import lxml.html
root = lxml.html.fromstring(mydocument)
print(lxml.html.tostring(root))

Use lxml.html.tostring to serialize to HTML

import lxml.html
root = lxml.html.fromstring(mydocument)
print(lxml.html.tostring(root))
仄言 2024-09-07 12:10:08

使用空字符串 '' 像这样:

root = etree.Element('document')
etree.SubElement(root, 'test').text = ''

Use empty string '' like this:

root = etree.Element('document')
etree.SubElement(root, 'test').text = ''
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文