Close a tag with no text in lxml

前端 未结 4 1928
[愿得一人]
[愿得一人] 2020-12-11 21:36

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

相关标签:
4条回答
  • 2020-12-11 21:56

    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>'
    
    0 讨论(0)
  • 2020-12-11 21:58

    Use lxml.html.tostring to serialize to HTML

    import lxml.html
    root = lxml.html.fromstring(mydocument)
    print(lxml.html.tostring(root))
    
    0 讨论(0)
  • 2020-12-11 22:02

    Use empty string '' like this:

    root = etree.Element('document')
    etree.SubElement(root, 'test').text = ''
    
    0 讨论(0)
  • 2020-12-11 22:04

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

    0 讨论(0)
提交回复
热议问题