代码之家  ›  专栏  ›  技术社区  ›  Almad

如何防止lxml prom压缩元素?

  •  0
  • Almad  · 技术社区  · 14 年前

    具有以下python代码:

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

    如何强制LXML使用“长”版本?

    喜欢

    >>> etree.tostring(root)
    '<a><b></b></a>'
    
    3 回复  |  直到 13 年前
        1
  •  3
  •   newtover    14 年前
    >>> import lxml.html
    >>> html = lxml.html.fromstring('<a><b></b></a>')
    >>> lxml.html.tostring(html)
    '<a><b></b></a>'
    

    搅拌工程:

    >>> from lxml import etree
    >>> import lxml.html
    >>> xml = etree.XML('<a><b/></a>')
    >>> lxml.html.tostring(xml)
    '<a><b></b></a>'
    
        2
  •  2
  •   djc    14 年前

    你为什么想要?两者在XML的数据模型方面是等效的。

        3
  •  1
  •   macm    13 年前

    从文档

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