首页 > 解决方案 > 这个 XML 可以解析吗?

问题描述

我正在尝试解析 XML 响应,但没有运气。

我正在使用 python 请求库连接到返回 XML 的 API。

从 response.content 我得到:

{"GetQuestions":"<Questions><Question><QuestionId>393938<\/QuestionId><QuestionText>Please respond to the following statement:\"The assigned task was easy to complete\"<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393939<\/QuestionId><QuestionText>Did you save your  datafor later? Why\/why not?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>1<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393940<\/QuestionId><QuestionText>Did you notice how much it cost to find the item? How much was it?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393941<\/QuestionId><QuestionText>Did you select ‘signature on form’? Why\/why not?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>1<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393942<\/QuestionId><QuestionText>Was it easy to find thethe new page? Why\/why not?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>1<\/QuestionStatus><ExtendedType>4<\/ExtendedType><\/Question><Question><QuestionId>393943<\/QuestionId><QuestionText>Please enter your email. So that we can track your responses, we need you to provide this for each task.<\/QuestionText><QuestionShortCode>email<\/QuestionShortCode><QuestionType>text<\/QuestionType><QuestionStatus>1<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393944<\/QuestionId><QuestionText>Why didn't you save your  datafor later?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393945<\/QuestionId><QuestionText>Why did you save your  datafor later?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>4<\/ExtendedType><\/Question><Question><QuestionId>393946<\/QuestionId><QuestionText>Did you save your  datafor later?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393947<\/QuestionId><QuestionText>Why didn't you select 'signature on form'?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393948<\/QuestionId><QuestionText>Why did you select 'signature on form'?<\/QuestionText><QuestionType>text<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>4444449<\/QuestionId><QuestionText>Did you select ‘signature on form’?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393950<\/QuestionId><QuestionText>Why wasn't it easy to find thethe new page?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>4<\/ExtendedType><\/Question><Question><QuestionId>393951<\/QuestionId><QuestionText>Was it easy to find thethe new page?<\/QuestionText><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>0<\/ExtendedType><\/Question><Question><QuestionId>393952<\/QuestionId><QuestionText>Please enter your email addressSo that we can track your responses, we need you to provide this for each task<\/QuestionText><QuestionShortCode>email<\/QuestionShortCode><QuestionType>single<\/QuestionType><QuestionStatus>0<\/QuestionStatus><ExtendedType>4<\/ExtendedType><\/Question><\/Questions>"}

如果我直接将其传递给 ElementTree :

ElementTree.fromstring(response.content)

它返回:

xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0

我从一开始就删除:{“GetQuestions”:

我从最后删除:“}

仍然返回xml.etree.ElementTree.ParseError。

我的方法有问题还是 XML 有问题?

任何意见,将不胜感激。

标签: pythonxmlpython-requestsxml-parsing

解决方案


使用该 JSON 字符串response

xml.etree.ElementTree.fromstring(response.json()['GetQuestions'])

推荐阅读