Close tag without text in lxml

I am trying to output an XML file using Python and lxml

However, I notice one thing: if the tag has no text, it does not close. An example of this might be:

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

The result of this:

 <document> <test/> </document 

I want the output to be:

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

So basically I want to close a tag that has no text but is used for the attribute value. How can I do it? And also, what is such a tag called? I would googled, but I don’t know how to look for it.

+4
source share
4 answers

Note that <test></test> and <test/> mean the same thing. You want the test tag to really have text consisting of a single line break. However, an empty tag without text is usually written as <test/> , and it does not make much sense to insist that it be displayed as <test></test> .

+7
source

To clarify @ymv's answer in case it can help 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
source

Use lxml.html.tostring to serialize in HTML

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

Use the empty string '' as follows:

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

Source: https://habr.com/ru/post/1308860/


All Articles