怎样在lxm解析XML时避免加载评论

2024-10-01 13:34:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用lxml在Python中解析XML文件,如下所示:

objectify.parse(xmlPath, parserWithSchema)

但XML文件可能在奇怪的地方包含注释:

^{pr2}$

这是一种在解析之前不加载或删除注释的方法吗?在


Tags: 文件方法parse地方xmllxmlobjectifypr2
1条回答
网友
1楼 · 发布于 2024-10-01 13:34:28

在解析器(documentation)上设置remove_comments=True

from lxml import etree, objectify

parser = etree.XMLParser(remove_comments=True)
tree = objectify.parse(xmlPath, parser=parser)

或者,使用makeparser()方法:

^{pr2}$

希望有帮助。在

相关问题 更多 >