Getting contents of an lxml.objectify comment

不羁的心 提交于 2019-12-12 02:39:08

问题


I have an XML file that I'm reading using python's lxml.objectify library.

I'm not finding a way of getting the contents of an XML comment:

<data>
  <!--Contents 1-->
  <some_empty_tag/>
  <!--Contents 2-->
</data>

I'm able to retrieve the comment (is there a better way? xml.comment[1] does not seem to work):

xml = objectify.parse(the_xml_file).getroot()
for c in xml.iterchildren(tag=etree.Comment):
   print c.???? # how do i print the contets of the comment?
   # print c.text  # does not work
   # print str(c)  # also does not work

What is the correct way?


回答1:


You just need to convert the child back to string to extract the comments, like this:

In [1]: from lxml import etree, objectify

In [2]: tree = objectify.fromstring("""<data>
   ...:   <!--Contents 1-->
   ...:   <some_empty_tag/>
   ...:   <!--Contents 2-->
   ...: </data>""")

In [3]: for node in tree.iterchildren(etree.Comment):
   ...:     print(etree.tostring(node))
   ...:
b'<!--Contents 1-->'
b'<!--Contents 2-->'

Of course you may want to strip the unwanted wrapping.



来源:https://stackoverflow.com/questions/39390653/getting-contents-of-an-lxml-objectify-comment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!