Python使用xpath解析带命名空间的XML

xpath解析XML简单明了,但是XML有命名空间的话就会出错了。解决方法是节点前加命名空间的前缀,下例中x、y是变量可以任意定义。

例如XML文档如下:

<a xmlns:a="http://www.jfsay.com" xmlns:b="http://jingfengshuo.com">
    <b>Text</b>
<a>

解析代码片段:

tree = etree.parse(path)
        root = tree.getroot()        
        for child in root:
             r=child.xpath('x:a/y:b/text()',namespaces={'x': 'www.jingfengshuo.com',
               'y': 'jingfengshuo.com'})[0] 

如果XML文档如下:

<a xmlns:="http://www.jfsay.com">
    <b>Text</b>
<a>

解析代码片段:

tree = etree.parse(path)
        root = tree.getroot()        
        for child in root:
             r=child.xpath('x:a/x:b/text()',namespaces={'x': 'www.jingfengshuo.com'})[0]

发表回复

您可以匿名评论,只有「评论」 是必填项,其他的都可以不填。如果填写电子邮箱地址,有人回复时您将收到邮件通知。