开发者

How to prevent lxml prom compacting elements?

开发者 https://www.devze.com 2023-01-01 10:23 出处:网络
Having following Python code: >>> from lxml import etree >>> root = etree.XML(\"<a>开发者_运维问答;<b></b></a>\")

Having following Python code:

>>> from lxml import etree
>>> root = etree.XML("<a>开发者_运维问答;<b></b></a>")
>>> etree.tostring(root)
'<a><b/></a>'

How can I force lxml to use "long" version?

Like

>>> etree.tostring(root)
'<a><b></b></a>'


>>> import lxml.html
>>> html = lxml.html.fromstring('<a><b></b></a>')
>>> lxml.html.tostring(html)
'<a><b></b></a>'

Mixing works as well:

>>> from lxml import etree
>>> import lxml.html
>>> xml = etree.XML('<a><b/></a>')
>>> lxml.html.tostring(xml)
'<a><b></b></a>'


Why do you want to? Both are equivalent in terms of XML's data model.


from the docs

http://lxml.de/tutorial.html#serialisation

    from lxml import etree
    xml = etree.XML('<a><b/></a>')
    etree.tostring(xml, method='html')
    '<a><b></b></a>'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号