Search notes:

Python standard library: xml.etree.ElementTree

Module members

C14NWriterTarget xml.etree.ElementTree.C14NWriterTarget class
canonicalize()
collections Module
Comment()
contextlib Module
dump()
Element xml.etree.ElementTree.Element class
ElementPath Module
_Element_Py xml.etree.ElementTree.Element class
ElementTree xml.etree.ElementTree.ElementTree class
_escape_attrib()
_escape_attrib_c14n()
_escape_attrib_html()
_escape_cdata()
_escape_cdata_c14n()
fromstring() Returns an xml.etree.ElementTree.Element object. See also parse().
fromstringlist()
_get_writer()
HTML_EMPTY set object
indent()
io Module
iselement()
iterparse()
_ListDataStream ?
_looks_like_prefix_name()
_namespace_map dict object
_namespaces()
parse() Parses XML stored in a file (filename or filehandle) and returns an xml.etree.ElementTree.ElementTree object. The 2nd (optional) parameter is the parser-object (XMLParser) by default. Returns an xml.etree.ElementTree.Element object. See also fromstring().
ParseError xml.etree.ElementTree.ParseError class
PI()
ProcessingInstruction()
QName xml.etree.ElementTree.QName class
_raise_serialization_error()
re Module
register_namespace()
_serialize dict object
_serialize_html()
_serialize_text()
_serialize_xml()
_set_factories()
SubElement()
sys Module
tostring()
tostringlist()
TreeBuilder xml.etree.ElementTree.TreeBuilder class
VERSION str object
warnings Module
XML()
XMLID()
XMLParser xml.etree.ElementTree.XMLParser class
XMLPullParser xml.etree.ElementTree.XMLPullParser class

xml.etree.ElementTree.ElementTree object

ElementTree represents the tree structure of an XML document.

Membersl

find()
findall()
findtext()
getroot() Returns the tree's root element (an Element object)
iter()
iterfind()
parse()
_root ?
_setroot()
write()
write_c14n()

xml.etree.ElementTree.Element object

Element represents a single node in a tree represented by ElementTree.

Members

append()
attrib dict object containing the element's attributes.
clear()
extend()
find() Returns the first (immediate) child having a given tag and returns its representation as another Element object.
findall()
findtext()
get() The value of the given attribute (or the specified default if the value does not exist).
insert()
items()
iter()
iterfind()
itertext()
keys()
makeelement()
remove()
set()
tag str object
tail The text after the last child of the Element
text The text before the first tag of the Element

Simple snippet

import xml.etree.ElementTree as ET


xml_data = """
<foo>
  <w l="el">one</w>
  <w l="le">two</w>
  <s t="ss">-</s>
  <w l="ll">three</w>
  <c>Z<sub>sss</sub>X<sub>ttt</sub>Y</c>
</foo>
"""

root = ET.fromstring(xml_data) # get root

print(root.tag)# foo

print("Immediate children:")

for immediate_child in root:
  print("  ", immediate_child.tag)
  print("    ", immediate_child.text)

  if immediate_child.tag == 'w':
     print("     l: ", immediate_child.attrib['l'])

  if immediate_child.tag == 'c':
#    ET.dump(immediate_child)

     for i in immediate_child.iter():
         print("       i: ", i)

     for s in immediate_child:
         print("       ", s.text)

     print("    ", s.tail) # Y
Github repository about-python, path: /standard-library/xml/etree/ElementTree/script.py

See also

Example(s) for xml.etree.ElementTree.
standard library

Index