find() method in ElementTree

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mirandacascade@yahoo.com

    #1

    find() method in ElementTree

    I do not understand how to use the find() method in ElementTree.

    The file 'sample.xml' is:

    <?xml version="1.0"?>
    <SampleRoot>
    <Header>
    <Product>FindMy stery</Product>
    </Header>
    <SpecificInform ation>
    <SampleDetail>a bc</SampleDetail>
    </SpecificInforma tion>
    </SampleRoot>
    [color=blue][color=green][color=darkred]
    >>> from elementtree.Ele mentTree import ElementTree
    >>> doc = ElementTree(fil e='sample.xml')
    >>> iterList = doc.getiterator ()
    >>> iterList[/color][/color][/color]
    [<Element SampleRoot at 1166850>, <Element Header at 1166878>, <Element
    Product at 11668a0>, <Element SpecificInforma tion at 1166940>, <Element
    SampleDetail at 1166990>][color=blue][color=green][color=darkred]
    >>> len(iterList)[/color][/color][/color]
    5[color=blue][color=green][color=darkred]
    >>> element = iterList[4]
    >>> element.tag[/color][/color][/color]
    'SampleDetail'[color=blue][color=green][color=darkred]
    >>> x = doc.find('Sampl eDetail')
    >>> if x == None:[/color][/color][/color]
    .... print 'x is none'
    ....
    x is none[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    The ElementTree documentation indicates that:
    "find(patte rn) returns the first subelement that matches the given
    pattern, or None if there is no matching element."

    and

    "the pattern argument can either be a tag name, or a path expression"

    Based on the following snippet from the interactive window:
    [color=blue][color=green][color=darkred]
    >>> doc = ElementTree(fil e='sample.xml')
    >>> iterList = doc.getiterator ()
    >>> element = iterList[4]
    >>> element.tag[/color][/color][/color]
    'SampleDetail'

    I inferred (perhaps incorrectly) that within doc there is a subelement
    with a tag 'SampleDetail'.

    Based on the following snippet:
    [color=blue][color=green][color=darkred]
    >>> x = doc.find('Sampl eDetail')
    >>> if x == None:[/color][/color][/color]
    .... print 'x is none'
    ....
    x is none

    I conclude that there is no subelement in doc with a tag
    'SampleDetails' .

    My questions:
    1) in the example above is there a subelement of doc with a tag
    'SampleDetails' ?
    2) if so, what is the proper way of writing the call to the find()
    method to locate that subelement?

  • Fredrik Lundh

    #2
    Re: find() method in ElementTree

    mirandacascade@ yahoo.com wrote:
    [color=blue]
    > My questions:
    > 1) in the example above is there a subelement of doc with a tag
    > 'SampleDetails' ?[/color]

    find only searches for direct subelements; SampleDetail is not a direct
    subelement to SampleRoot, since there's a SpecificInforma tion element
    in between.
    [color=blue]
    > 2) if so, what is the proper way of writing the call to the find()
    > method to locate that subelement?[/color]

    elem = doc.find(".//SampleDetail")

    should work.

    </F>



    Comment

    Working...