关闭lxml中没有文本的标签 [英] Close a tag with no text in lxml

查看:70
本文介绍了关闭lxml中没有文本的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python和lxml输出XML文件

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')

此输出为:

<document>
<test/>
</document

我希望输出为:

<document>
<test>
</test>
</document>

因此,基本上我想关闭一个没有文本但用于属性值的标签.我怎么做?而且,这种标签叫什么?我可以用Google搜索它,但是我不知道如何搜索它.

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.

推荐答案

请注意,<test></test><test/>的含义完全相同.您想要的是测试标签实际上具有包含单个换行符的文本.但是,没有文本的空标签通常写为<test/>,而坚持将其显示为<test></test>几乎没有任何意义.

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

这篇关于关闭lxml中没有文本的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆