使用Python的ElementTree查找顶级xml注释 [英] Finding top-level xml comments using Python's ElementTree

查看:96
本文介绍了使用Python的ElementTree查找顶级xml注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python的ElementTree解析xml文件,例如:

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

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

test.xml以几行xml注释开头。

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

有没有办法从et中获得这些评论?

Is there a way to get those comments from et?

推荐答案

对于ElementTree 1.2.X,有一篇文章使用ElementTree
阅读处理说明和注释
http://effbot.org/zone/element-pi.htm)。

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

编辑:

替代方法是使用实​​现ElementTree API的 lxml.etree lxml.etree
的元素树兼容性的引用:

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


ElementTree在解析
XML时会忽略注释

处理指令,而 etree会在其中读取它们
分别将它们视为Comment或
ProcessingInstruction元素

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

这篇关于使用Python的ElementTree查找顶级xml注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆