简体   繁体   English

用元素树解析XML

[英]Parsing XML with element tree

I'm trying to parse XML with ElementTree, but I get this error: 我正在尝试使用ElementTree解析XML,但出现此错误:

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

My file.py: 我的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. 当我尝试从天气字符串解析我的xml时。 Anyone know how to solve this kind of problem? 有人知道如何解决这种问题吗?

The weather response is not a string: weather响应不是字符串:

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

but ElementTree will turn it into text. 但是ElementTree会将其转换为文本。 The claimed encoding is UTF16 however: 声明的编码为UTF16,但是:

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

Turn this response into text by explicitly encoding it to UTF-16: 通过将此响应显式编码为UTF-16,将其转换为文本:

>>> weather = weather.encode('utf16')
>>> parseWeather = ET.fromstring(weather)

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. 虽然你不能确定编码文件应该是的,我试图改变XML编码声明为UTF-8和ElementTree的是能够解析它。

weather = client.service.GetWeather('Sao Paulo', 'Brazil')
weather = weather.replace('encoding="utf-16"?', 'encoding="utf-8"?')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM