<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    I can't use getroot() when using fromstring() -- as fromstring()
    returns an Element, not an ElementTree object.<br>
    <br>
    Yes, my root is the 'components' element, but find() seems to insist
    on searching for sub-elements.<br>
    <br>
    Ideally, I would like root.find('components') or
    root.find('./components') to find the components element at the
    top-level.<br>
    <br>
    I'd also like to be able to do root.find('./components/component')
    or root.find('./components/component/name') and so on.<br>
    <br>
    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 :)<br>
    <br>
    Cheers, Brendan.<br>
    <br>
    <br>
    On 31/08/10 6:57 PM, Nitin Pawar wrote:
    <blockquote type="cite">Try using <tt class="descname">getroot</tt><big>(</big><big>)</big>
      <div>
        <span class="Apple-style-span" style="font-size: 16px;"><br>
        </span></div>
      <div><span class="Apple-style-span" style="font-size: 16px;">I
          think your root is components so its searching in root <br>
        </span><br>
        <div class="gmail_quote">
          On Tue, Aug 31, 2010 at 2:19 PM, Brendan Simon (eTRIX) <span
            dir="ltr"><<a href="mailto:brendan.simon@etrix.com.au">brendan.simon@etrix.com.au</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
            0.8ex; border-left: 1px solid rgb(204, 204, 204);
            padding-left: 1ex;">
            <div text="#000000" bgcolor="#ffffff"> 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.<br>
              <br>
              How do I find top level elements ??<br>
              Here is my code.<br>
              <br>
              <blockquote><tt>import xml.etree.ElementTree as ET<br>
                  <br>
                  xml = '''\<br>
                  <?xml version="1.0" encoding="Windows-1252" ?><br>
                  <components><br>
                    <component><br>
                      <name>Fred</name><br>
                      <location>Australia</location><br>
                    </component><br>
                  </components><br>
                  '''<br>
                  <br>
                  root = ET.fromstring( xml )<br>
                  <br>
                  ### This pattern is not found :(<br>
                  comps = root.find( './/components' )<br>
                  <br>
                </tt><tt>### These patterns are found ok :)</tt><br>
                <tt>comp = root.find( './/component' )<br>
                </tt><tt>name = root.find( './/name' )<br>
                </tt><br>
                <tt>print 'comps =', comps<br>
                  print 'comp =', comp<br>
                </tt><tt>print 'name =', name</tt><br>
              </blockquote>
              <br>
              Thanks, Brendan.<br>
              <br>
            </div>
            <br>
            --<br>
            <a
              href="http://mail.python.org/mailman/listinfo/python-list"
              target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
            <br>
          </blockquote>
        </div>
        <br>
        <br clear="all">
        <br>
        -- <br>
        Nitin Pawar<br>
        <br>
      </div>
    </blockquote>
    <br>
  </body>
</html>