如何使用python读取具有名称空间的XML字符串

时间:2018-12-12 11:33:07

标签: python xml python-3.x elementtree

我想从XML响应中读取标记,该标记中包含很多命名空间。     之前它可以正常工作,但是现在已经停止工作了:(

代码:

以下代码以前曾起作用,但现在不起作用

response = requests.post(url,data=body,headers=Headers)
tree = ET.fromstring(response.content)
lst = tree.find('.//Id').text
print('request submitted Request Id: '+str(lst))

Error:
Traceback (most recent call last):
  File "p3.py", line 179, in <module>
    lst = tree.find('.//Id').text
AttributeError: 'NoneType' object has no attribute 'text'

我尝试了“关注”,但大部分时间返回[] or None都不起作用。

st=(response.content)
st1=(st.decode('utf-8'))  
root=ET.fromstring(st1)

ID =root.findall("./findContractResponse/")
#root=tree.getroot()
#print(root.tag)
#print(root.attrib)

# xmlstr is your xml in a string
#root = lxml.etree.fromstring(st1)
#textelem = root.find('Body/findContractResponse/result/Value/Id')
#print (textelem.text)
namespaces = {'ns1': 'http://xmlns.oracle.com/apps/contracts/coreAuthoring/contractService/'}
ns1="http://xmlns.oracle.com/apps/contracts/coreAuthoring/contractService/"
#root = ET.fromstring(xml)
print(root.findall('ns1:Value', namespaces))
#str=root.findall('ns1:Value', namespaces)
#print(str)
print(root.find('{http://xmlns.oracle.com/apps/contracts/coreAuthoring/contractService/}Value'))

以下是我从SOAP请求获得的XML响应

    xml ="""<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:typ="http://xmlns.oracle.com/apps/contracts/coreAuthoring/contractService/types/">
               <env:Header>
                  <wsa:Action>http://xmlns.oracle.com/apps/contracts/coreAuthoring/contractService//ContractService/findContractResponse</wsa:Action>
                  <wsa:MessageID>urn:uuid:12310951-51ba-4f8c-af28-71f22f65ed64</wsa:MessageID>
                  <wsse:Security env:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                     <wsu:Timestamp wsu:Id="Timestamp-1sBNCU5ovGHx3N8YmAzKIQ22" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                        <wsu:Created>2018-12-12T06:53:00Z</wsu:Created>
                        <wsu:Expires>2018-12-15T18:13:00Z</wsu:Expires>
                     </wsu:Timestamp>
                  </wsse:Security>
               </env:Header>
               <env:Body>
                  <ns0:findContractResponse xmlns:ns0="http://xmlns.oracle.com/apps/contracts/coreAuthoring/contractService/types/">
                     <ns2:result xsi:type="ns1:ContractHeaderResult" xmlns:ns2="http://xmlns.oracle.com/apps/contracts/coreAuthoring/contractService/types/" xmlns:ns1="http://xmlns.oracle.com/apps/contracts/coreAuthoring/contractService/" xmlns:tns="http://xmlns.oracle.com/adf/svc/errors/" xmlns:ns0="http://xmlns.oracle.com/adf/svc/types/" xmlns:ns5="http://xmlns.oracle.com/apps/projects/billing/contracts/contractPublicService/" xmlns:ns6="http://xmlns.oracle.com/apps/projects/billing/contracts/contractBillingService/" xmlns:ns7="http://xmlns.oracle.com/apps/contracts/coreAuthoring/header/flex/header/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                        <ns1:Value>
                           <ns1:Id>100000011169350</ns1:Id>
                        </ns1:Value>
                        <ns1:Value>
                           <ns1:Id>100000011169350</ns1:Id>
                        </ns1:Value>
                     </ns2:result>
                  </ns0:findContractResponse>
               </env:Body>
            </env:Envelope>"""

伙计们,我被困在这里,我尝试使用elementree的所有选项,但注意会有所帮助。具有讽刺意味的是它以前曾经工作过,但我不知道为什么现在不工作

0 个答案:

没有答案