简体   繁体   English

如何查看lxml元素的文本表示?

[英]How can I view a text representation of an lxml element?

If I'm parsing an XML document using lxml, is it possible to view a text representation of an element? 如果我使用lxml解析XML文档,是否可以查看元素的文本表示? I tried to do : 我试着这样做:

print repr(node)

but this outputs 但是这个输出

<Element obj at b743c0>

What can I use to see the node like it exists in the XML file? 我可以使用什么来查看XML文件中存在的节点? Is there some to_xml method or something? 是否有一些to_xml方法或什么?

From http://lxml.de/tutorial.html#serialisation 来自http://lxml.de/tutorial.html#serialisation

>>> root = etree.XML('<root><a><b/></a></root>')

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

>>> print(etree.tostring(root, xml_declaration=True))
<?xml version='1.0' encoding='ASCII'?>
<root><a><b/></a></root>

>>> print(etree.tostring(root, encoding='iso-8859-1'))
<?xml version='1.0' encoding='iso-8859-1'?>
<root><a><b/></a></root>

>>> print(etree.tostring(root, pretty_print=True))
<root>
  <a>
    <b/>
  </a>
</root>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM