Get specifics elements of a xml output in python
Question: I'm getting some problems to get this part of the code, that I get from
[yourself](http://stackoverflow.com/questions/1140672/parsing-
xml/1140753#1140753). Here is my code:

    
    
    import cStringIO
    import pycurl
    from xml.etree import ElementTree
    
    _API_KEY = 'my api key'
    _ima = '/the/path/to/a/image'
    
    sock = cStringIO.StringIO()
    
    upl = pycurl.Curl()
    
    values = [
                ("key", _API_KEY),
                ("image", (upl.FORM_FILE, _ima))]
    
    upl.setopt(upl.URL, "http://api.imgur.com/2/upload.xml")
    upl.setopt(upl.HTTPPOST, values)
    upl.setopt(upl.WRITEFUNCTION, sock.write)
    upl.perform()
    upl.close()
    xmldata = sock.getvalue()
    #print xmldata
    sock.close()
    tree = ElementTree.fromstring(xmldata)
    url = tree.findtext('original')
    webpage = tree.findtext('imgur_page')
    delpage = tree.findtext('delete_page')
    
    
    print 'Url: ' + str(url)
    print 'Pagina: ' + str(webpage)
    print 'Link de borrado: ' + str(delpage)
    

If I uncomment the test print, I get a output similar to this:

    
    
    <?xml version="1.0" encoding="utf-8"?>
    <upload><image><name></name><title></title><caption></caption><hash>dxPGi</hash><deletehash>kj2XOt4DC13juUW</deletehash><datetime>2011-06-10 02:59:26</datetime><type>image/png</type><animated>false</animated><width>1024</width><height>768</height><size>172863</size><views>0</views><bandwidth>0</bandwidth></image><links><original>http://i.stack.imgur.com/dxPGi.png</original><imgur_page>http://imgur.com/dxPGi</imgur_page><delete_page>http://imgur.com/delete/kj2XOt4DC13juUW</delete_page><small_square>http://i.stack.imgur.com/dxPGis.jpg</small_square><large_thumbnail>http://i.stack.imgur.com/dxPGil.jpg</large_thumbnail></links></upload>
    

I want the receive some specific value, but it throw me this error:

    
    
    Traceback (most recent call last):
      File "<pyshell#28>", line 27, in <module>
        url = tree.find('original').text
    AttributeError: 'NoneType' object has no attribute 'text'
    

I searched in the python module help about ElementTree, and it don't have this
attribute, so how can I get only the text, not the object.

Update:

I found some info about getting a text string
[here](http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.findtext)
but still no results. Now I get:

    
    
    Traceback (most recent call last):   File "<pyshell#32>", line 34, in <module>
        print 'Url: ' + url TypeError: cannot concatenate 'str' and 'NoneType' objects
    

Update 2:

I added the str() to the print and now I get a nice string. The bad part is,
that now I get None as response. So question still apply. How I get the url,
webpage and delete_page of this xml?


Answer: Your `find()` call is trying to find an immediate child of the top of the tree
with a tag named `original`, not a tag at any lower level than that. Use:

    
    
    url = tree.find('.//original').text
    

if you want to find all elements in the tree with the tag named `original`.
The pattern matching rules for ElementTree's `find()` method are laid out in a
table on this page: <http://effbot.org/zone/element-xpath.htm>

For `//` matching it says:

> Selects all subelements, on all levels beneath the current element (search
> the entire subtree). For example, “.//egg” selects all “egg” elements in the
> entire tree.

Edit: here is some test code for you, it use the XML sample string you posted
I just ran it through XML Tidy in TextMate to make it legible:

    
    
    from xml.etree import ElementTree
    xmldata = '''<?xml version="1.0" encoding="utf-8"?>
    <upload>
        <image>
            <name/>
            <title/>
            <caption/>
            <hash>dxPGi</hash>
            <deletehash>kj2XOt4DC13juUW</deletehash>
            <datetime>2011-06-10 02:59:26</datetime>
            <type>image/png</type>
            <animated>false</animated>
            <width>1024</width>
            <height>768</height>
            <size>172863</size>
            <views>0</views>
            <bandwidth>0</bandwidth>
    </image>
    <links>
        <original>http://i.stack.imgur.com/dxPGi.png</original>
        <imgur_page>http://imgur.com/dxPGi</imgur_page>
        <delete_page>http://imgur.com/delete/kj2XOt4DC13juUW</delete_page>
        <small_square>http://i.stack.imgur.com/dxPGis.jpg</small_square>
        <large_thumbnail>http://i.stack.imgur.com/dxPGil.jpg</large_thumbnail>
    </links>
    </upload>'''
    tree = ElementTree.fromstring(xmldata)
    print tree.find('.//original').text
    

On my machine (OS X running python 2.6.1) that produces:

    
    
    Ian-Cs-MacBook-Pro:tmp ian$ python test.py 
    http://i.stack.imgur.com/dxPGi.png
    

