[英]Parsing XML string with python
我有一个从wsdl Web服务获取的XML字符串。 我试图用elementTree解析它,但是它仅用于文件。 我试图将其保存在文件中以进行解析,但是它说我没有打开文件的权限。 因此,我使用了在stackoverflow中找到的以下解决方案:
try:
queryresult = client.service.executeQuery(query)
except WebFault, e:
print e
tree = ET.ElementTree(ET.fromstring(queryresult))
rootElem = tree.getroot()
result = rootElem.findall(".//result")
但是当我打印结果时,我在0x7f6e454bdb90处采用了像“结果”这样的值
我也尝试打印
result.text
要么
for s in result:
test = s.attrib
并且在result = rootElem.findall(“ .// result”)中,我还尝试了result = rootElem.findall(“ result”)或result = rootElem.findall(“ results / result”)
这是xml字符串(我只添加了一部分,因为它太大了):
<?xml version="1.0" encoding="UTF-8"?> <sparql> <head> <variable name="id"/> <variable name="code"/> <variable name="title"/> <variable name="text"/> <variable name="substanceCode"/> <variable name="patientId"/> <variable name="birthTime"/> <variable name="effectiveTime_start"/> </head> <results> <result> <binding name="id"> <literal>df868fff-9d48-11e2-8ee8-833b8491ffe6</literal> </binding> <binding name="code"> <literal>432102000</literal> </binding> <binding name="title"> <literal>Administration of substance (procedure)</literal> </binding> <binding name="text"> <literal>Aclarubicin (product)</literal> </binding> <binding name="substanceCode"> <literal>326830005</literal> </binding> <binding name="patientId"> <literal>fictitious1</literal> </binding> <binding name="birthTime"> <literal>1965-03-01T00:00:00.0</literal> </binding> </result>
我也尝试了@alecxe解决方案:
def index(request,requestresult):
data = requestresult
tree = ET.fromstring(data)
for literal in tree.findall('.//result/binding/literal')
returnresult = literal.text
if tokens_p(request):
account_id = urllib.unquote(request.session['oauth_token_set']['account_id'])
return utils.render_template('ui/index', { 'ACCOUNT_ID': account_id, 'SETTINGS': settings,'returnresult':returnresult})
return HttpResponseRedirect(reverse(login))
然后,我在index.html中打印了结果{{returnresult}},但没有打印内容。 然后我尝试了:
tree = ET.ElementTree(ET.fromstring(data))
rootElem = tree.getroot()
returnresult = rootElem.findall('.//results/result/binding/literal')
并在0x7f6e4529fc10处打印元素'literal'在0x7f6e4529fc90等处打印元素'literal'等。当我尝试使用以下命令打印时:
for literal in rootElem.findall('.//results/result/binding/literal')
returnresult = literal.text
我在行中有一个错误无效的语法:“对于rootElem.findall('.// results / result / binding / literal')中的文字”
根据您的评论,您想要获取result
里面的binding
标签内的literal
标签的值。 在这种情况下,您应该使用.//result/binding/literal
xpath表达式:
import xml.etree.ElementTree as ET
data = """<?xml version="1.0" encoding="UTF-8"?>
<sparql>
<head>
<variable name="id"/>
<variable name="code"/>
<variable name="title"/>
<variable name="text"/>
<variable name="substanceCode"/>
<variable name="patientId"/>
<variable name="birthTime"/>
<variable name="effectiveTime_start"/>
</head>
<results>
<result>
<binding name="id">
<literal>df868fff-9d48-11e2-8ee8-833b8491ffe6</literal>
</binding>
<binding name="code">
<literal>432102000</literal>
</binding>
</result>
</results>
</sparql>"""
tree = ET.fromstring(data)
print [literal.text for literal in tree.findall('.//result/binding/literal')]
印刷品:
['df868fff-9d48-11e2-8ee8-833b8491ffe6', '432102000']
注意我已经减少了xml。
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.