PyDocs

Nav

Structured Markup Processing Tools

ElementTree.parse

  • ElementTree.parse(source, parser=None)
Load external XML document into element tree.

*source* is a file name or file object, *parser* is an optional
parser instance that defaults to XMLParser.

ParseError is raised if the parser fails to parse the document.

Returns the root element of the given source document.
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.parse
0 Notes
No notes for ElementTree.parse