Re: Can't find elements using ElementTree find method

2010-08-31 Thread Brendan Simon (eTRIX)
 I can't use getroot() when using fromstring() -- as fromstring()
returns an Element, not an ElementTree object.

Yes, my root is the 'components' element, but find() seems to insist on
searching for sub-elements.

Ideally, I would like root.find('components') or
root.find('./components') to find the components element at the top-level.

I'd also like to be able to do root.find('./components/component') or
root.find('./components/component/name') and so on.

I guess I just have to check that root.tag == 'components' first, then
search for './component', './component/name' and so on.  It's a bit
ugly, but heaps better than using minidom :)

Cheers, Brendan.


On 31/08/10 6:57 PM, Nitin Pawar wrote:
> Try using getroot()
>
> I think your root is components so its searching in root 
>
> On Tue, Aug 31, 2010 at 2:19 PM, Brendan Simon (eTRIX)
> mailto:brendan.si...@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 = '''\
> 
> 
>   
> Fred
> Australia
>   
> 
> '''
>
> 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
>

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't find elements using ElementTree find method

2010-08-31 Thread Stefan Behnel

Brendan Simon (eTRIX), 31.08.2010 10:49:

  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 = '''\
 
 
   
 Fred
 Australia
   
 
 '''

 root = ET.fromstring( xml )

 ### This pattern is not found :(
 comps = root.find( './/components' )


"." matches the current element, so the path expression looks for all 
"components" nodes *below* the current element.


You can either wrap the root in an ElementTree and search globally (i.e. 
without the leading "."), or you can test the root node yourself.


Stefan

--
http://mail.python.org/mailman/listinfo/python-list


Re: Can't find elements using ElementTree find method

2010-08-31 Thread Nitin Pawar
Try using 
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.si...@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 = '''\
> 
> 
>   
> Fred
> Australia
>   
> 
> '''
>
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list