用python解析XML字符串

2024-10-03 23:29:41 发布

您现在位置: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”的值

我也试着打印

^{pr2}$

或者

 for s in result:
    test =  s.attrib

结果=罗特莱姆·芬德尔(“//result”)我也尝试了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)) 

然后我将结果{returnresult}}打印到索引.html但什么都没有印刷。 然后我试着:

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

我在“for literal in”行中有一个错误无效语法罗特莱姆·芬德尔('.//results/result/binding/literal')“


Tags: textnameinidtreeforresultvariable
1条回答
网友
1楼 · 发布于 2024-10-03 23:29:41

{{{cd2>的值是你想要的。在这种情况下,您应该使用.//result/binding/literalxpath表达式:

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')]

印刷品:

^{pr2}$

注意,我已经减少了一点xml。在

希望有帮助。在

相关问题 更多 >