Finding top-level xml comments using Python's ElementTree

余生长醉 提交于 2019-12-07 17:53:41

问题


I'm parsing an xml file using Python's ElementTree, like that:

et = ElementTree(file=file("test.xml"))

test.xml starts with a few lines of xml comments.

Is there a way to get those comments from et?


回答1:


For ElementTree 1.2.X there is an article on Reading processing instructions and comments with ElementTree (http://effbot.org/zone/element-pi.htm).


EDIT:

The alternative would be using lxml.etree which implements the ElementTree API. A quote from ElementTree compatibility of lxml.etree :

ElementTree ignores comments and processing instructions when parsing XML, while etree will read them in and treat them as Comment or ProcessingInstruction elements respectively.



来源:https://stackoverflow.com/questions/541100/finding-top-level-xml-comments-using-pythons-elementtree

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