我正尝试使用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/> </documentI 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.
最满意答案
请注意, <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>.
更多推荐
发布评论