Parsing XML string with python
Question: I have an XML string that I take from a wsdl web service. I tried to parse it
with elementTree but it is only for files. I tried to save it in a file in
order to parse it but it says I don't have permission to open the file. So I
used this solution that I found in 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")
    

but when I print the result I take a value like Element 'result' at
0x7f6e454bdb90

I also tried to print

    
    
    result.text
    

or

    
    
     for s in result:
        test =  s.attrib
    

and in result = rootElem.findall(".//result") i also tried result =
rootElem.findall("result") or result = rootElem.findall("results/result")

This is the xml string (I added only a part of it because it is too big):

    
    
     <?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>
    

I also tried @alecxe solution:

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

And then I printed the results {{returnresult}} in index.html but nothing is
printing. Then I tried:

    
    
    tree =  ET.ElementTree(ET.fromstring(data))
    rootElem = tree.getroot()
    returnresult = rootElem.findall('.//results/result/binding/literal')
    

and it prints Element 'literal' at 0x7f6e4529fc10 Element 'literal' at
0x7f6e4529fc90 etc. And when I try to print it with:

    
    
     for literal in rootElem.findall('.//results/result/binding/literal')
         returnresult = literal.text
    

I have an error invalid syntax in line: "for literal in
rootElem.findall('.//results/result/binding/literal')"


Answer: According to your comment, you want to get the value of `literal` tag inside
`binding` tag that is inside `result`. In this case you should use
`.//result/binding/literal` xpath expression:

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

prints:

    
    
    ['df868fff-9d48-11e2-8ee8-833b8491ffe6', '432102000']
    

Note I've cut down xml a bit.

Hope that helps.

