0

I am using a Python script to receive an XML response from a SOAP web service, and I'd like to extract specific values from the XML response. I'm trying to use the 'untangle' library, but keep getting the following error:

AttributeError: 'None' has no attribute 'Envelope'

Below is a sample of my code. I'm trying to extract the RequestType value from the below

<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header/>
    <soap:Body>
        <Response>\n  
            <RequestType>test</RequestType> 
        </Response>
    </soap:Body>
</soap:Envelope>

Sample use of untangle

parsed_xml = untangle.parse(xml)
print(parsed_xml.Envelope.Response.RequestType.cdata)

I've also tried parsed_xml.Envelope.Body.Response.RequestType.cdata

1
  • I have not used untangle, but it is unclear how it handles XML namespaces (no information here: untangle.readthedocs.io/en/latest). Commented May 17, 2021 at 7:25

2 Answers 2

3

This will solve your problem, assuming you want to extract 'test'. By the way, i think your response should not have 'soap:Header/':

import xmltodict

stack_d = xmltodict.parse(response.content)
stack_d['soap:Envelope']['soap:Body']['Response']['RequestType']
Sign up to request clarification or add additional context in comments.

Comments

0

I think you will find the xml.etree library to be more usable in this context.

import requests
from xml.etree import ElementTree

Then we need to define the namespaces for the SOAP Response

namespaces = {
    'soap': 'http://schemas.xmlsoap.org/soap/envelope/',
    'a': 'http://www.etis.fskab.se/v1.0/ETISws',
}

dom = Element.tree.fromstring(response.context)

Then simply find all the DOMs

names = dom.findall('./soap:Body',namespaces)

2 Comments

Thanks for this, but the code doesn't seem to work. It has an issue with 'Element' in your dom = Element.tree.fromstring command: NameError: name 'Element' is not defined
1) Neither requests nor the http://www.etis.fskab.se/v1.0/ETISws namespace is used in the question. Referring to them is just confusing. 2) "find all the DOMs" is a strange phrase. findall does not find "DOMs", it finds elements. Please put more effort into the answer.

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.