简体   繁体   English

如何使用python:xml.etree.ElementTree在soap响应中从多个名称空间获取数据

[英]How to fetch data from multiple namespaces in a soap response using python: xml.etree.ElementTree

How to fetch value of through the correct xpath using python xml.etree.ElementTree 如何使用python xml.etree.ElementTree通过正确的xpath获取值

This is the 'soap.xml' file 这是'soap.xml'文件

    <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header>
        <platformMsgs:documentInfo xmlns:platformMsgs="urn:messages_2014_1.platform.webservices.netsuite.com">
            <platformMsgs:nsId>WEBSERVICES_3883026_SB1_103120188552030381995151284_f039e9cc</platformMsgs:nsId>
        </platformMsgs:documentInfo>
    </soapenv:Header>
    <soapenv:Body>
        <searchResponse xmlns="urn:messages_2014_1.platform.webservices.netsuite.com">
            <platformCore:searchResult xmlns:platformCore="urn:core_2014_1.platform.webservices.netsuite.com">
                <platformCore:status isSuccess="true"/>
                <platformCore:totalRecords>200</platformCore:totalRecords>
                <platformCore:pageSize>1000</platformCore:pageSize>
                <platformCore:totalPages>1</platformCore:totalPages>
                <platformCore:pageIndex>1</platformCore:pageIndex>
                <platformCore:searchId>WEBSERVICES_3883026_SB1_103120188552030381995151284_f039e9cc</platformCore:searchId>
            </platformCore:searchResult>
        </searchResponse>
    </soapenv:Body>
</soapenv:Envelope>

Here is my code. 这是我的代码。

    import xml.etree.ElementTree as ET
    tree = ET.parse('soap.xml') 
    print  tree.findall('.//{http://schemas.xmlsoap.org/soap/envelope/}platformCore')

But it return [] instead of value = '200' 但它返回[]而不是value ='200'

You need to use the correct namespace (nsmap), then you can search easily using prefixes: 您需要使用正确的名称空间(nsmap),然后可以使用前缀轻松进行搜索:

from lxml import etree
tree = etree.parse('soap.xml')
nsmap = {'platformCore': 'urn:core_2014_1.platform.webservices.netsuite.com'}
print (tree.findall('.//platformCore:totalRecords', nsmap)[0].text)

Returns: 返回:

200

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 借助xml.etree.ElementTree从具有名称空间的XML检索数据 - retrieve data from XML with namespaces with the help of xml.etree.ElementTree python:xml.etree.ElementTree,删除“命名空间” - python: xml.etree.ElementTree, removing "namespaces" Python:忽略xml.etree.ElementTree中的命名空间? - Python: ignoring namespaces in xml.etree.ElementTree? 从Python中的XML数据创建字典(使用xml.etree.ElementTree) - Creating Dictionaries from XML Data in Python (with xml.etree.ElementTree) 如何在Python中使用xml.etree.ElementTree生成的xml的同一标签内添加多个值? - How to add multiple values inside the same tag of xml generated using xml.etree.ElementTree in Python? How to get all relevant fields from a XML file into a pandas dataframe in Python using xml.etree.ElementTree? - How to get all relevant fields from a XML file into a pandas dataframe in Python using xml.etree.ElementTree? Python 和 xml.etree.ElementTree - - Python and xml.etree.ElementTree - 如何处理在 python xml.etree.ElementTree 中修改 XML 的隐式命名空间 - How to handle implicit namespaces modifying XML in python xml.etree.ElementTree 如何使用python中的xml.etree.ElementTree解析当前节点中的所有子元素和孙元素元素 - How to parse all children and grandchildren elements from a current node using xml.etree.ElementTree in python 使用xml.etree.ElementTree在python中解析XML分析问题 - XML Parsing issue in python using xml.etree.ElementTree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM