### Functions ¶ xml.etree.ElementTree.indent( _tree_, _space=''_, _level=0_) ¶ Appends whitespace to the subtree to indent the tree visually. This can be used to generate pretty-printed XML output. _tree_ can be an Element or ElementTree. _space_ is the whitespace string that will be inserted for each indentation level, two space characters by default. For indenting partial subtrees inside of an already indented tree, pass the initial indentation level as _level_. Added in version 3.9. ### ElementTree Objects ¶ _class_ xml.etree.ElementTree.ElementTree( _element=None_, _file=None_) ¶ ElementTree wrapper class. This class represents an entire element hierarchy, and adds some extra support for serialization to and from standard XML. _element_ is the root element. The tree is initialized with the contents of the XML _file_ if given. \_setroot( _element_) ¶ Replaces the root element for this tree. This discards the current contents of the tree, and replaces it with the given element. Use with care. _element_ is an element instance. find( _match_, _namespaces=None_) ¶ Same as `Element.find()`, starting at the root of the tree. findall( _match_, _namespaces=None_) ¶ Same as `Element.findall()`, starting at the root of the tree. findtext( _match_, _default=None_, _namespaces=None_) ¶ Same as `Element.findtext()`, starting at the root of the tree. getroot() ¶ Returns the root element for this tree. iter( _tag=None_) ¶ Creates and returns a tree iterator for the root element. The iterator loops over all elements in this tree, in section order. _tag_ is the tag to look for (default is to return all elements). iterfind( _match_, _namespaces=None_) ¶ Same as `Element.iterfind()`, starting at the root of the tree. Added in version 3.2. parse( _source_, _parser=None_) ¶ Loads an external XML section into this element tree. _source_ is a file name or file object. _parser_ is an optional parser instance. If not given, the standard `XMLParser` parser is used. Returns the section root element. write( _file_, _encoding='us-ascii'_, _xml\_declaration=None_, _default\_namespace=None_, _method='xml'_, _\*_, _short\_empty\_elements=True_) ¶ Writes the element tree to a file, as XML. _file_ is a file name, or a file object opened for writing. _encoding_ [\[1\]](https://docs.python.org/3/library/xml.etree.elementtree.html#id9) is the output encoding (default is US-ASCII). _xml\_declaration_ controls if an XML declaration should be added to the file. Use `False` for never, `True` for always, `None` for only if not US-ASCII or UTF-8 or Unicode (default is `None`). _default\_namespace_ sets the default XML namespace (for “xmlns”). _method_ is either `"xml"`, `"html"` or `"text"` (default is `"xml"`). The keyword-only _short\_empty\_elements_ parameter controls the formatting of elements that contain no content. If `True` (the default), they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags. The output is either a string ( `str`) or binary ( `bytes`). This is controlled by the _encoding_ argument. If _encoding_ is `"unicode"`, the output is a string; otherwise, it’s binary. Note that this may conflict with the type of _file_ if it’s an open file object; make sure you do not try to write a string to a binary stream and vice versa. Changed in version 3.4: Added the _short\_empty\_elements_ parameter. Changed in version 3.8: The `write()` method now preserves the attribute order specified by the user. This is the XML file that is going to be manipulated: Copy ``` <html> <head> <title>Example page</title> </head> <body> <p>Moved to <a href="http://example.org/">example.org</a> or <a href="http://example.com/">example.com</a>.</p> </body> </html> ``` Example of changing the attribute “target” of every link in first paragraph: Copy ``` >>> 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") ```