我正在使用Python的ElementTree解析xml文件,如下所示:
et = ElementTree(file=file("test.xml"))
test.xml以几行xml注释开头.
有没有办法从et得到这些评论?
解决方法:
对于ElementTree 1.2.X,有一篇关于Reading processing instructions and comments with ElementTree
(http://effbot.org/zone/element-pi.htm)的文章.
编辑:
另一种选择是使用实现ElementTree API的lxml.etree.引用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.