【问题标题】:How to not load the comments while parsing XML in lxml在 lxml 中解析 XML 时如何不加载注释
【发布时间】:2013-08-21 06:01:20
【问题描述】:

我尝试像这样使用 lxml 在 Python 中解析 XML 文件:

objectify.parse(xmlPath, parserWithSchema)

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

<root>
    <text>Sam<!--comment-->ple text</text>
    <!--comment-->
    <float>1.2<!--comment-->3456</float>
</root>

解析前不加载或删除cmets的方法?

【问题讨论】:

    标签: python xml xml-parsing comments lxml


    【解决方案1】:

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

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

    或者,使用makeparser() 方法:

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

    希望对您有所帮助。

    【讨论】:

    • 这对我不起作用。正确的方法是使用parser = objectify.makeparser(remove_comments=True),如此处所示stackoverflow.com/a/7513498/551045
    • @RedX 谢谢,我已经相应地改进了答案。
    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多