* The xml.etree.ElementTree Module This module provides implementations of the Element and ElementTree types, plus support classes. A C version of this API is available as xml.etree.cElementTree. * Overview The Element type is a flexible container object, designed to store hierarchical data structures in memory. The type can be described as a cross between a list and a dictionary. Each element has a number of properties associated with it: * a tag. This is a string identifying what kind of data this element represents (the element type, in other words). * a number of attributes, stored in a Python dictionary. * a text string. * an optional tail string. * a number of child elements, stored in a Python sequence To create an element instance, use the Element or SubElement factory functions. The ElementTree class can be used to wrap an element structure, and convert it from and to XML. * Functions * Comment(text=None) Comment element factory. This factory function creates a special element that will be serialized as an XML comment. The comment string can be either an 8-bit ASCII string or a Unicode string. Arguments: text: A string containing the comment string. Returns: An element instance, representing a comment. * dump(elem) Writes an element tree or element structure to sys.stdout. This function should be used for debugging only. The exact output format is implementation dependent. In this version, it's written as an ordinary XML file. Arguments: elem: An element tree or an individual element. * Element(tag, attrib={}, **extra) Element factory. This function returns an object implementing the standard Element interface. The exact class or type of that object is implementation dependent, but it will always be compatible with the _ElementInterface class in this module. The element name, attribute names, and attribute values can be either 8-bit ASCII strings or Unicode strings. Arguments: tag: The element name. attrib: An optional dictionary, containing element attributes. **extra: Additional attributes, given as keyword arguments. Returns: An element instance. * fromstring(text) Parses an XML document from a string constant. Same as XML. Arguments: source: A string containing XML data. Returns: An Element instance. * iselement(element) Checks if an object appears to be a valid element object. Arguments: An: element instance. Returns: A true value if this is an element object. * iterparse(source, events=None) Parses an XML document into an element tree incrementally, and reports what's going on to the user. Arguments: source: A filename or file object containing XML data. events: A list of events to report back. If omitted, only "end" events are reported. Returns: A (event, elem) iterator. * parse(source, parser=None) Parses an XML document into an element tree. Arguments: source: A filename or file object containing XML data. parser: An optional parser instance. If not given, the standard XMLTreeBuilder parser is used. Returns: An ElementTree instance * ProcessingInstruction(target, text=None) PI element factory. This factory function creates a special element that will be serialized as an XML processing instruction. Arguments: target: A string containing the PI target. text: A string containing the PI contents, if any. Returns: An element instance, representing a PI. * SubElement(parent, tag, attrib={}, **extra) Subelement factory. This function creates an element instance, and appends it to an existing element. The element name, attribute names, and attribute values can be either 8-bit ASCII strings or Unicode strings. Arguments: parent: The parent element. tag: The subelement name. attrib: An optional dictionary, containing element attributes. **extra: Additional attributes, given as keyword arguments. Returns: An element instance. * tostring(element, encoding=None) Generates a string representation of an XML element, including all subelements. Arguments: element: An Element instance. Returns: An encoded string containing the XML data. * XML(text) Parses an XML document from a string constant. This function can be used to embed "XML literals" in Python code. Arguments: source: A string containing XML data. Returns: An Element instance. * XMLID(text) Parses an XML document from a string constant, and also returns a dictionary which maps from element id:s to elements. Arguments: source: A string containing XML data. Returns: A tuple containing an Element instance and a dictionary. * ElementTree Objects * class 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. Arguments: element: Optional root element. file (keyword): Optional file handle or name. If given, the tree is initialized with the contents of this XML file. * _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. Arguments: element: An element instance. * find(path) Finds the first toplevel element with given tag. Same as getroot().find(path). Arguments: path: What element to look for. Returns: The first matching element, or None if no element was found. * findall(path) Finds all toplevel elements with the given tag. Same as getroot().findall(path). Arguments: path: What element to look for. Returns: A list or iterator containing all matching elements, in document order. * findtext(path, default=None) Finds the element text for the first toplevel element with given tag. Same as getroot().findtext(path). Arguments: path: What toplevel element to look for. default: What to return if the element was not found. Returns: The text content of the first matching element, or the default value no element was found. Note that if the element has is found, but has no text content, this method returns an empty string. * getiterator(tag=None) Creates a tree iterator for the root element. The iterator loops over all elements in this tree, in document order. Arguments: tag: What tags to look for (default is to return all elements) Returns: An iterator. * getroot() Gets the root element for this tree. Returns: An element instance. * parse(source, parser=None) Loads an external XML document into this element tree. Arguments: source: A file name or file object. parser: An optional parser instance. If not given, the standard XMLTreeBuilder parser is used. Returns: The document root element. * write(file, encoding="us-ascii") Writes the element tree to a file, as XML. Arguments: file: A file name, or a file object opened for writing. encoding: Optional output encoding (default is US-ASCII). * QName Objects * class QName(text_or_uri, tag=None) QName wrapper. This can be used to wrap a QName attribute value, in order to get proper namespace handling on output. Arguments: text: A string containing the QName value, in the form {uri}local, or, if the tag argument is given, the URI part of a QName. tag: Optional tag. If given, the first argument is interpreted as an URI, and this argument is interpreted as a local name. Returns: An opaque object, representing the QName. * TreeBuilder Objects * class TreeBuilder(element_factory=None) Generic element structure builder. This builder converts a sequence of start, data, and end method calls to a well-formed element structure. You can use this class to build an element structure using a custom XML parser, or a parser for some other XML-like format. Arguments: element_factory: Optional element factory. This factory is called to create new Element instances, as necessary. * close() Flushes the parser buffers, and returns the toplevel documen element. Returns: An Element instance. * data(data) Adds text to the current element. Arguments: data: A string. This should be either an 8-bit string containing ASCII text, or a Unicode string. * end(tag) Closes the current element. Arguments: tag: The element name. Returns: The closed element. * start(tag, attrs) Opens a new element. Arguments: tag: The element name. attrib: A dictionary containing element attributes. Returns: The opened element. * XMLTreeBuilder Objects * class XMLTreeBuilder(html=0, target=None) Element structure builder for XML source data, based on the expat parser. Arguments: target (keyword): Target object. If omitted, the builder uses an instance of the standard TreeBuilder class. html (keyword): Predefine HTML entities. This flag is not supported by the current implementation. * close() Finishes feeding data to the parser. Returns: An element structure. * doctype(name, pubid, system) Handles a doctype declaration. Arguments: name: Doctype name. pubid: Public identifier. system: System identifier. * feed(data) Feeds data to the parser. Arguments: data: Encoded data.