Invalid XML content received when initiating Zeep client

58 Views Asked by At

When initialising a Zeep client like so

url = 'http://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl'
client = Client(wsdl = url)

I get the error message

XMLSyntaxError: Invalid XML content received (Opening and ending tag mismatch: p line 122 and ul, line 124, column 20)

I have tried solutions mentioned here but have had no success. I have a feeling that I need to change the way the XML parser works by some parameter but I am unsure since I am new to using this library.

1

There are 1 best solutions below

3
Bench Vue On

Overview

enter image description here

#1 Check WSDL

Open Browser to access this URL

https://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl

Should be displayed XML content enter image description here

#2 Get function list

SoapUI Open Source

enter image description here

Paste the WSDL URL in here then press OK

enter image description here

You can get the function list

enter image description here

#3 Test function

Clicked one of function

enter image description here

Then click Request1

enter image description here

Superman for <cara:search>

1978 for <cara:year>

Then press

enter image description here

And Click XML tab in next panel That is result of function call

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetExportWithTitleYearResponse xmlns="http://cara.org/">
         <GetExportWithTitleYearResult><![CDATA[<?xml version="1.0" encoding="utf-8"?><SearchResults><title><name><![CDATA[Superman]]]]>><![CDATA[</name><year><![CDATA[1978]]]]>><![CDATA[</year><url><![CDATA[http://www.imdb.com/find?q=Superman]]]]>><![CDATA[</url><rating><![CDATA[PG]]]]>><![CDATA[</rating><ratingReason><![CDATA[]]]]>><![CDATA[</ratingReason><distributor><![CDATA[Warner Bros. Inc.]]]]>><![CDATA[</distributor></title></SearchResults>]]></GetExportWithTitleYearResult>
      </GetExportWithTitleYearResponse>
   </soap:Body>
</soap:Envelope>

enter image description here

#4 Test zeep

Same function by zeep Save as test-function.py file name

from zeep import Client

# URL of the SOAP service
url = 'https://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl'

client = Client(url)

# Call the GetExportWithTitleYear method
response = client.service.GetExportWithTitleYear(search='Superman', year='1978')

print(response) # Print the XML data

You should be install dependency before run it.

pip install zeep

Run it

python test-function.py

Result

<?xml version="1.0" encoding="utf-8"?><SearchResults><title><name><![CDATA[Superman]]></name><year><![CDATA[1978]]></year><url><![CDATA[http://www.imdb.com/find?q=Superman]]></url><rating><![CDATA[PG]]></rating><ratingReason><![CDATA[]]></ratingReason><distributor><![CDATA[Warner Bros. Inc.]]></distributor></title></SearchResults>

enter image description here

#5 Data convert

Will print XML and extract key/value as JSON format data.

from zeep import Client
import xml.dom.minidom
import xml.etree.ElementTree as ET
import json

# URL of the SOAP service
url = 'https://www.filmratings.com/Filmratings_CARA/WebCaraSearch/Service.asmx?wsdl'

client = Client(url)

# Call the GetExportWithTitleYear method
response = client.service.GetExportWithTitleYear(search='Superman', year='1978')

dom = xml.dom.minidom.parseString(response) # Parse the XML string

pretty_xml = dom.toprettyxml()

print(pretty_xml) # Print the XML data

root = ET.fromstring(response)

data = {}

# Loop through child elements of the root element
for child in root:
    for item in child:
        tag = item.tag
        text = item.text.strip() if item.text else ""
        data[tag] = text

json_data = json.dumps(data, indent=4)

print(json_data) # Print the JSON data

Result

<?xml version="1.0" ?>
<SearchResults>
        <title>
                <name><![CDATA[Superman]]></name>
                <year><![CDATA[1978]]></year>
                <url><![CDATA[http://www.imdb.com/find?q=Superman]]></url>
                <rating><![CDATA[PG]]></rating>
                <ratingReason/>
                <distributor><![CDATA[Warner Bros. Inc.]]></distributor>
        </title>
</SearchResults>

{
    "name": "Superman",
    "year": "1978",
    "url": "http://www.imdb.com/find?q=Superman",
    "rating": "PG",
    "ratingReason": "",
    "distributor": "Warner Bros. Inc."
}

enter image description here