Close a tag with no text in lxml

女生的网名这么多〃 提交于 2019-12-17 20:52:31

问题


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.


回答1:


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




回答2:


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



回答3:


Use lxml.html.tostring to serialize to HTML

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



回答4:


Use empty string '' like this:

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


来源:https://stackoverflow.com/questions/2771383/close-a-tag-with-no-text-in-lxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!