0

I'm trying to parse XML with ElementTree, but I get this error:

xml.etree.ElementTree.ParseError: encoding specified in XML declaration is incorrect

My file.py:

from suds.client import Client
import xml.etree.ElementTree as ET

url = 'http://www.webservicex.com/globalweather.asmx?WSDL'
client = Client(url)
weather = client.service.GetWeather('Sao Paulo', 'Brazil')
print weather

parseWeather = ET.fromstring(weather) # >>>> Here I got my problem! 

When I try to parse my xml from string weather. Anyone know how to solve this kind of problem?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Luiz.brsp
  • 13
  • 4

2 Answers2

3

The weather response is not a string:

>>> type(weather)
<class 'suds.sax.text.Text'>

but ElementTree will turn it into text. The claimed encoding is UTF16 however:

>>> weather.splitlines()[0]
'<?xml version="1.0" encoding="utf-16"?>'

Turn this response into text by explicitly encoding it to UTF-16:

>>> weather = weather.encode('utf16')
>>> parseWeather = ET.fromstring(weather)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

While you can't be sure of the encoding a file should be, I tried changing the xml encoding declaration to utf-8 and ElementTree was able to parse it.

weather = client.service.GetWeather('Sao Paulo', 'Brazil')
weather = weather.replace('encoding="utf-16"?', 'encoding="utf-8"?')
tim-phillips
  • 997
  • 1
  • 11
  • 22