Python:如何使用 lxml objectify 的 iterchildren 获取不同命名空间中 sibling 的详细信息

标签 python xml lxml.objectify

这是我的 xml 文件。

get_subscribers_result.xml

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns3:GetSubscriberResponse xmlns:ns3="http://example.com/123/ss/subscribermgmt/v1_0" xmlns:ns2="http://example.com/123/ss/base/v1_0" xmlns:ns4="http://example.com/123/ss/xyz/v1_0" >
            <ns3:subscriber>
                <ns2:created>2015-10-20T16:02:58.831Z</ns2:created>
                <ns2:createdBy>admin</ns2:createdBy>
                <ns2:lastModified>2015-10-20T16:02:58.824Z</ns2:lastModified>
                <ns2:lastModifiedBy>super</ns2:lastModifiedBy>
                <ns2:subscriberDetail>
                    <ns2:key>address</ns2:key>
                    <ns2:value>1st vivekanandar street</ns2:value>
                </ns2:subscriberDetail>
                <ns2:subscriberDetail>
                    <ns2:key>state</ns2:key>
                    <ns2:value>Abu Dhabi</ns2:value>
                </ns2:subscriberDetail>
            </ns3:subscriber>
        </ns3:GetSubscriberResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

现在,我看到了来自 http://davekuhlman.org/Objectify_files/weather_test.py 的例子使用 iterchildren 的地方。

如果没有命名空间,代码就可以工作。 如果我的 xml 中没有命名空间,下面的代码将适用于我拥有的 xml。

    obj2 = lxml.objectify.parse("get_subscribers_result.xml")
    root = obj2.getroot()

    tag = '{http://example.com/123/ss/base/v1_0}subscriberDetail'

    for subscriberDetail in enumerate(root.subscriber.iterchildren(tag=tag)):
                   print subscriberDetail.key
                   print subscriberDetail.value
                   print "*********************************"

如果我运行这个,我得到

AttributeError: no such child: {http://schemas.xmlsoap.org/soap/envelope/}subscriber

那是因为 subscriber 不属于默认命名空间,这是正确的!

我试过了

    for subscriberDetail in enumerate(root.{http://example.com/123/ss/subscribermgmt/v1_0}subscriber.iterchildren(tag=tag)):

有什么想法可以在存在命名空间时实现这一点吗?

最佳答案

您可以改用“命名空间感知”xpath 函数并明确指定命名空间:

from lxml import objectify    

obj2 =  lxml.objectify.parse('get_subscribers_result.xml')
root = obj2.getroot()

tag = '{http://example.com/123/ss/base/v1_0}subscriberDetail'

for subscriberDetail in (root.xpath('//ns2:subscriberDetail', namespaces={'ns2': 'http://example.com/123/ss/base/v1_0'})):
               print subscriberDetail.key
               print subscriberDetail.value               
               print "*********************************"

如果你想遍历所有的节点包括他们的 child ,你可以这样做:

for details in root.xpath('//SOAP-ENV:Envelope/descendant-or-self::*', namespaces={'SOAP-ENV':'http://schemas.xmlsoap.org/soap/envelope/','ns2': 'http://example.com/123/ss/base/v1_0','ns3':"http://example.com/123/ss/subscribermgmt/v1_0"}):
    for element in details:        
        cleaned_tag = element.tag.replace('{'+element.nsmap[element.prefix]+'}','')      
       if element.text:
           print("%s --> %s" % (element.prefix+':'+cleaned_tag,element.text))

关于Python:如何使用 lxml objectify 的 iterchildren 获取不同命名空间中 sibling 的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959772/

相关文章:

java - 更改操作栏和其他元素的颜色

python - 获取用户输入来读取文件中的那么多行

xml - PostgreSQL 8.3 数据类型 : xml vs varchar

java - 如何在 Maven pom.xml 中定义属性并从命令行传递参数?

python - 从对象化 XML 中高效获取多个切片

python - lxml.objectify 和前导零

Python lxml 对象化 : Strange behaviour when changing an elements value

python - 我无法专注于上传按钮来上传简历

javascript - GAE Python JavaScript : How to insert response data from facebook login into the datastore?

python - 测试 Django 信号的正确方法