Can't find elements using ElementTree find method

Nitin Pawar nitinpawar432 at gmail.com
Tue Aug 31 04:57:21 EDT 2010


Try using getroot()<http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.getroot>

I think your root is components so its searching in root

On Tue, Aug 31, 2010 at 2:19 PM, Brendan Simon (eTRIX) <
brendan.simon at etrix.com.au> wrote:

>  I am trying to use ElementTree (with Python 2.7) and can't seem to find
> elements at the top level.  The find() and findall() methods seem to find
> elements within the top level, but not if it the elements are at the top
> level.
>
> How do I find top level elements ??
> Here is my code.
>
> import xml.etree.ElementTree as ET
>
> xml = '''\
> <?xml version="1.0" encoding="Windows-1252" ?>
> <components>
>   <component>
>     <name>Fred</name>
>     <location>Australia</location>
>   </component>
> </components>
> '''
>
> root = ET.fromstring( xml )
>
> ### This pattern is not found :(
> comps = root.find( './/components' )
>
> ### These patterns are found ok :)
> comp = root.find( './/component' )
> name = root.find( './/name' )
>
> print 'comps =', comps
> print 'comp =', comp
> print 'name =', name
>
>
> Thanks, Brendan.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
Nitin Pawar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100831/325c3899/attachment-0001.html>


More information about the Python-list mailing list