Python 在lxml中解析XML时如何不加载注释
我尝试使用lxml解析Python中的XML文件,如下所示:Python 在lxml中解析XML时如何不加载注释,python,xml,xml-parsing,comments,lxml,Python,Xml,Xml Parsing,Comments,Lxml,我尝试使用lxml解析Python中的XML文件,如下所示: objectify.parse(xmlPath, parserWithSchema) 但XML文件可能在奇怪的地方包含注释: <root> <text>Sam<!--comment-->ple text</text> <!--comment--> <float>1.2<!--comment-->3456</float&g
objectify.parse(xmlPath, parserWithSchema)
但XML文件可能在奇怪的地方包含注释:
<root>
<text>Sam<!--comment-->ple text</text>
<!--comment-->
<float>1.2<!--comment-->3456</float>
</root>
示例文本
1.23456
这是一种在解析之前不加载或删除注释的方法?在解析器()上设置
删除\u comments=True
:
或者,使用makeparser()
方法:
parser = objectify.makeparser(remove_comments=True)
tree = objectify.parse(xmlPath, parser=parser)
希望这能有所帮助。这对我不起作用。正确的方法是使用
parser=objectify.makeparser(remove\u comments=True)
如这里所示@RedX谢谢,我相应地改进了答案。
parser = objectify.makeparser(remove_comments=True)
tree = objectify.parse(xmlPath, parser=parser)