Parse soap response python

Trying to Parse SOAP Response in Python

I’m struggling to find a way to parse the data that I’m getting back from a SOAP response. I’m only familiar with Python (v3.4), but relatively new to it. I’m using suds-jurko to pull the data from a 3rd party SOAP server. The response comes back in the form of «ArrayOfXmlNode». I’ve tried using ElementTree in different ways to parse the data, but I either get no information or I get «TypeError: invalid file: (ArrayOfXmlNode)» errors. Googling how to handle the ArrayOfXMLNode type response has gotten me nowhere. The first part of the SOAP response is:

from suds.client import Client url = 'http://qag.petpoint.com/webservices/AdoptableSearch.asmx?WSDL' client = Client(url) result = client.service.adoptableSearchExtended('nunya', 0, 'A', 'All', 'N') tree = result[0] for node in tree: pet_info = [] pet_info.extend(node) print(pet_info) 

The code above gives me the entire response in «result[0]». Below that I try to create a list from the data, but only get very last node (node being 1 set of information from ID to Photo). Attempts to modify this approach gives me either everything, nothing, or only the last node. So then I tried to make use of ElementTree with simple code to test it out, but only get the «invalid file» errors.

import xml.etree.ElementTree as ET from suds.client import Client url = 'http://qag.petpoint.com/webservices/AdoptableSearch.asmx?WSDL' client = Client(url) result = client.service.adoptableSearchExtended('nunya', 0, 'A', 'All', 'N') pet_info = ET.parse(result) print(pet_info) 
Traceback (most recent call last): File "D:\Python\Eclipse Workspace\KivyTest\src\root\nested\Parse.py", line 11, in pet_info = ET.parse(result) File "D:\Programs\Python34\lib\xml\etree\ElementTree.py", line 1186, in parse tree.parse(source, parser) File "D:\Programs\Python34\lib\xml\etree\ElementTree.py", line 587, in parse source = open(source, "rb") TypeError: invalid file: (ArrayOfXmlNode) < XmlNode[] = (XmlNode)< Hl = (Hl)< Name = "Daisy" SpeciesID = "1" Sex = "Female" PrimaryBreed = "Terrier, Pit Bull" SecondaryBreed = "" SN = "" Age = "42" OnHold = "No" Location = "Dog Adoption" BehaviorResult = "" Photo = "http://sms.petpoint.com/sms/photos/615/40f428de-c015-4334-9101-89c707383817.jpg" >>, 

Источник

Читайте также:  Html кнопка в правый угол

Parsing a SOAP response in Python

I’m trying to parse a SOAP response from a server. I’m 100% new to SOAP and pretty new to communicating using HTTP / HTTPS . I’m using Python 2.7 on Ubuntu 12.04. It looks like SOAP is very much like XML . However, I seem to be unable to parse it as such. I’ve tried to use ElementTree but keep getting errors. From searches I’ve been able to conclude that there may be issues with the SOAP tags. (I could be way off here. let me know if I am.) So, here is an example of the SOAP message I have and what I’m trying to do to parse it (this is an actual server response from Link Point Gateway, in case that’s relevant).

import xml.etree.ElementTree as ET soap_string = 'Wed Jul 25 10:26:40 2012SGS-002303: Invalid credit card number.1FAILED' targetTree = ET.fromstring(soap_string) 
unbound prefix: line 1, column 0 

From another stackoverflow post I’ve concluded that SOAP-ENV:Body may be causing a namespace problem. (I could be wrong.) I’ve done other searches to find a good solution for parsing SOAP but most of them are from 3+ years ago. It seems that suds is pretty highly recommended. I wanted to get «updated» recommendations before I got too far down a path. Can anyone recommend a solid (and easy) way to parse a SOAP response like the one I received above? It would be appreciated if you could provide a simple example to get me started (as I said above, I’m completely new to SOAP ).

Источник

How to parse soap xml with python?

SOAP (Simple Object Access Protocol) is a messaging protocol that is commonly used to send and receive data in the form of XML messages. In Python, there are several libraries available that allow you to parse and manipulate SOAP XML messages. Here are a few methods you can use to parse SOAP XML in Python:

Читайте также:  Получить html код web страницы

Method 1: xml.etree.ElementTree

To parse SOAP XML with Python using xml.etree.ElementTree , follow these steps:

import xml.etree.ElementTree as ET
response = body.find('.//Response')
result = response.find('.//Result')
import xml.etree.ElementTree as ET tree = ET.parse('soap.xml') root = tree.getroot() body = root.find('.//Body') response = body.find('.//Response') result = response.find('.//Result') print(result.text)

Note that you need to replace the namespace and element names with the actual ones in your SOAP XML file.

Method 2: BeautifulSoup

BeautifulSoup is a popular Python library for parsing HTML and XML documents. It provides an easy-to-use interface for navigating and searching the document tree. In this tutorial, we will show you how to parse SOAP XML with Python using BeautifulSoup.

Step 1: Install BeautifulSoup

Before we can use BeautifulSoup, we need to install it. You can install BeautifulSoup using pip:

pip install beautifulsoup4

Step 2: Import the necessary libraries

Once you have installed BeautifulSoup, you can import it along with the other necessary libraries:

from bs4 import BeautifulSoup import requests

We will also use the requests library to download the SOAP XML file.

Step 3: Download the SOAP XML file

In this example, we will download a SOAP XML file from a URL. You can replace the URL with the URL of your own SOAP XML file:

url = "https://www.example.com/soap.xml" response = requests.get(url)

Step 4: Parse the SOAP XML file with BeautifulSoup

Now that we have downloaded the SOAP XML file, we can parse it with BeautifulSoup:

soup = BeautifulSoup(response.content, "xml")

The response.content parameter contains the XML content of the SOAP XML file. The «xml» parameter tells BeautifulSoup to use the XML parser.

Step 5: Extract data from the SOAP XML file

Once we have parsed the SOAP XML file with BeautifulSoup, we can extract data from it using various BeautifulSoup methods. Here is an example:

products = soup.find_all("product") for product in products: name = product["name"] price = product["price"] print(name, price)

This code finds all the «product» elements in the SOAP XML file and loops through them, printing the «name» and «price» attributes of each element.

Method 3: Zeep

Zeep is a Python library that can be used to consume SOAP APIs. Here is how you can use Zeep to parse SOAP XML in Python:

Step 1: Install Zeep

You can install Zeep using pip:

Step 2: Import Zeep

You can import Zeep in your Python code using the following code:

from zeep import Client from zeep.exceptions import Fault

Step 3: Create a Zeep client

You can create a Zeep client by passing the URL of the WSDL file to the Client class:

client = Client('http://www.example.com/soap.wsdl')

Step 4: Call a SOAP method

You can call a SOAP method using the client object. For example, if there is a method called get_data in the SOAP API, you can call it like this:

result = client.service.get_data()

Step 5: Parse the SOAP response

The SOAP response is returned as an object. You can access the values of the response using dot notation. For example, if the response contains a value called name , you can access it like this:

Here is an example of how to use Zeep to parse SOAP XML in Python:

from zeep import Client from zeep.exceptions import Fault client = Client('http://www.example.com/soap.wsdl') try: # Call a SOAP method result = client.service.get_data() # Parse the SOAP response name = result.name age = result.age # Do something with the values print('Name:', name) print('Age:', age) except Fault as e: print(e)

In this example, we create a Zeep client by passing the URL of the WSDL file. We then call a SOAP method called get_data and parse the response to get the values of name and age . We then print these values to the console. If an error occurs, we catch the Fault exception and print the error message.

Method 4: PySimpleSOAP

PySimpleSOAP is a Python library that allows you to easily consume and create SOAP web services. Here’s an example of how to parse SOAP XML with PySimpleSOAP:

from pysimplesoap.client import SoapClient client = SoapClient(wsdl="http://www.webservicex.net/ConvertWeight.asmx?WSDL")
  1. Now, you can call any method on the SOAP service using the client object. For example, to convert pounds to kilograms:
response = client.ConvertWeight(Weight=10, FromUnit="Pounds", ToUnit="Kilograms")
  1. The response object will contain the result of the SOAP call. You can access the data using dot notation:
result = response.ConvertWeightResult
  1. Finally, you can parse the SOAP XML response using the built-in Python xml.etree.ElementTree library:
import xml.etree.ElementTree as ET root = ET.fromstring(result)
value = root.find(".//NewDataSet/Table/Kilograms").text

This will give you the converted weight in kilograms.

That’s it! This is a basic example of how to parse SOAP XML with PySimpleSOAP. You can find more information about the library in the official documentation.

Источник

Parsing soap/XML response in Python

I am trying to parse the below xml using the python. I do not understand which type of xml this is as I never worked on this kind of xml.I just got it from a api response form Microsoft. Now my question is how to parse and get the value of BinarySecurityToken in my python code. I refer this question Parse XML SOAP response with Python But look like this has also some xmlns to get the text .However in my xml I can’t see any nearby xmlns value through I can get the value. Please let me know how to get the value of a specific filed using python from below xml.

http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issuehttp://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous2017-06-12T10:23:01Z2017-06-12T10:28:01Zurn:passport:compacthttps://something.something.something.com2017-06-12T10:23:01Z2017-06-13T10:23:01Zmy token
    pythonxmlparsingsoapelementtree
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this question" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="question" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="1" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this question
asked Jun 14, 2017 at 10:18
Add a comment|

2 Answers 2

Reset to default
7

This declaration is part of the start tag of the root element:

xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"

It means that elements with the wsse prefix (such as BinarySecurityToken ) are in the http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd namespace. The solution is basically the same as in the answer to the linked question. It's just another namespace:

import xml.etree.ElementTree as ET tree = ET.parse('soap.xml') print tree.find('.//BinarySecurityToken').text 
import xml.etree.ElementTree as ET ns = tree = ET.parse('soap.xml') print tree.find('.//wsse:BinarySecurityToken', ns).text 

Источник

Оцените статью