2

I am getting XML as a response so I want to parse it. I tried many python libraries but not get my desired results. So if you can help, it will be really appreciative.

The following code returns None:

xmlResponse = ET.fromstring(context.response_document)
a = xmlResponse.findall('.//Body')
print(a)

Sample XML Data:

<S:Envelope
       xmlns:S="http://www.w3.org/2003/05/soap-envelope">
       <S:Header>
           <wsa:Action s:mustUnderstand="1"
               xmlns:s="http://www.w3.org/2003/05/soap-envelope"
               xmlns:wsa="http://www.w3.org/2005/08/addressing">urn:ihe:iti:2007:RegistryStoredQueryResponse
           </wsa:Action>
       </S:Header>
       <S:Body>
           <query:AdhocQueryResponse status="urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success"
               xmlns:query="urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0">
               <rim:RegistryObjectList
                   xmlns:rim="u`enter code here`rn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0"/>
               </query:AdhocQueryResponse>
           </S:Body>
       </S:Envelope>

I want to get status from it which is in Body. If you can suggest some changes of some library then please help me. Thanks

1

1 Answer 1

1

Given the following base code:

import xml.etree.ElementTree as ET

root = ET.fromstring(xml)

Let's build on top of it to get your desired output.

Your initial find for .//Body x-path returns NONE because it doesn't exist in your XML response.

Each tag in your XML has a namespace associated with it. More info on xml namespaces can be found here.

Consider the following line with xmlns value (xml-namespace):

<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">

The value of namespace S is set to be http://www.w3.org/2003/05/soap-envelope.

Replacing S in {S}Envelope with value set above will give you the resulting tag to find in your XML:

root.find('{http://www.w3.org/2003/05/soap-envelope}Envelope') #top most node

We would need to do the same for <S:Body>.


To get<S:Body> elements and it's child nodes you can do the following:

body_node = root.find('{http://www.w3.org/2003/05/soap-envelope}Body')

for response_child_node in list(body_node):
  print(response_child_node.tag) #tag of the child node
  print(response_child_node.get('status')) #the status you're looking for

Outputs:

{urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0}AdhocQueryResponse
urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success

Alternatively

You can also directly find all {query}AdhocQueryResponse in your XML using:

response_nodes = root.findall('.//{urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0}AdhocQueryResponse')

for response in response_nodes:
  print(response.get('status'))

Outputs:

urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.