PyDocs

Nav

Structured Markup Processing Tools

ElementTree.find

  • ElementTree.find(match, namespaces=None)
Find first matching element by tag name or path.

Same as getroot().find(path), which is Element.find()

*path* is a string having either an element tag or an XPath,
*namespaces* is an optional mapping from namespace prefix to full
name.

Return the first matching element, or None if no element was found.
1 Example
>>> from xml.etree.ElementTree import ElementTree
>>> tree = ElementTree()
>>> tree.parse("index.xhtml")
<Element 'html' at 0xb77e6fac>
>>> p = tree.find("body/p")     # Finds first occurrence of tag p in body
>>> p
<Element 'p' at 0xb77ec26c>
>>> links = list(p.iter("a"))   # Returns list of all links
>>> links
[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
>>> for i in links:             # Iterates through all found links
...     i.attrib["target"] = "blank"
...
>>> tree.write("output.xhtml")
from the Python docs
See Also
No see-alsos for xml.etree.ElementTree/ElementTree.find
0 Notes
No notes for ElementTree.find