Commit 583302c5 authored by Florent Xicluna's avatar Florent Xicluna

Update some parts of the xml.etree documentation.

parent a0fd4ccd
...@@ -9,9 +9,9 @@ ...@@ -9,9 +9,9 @@
.. versionadded:: 2.5 .. versionadded:: 2.5
The Element type is a flexible container object, designed to store hierarchical The :class:`Element` type is a flexible container object, designed to store
data structures in memory. The type can be described as a cross between a list hierarchical data structures in memory. The type can be described as a cross
and a dictionary. between a list and a dictionary.
Each element has a number of properties associated with it: Each element has a number of properties associated with it:
...@@ -35,8 +35,14 @@ convert it from and to XML. ...@@ -35,8 +35,14 @@ convert it from and to XML.
A C implementation of this API is available as :mod:`xml.etree.cElementTree`. A C implementation of this API is available as :mod:`xml.etree.cElementTree`.
See http://effbot.org/zone/element-index.htm for tutorials and links to other See http://effbot.org/zone/element-index.htm for tutorials and links to other
docs. Fredrik Lundh's page is also the location of the development version of the docs. Fredrik Lundh's page is also the location of the development version of
xml.etree.ElementTree. the xml.etree.ElementTree.
.. versionchanged:: 2.7
The ElementTree API is updated to 1.3. For more information, see
`Introducing ElementTree 1.3
<http://effbot.org/zone/elementtree-13-intro.htm>`_.
.. _elementtree-functions: .. _elementtree-functions:
...@@ -46,17 +52,17 @@ Functions ...@@ -46,17 +52,17 @@ Functions
.. function:: Comment([text]) .. function:: Comment([text])
Comment element factory. This factory function creates a special element that Comment element factory. This factory function creates a special element
will be serialized as an XML comment by the standard serializer. The comment that will be serialized as an XML comment by the standard serializer. The
string can be either an 8-bit ASCII string or a Unicode string. *text* is a comment string can be either a bytestring or a Unicode string. *text* is a
string containing the comment string. Returns an element instance representing string containing the comment string. Returns an element instance
a comment. representing a comment.
.. function:: dump(elem) .. function:: dump(elem)
Writes an element tree or element structure to sys.stdout. This function should Writes an element tree or element structure to sys.stdout. This function
be used for debugging only. should be used for debugging only.
The exact output format is implementation dependent. In this version, it's The exact output format is implementation dependent. In this version, it's
written as an ordinary XML file. written as an ordinary XML file.
...@@ -66,16 +72,16 @@ Functions ...@@ -66,16 +72,16 @@ Functions
.. function:: fromstring(text) .. function:: fromstring(text)
Parses an XML section from a string constant. Same as XML. *text* is a string Parses an XML section from a string constant. Same as XML. *text* is a
containing XML data. Returns an Element instance. string containing XML data. Returns an :class:`Element` instance.
.. function:: fromstringlist(sequence[, parser]) .. function:: fromstringlist(sequence[, parser])
Parses an XML document from a sequence of string fragments. *sequence* is a list Parses an XML document from a sequence of string fragments. *sequence* is a
or other sequence containing XML data fragments. *parser* is an optional parser list or other sequence containing XML data fragments. *parser* is an
instance. If not given, the standard :class:`XMLParser` parser is used. optional parser instance. If not given, the standard :class:`XMLParser`
Returns an Element instance. parser is used. Returns an :class:`Element` instance.
.. versionadded:: 2.7 .. versionadded:: 2.7
...@@ -89,11 +95,11 @@ Functions ...@@ -89,11 +95,11 @@ Functions
.. function:: iterparse(source[, events[, parser]]) .. function:: iterparse(source[, events[, parser]])
Parses an XML section into an element tree incrementally, and reports what's Parses an XML section into an element tree incrementally, and reports what's
going on to the user. *source* is a filename or file object containing XML data. going on to the user. *source* is a filename or file object containing XML
*events* is a list of events to report back. If omitted, only "end" events are data. *events* is a list of events to report back. If omitted, only "end"
reported. *parser* is an optional parser instance. If not given, the standard events are reported. *parser* is an optional parser instance. If not
:class:`XMLParser` parser is used. Returns an :term:`iterator` given, the standard :class:`XMLParser` parser is used. Returns an
providing ``(event, elem)`` pairs. :term:`iterator` providing ``(event, elem)`` pairs.
.. note:: .. note::
...@@ -109,53 +115,56 @@ Functions ...@@ -109,53 +115,56 @@ Functions
.. function:: parse(source[, parser]) .. function:: parse(source[, parser])
Parses an XML section into an element tree. *source* is a filename or file Parses an XML section into an element tree. *source* is a filename or file
object containing XML data. *parser* is an optional parser instance. If not object containing XML data. *parser* is an optional parser instance. If
given, the standard :class:`XMLParser` parser is used. Returns an not given, the standard :class:`XMLParser` parser is used. Returns an
:class:`ElementTree` instance. :class:`ElementTree` instance.
.. function:: ProcessingInstruction(target[, text]) .. function:: ProcessingInstruction(target[, text])
PI element factory. This factory function creates a special element that will PI element factory. This factory function creates a special element that
be serialized as an XML processing instruction. *target* is a string containing will be serialized as an XML processing instruction. *target* is a string
the PI target. *text* is a string containing the PI contents, if given. Returns containing the PI target. *text* is a string containing the PI contents, if
an element instance, representing a processing instruction. given. Returns an element instance, representing a processing instruction.
.. function:: register_namespace(prefix, uri) .. function:: register_namespace(prefix, uri)
Registers a namespace prefix. The registry is global, and any existing mapping Registers a namespace prefix. The registry is global, and any existing
for either the given prefix or the namespace URI will be removed. *prefix* is a mapping for either the given prefix or the namespace URI will be removed.
namespace prefix. *uri* is a namespace uri. Tags and attributes in this namespace *prefix* is a namespace prefix. *uri* is a namespace uri. Tags and
will be serialized with the given prefix, if at all possible. attributes in this namespace will be serialized with the given prefix, if at
all possible.
.. versionadded:: 2.7 .. versionadded:: 2.7
.. function:: SubElement(parent, tag[, attrib[, **extra]]) .. function:: SubElement(parent, tag[, attrib[, **extra]])
Subelement factory. This function creates an element instance, and appends it Subelement factory. This function creates an element instance, and appends
to an existing element. it to an existing element.
The element name, attribute names, and attribute values can be either 8-bit The element name, attribute names, and attribute values can be either
ASCII strings or Unicode strings. *parent* is the parent element. *tag* is the bytestrings or Unicode strings. *parent* is the parent element. *tag* is
subelement name. *attrib* is an optional dictionary, containing element the subelement name. *attrib* is an optional dictionary, containing element
attributes. *extra* contains additional attributes, given as keyword arguments. attributes. *extra* contains additional attributes, given as keyword
Returns an element instance. arguments. Returns an element instance.
.. function:: tostring(element[, encoding]) .. function:: tostring(element[, encoding])
Generates a string representation of an XML element, including all subelements. Generates a string representation of an XML element, including all
*element* is an Element instance. *encoding* is the output encoding (default is subelements. *element* is an :class:`Element` instance. *encoding* is the
US-ASCII). Returns an encoded string containing the XML data. output encoding (default is US-ASCII). Returns an encoded string containing
the XML data.
.. function:: tostringlist(element[, encoding]) .. function:: tostringlist(element[, encoding])
Generates a string representation of an XML element, including all subelements. Generates a string representation of an XML element, including all
*element* is an Element instance. *encoding* is the output encoding (default is subelements. *element* is an :class:`Element` instance. *encoding* is the
US-ASCII). Returns a sequence object containing the XML data. output encoding (default is US-ASCII). Returns a sequence object containing
the XML data.
.. versionadded:: 2.7 .. versionadded:: 2.7
...@@ -163,9 +172,9 @@ Functions ...@@ -163,9 +172,9 @@ Functions
.. function:: XML(text[, parser]) .. function:: XML(text[, parser])
Parses an XML section from a string constant. This function can be used to Parses an XML section from a string constant. This function can be used to
embed "XML literals" in Python code. *text* is a string containing XML data. embed "XML literals" in Python code. *text* is a string containing XML
*parser* is an optional parser instance. If not given, the standard data. *parser* is an optional parser instance. If not given, the standard
:class:`XMLParser` parser is used. Returns an Element instance. :class:`XMLParser` parser is used. Returns an :class:`Element` instance.
.. function:: XMLID(text[, parser]) .. function:: XMLID(text[, parser])
...@@ -173,8 +182,8 @@ Functions ...@@ -173,8 +182,8 @@ Functions
Parses an XML section from a string constant, and also returns a dictionary Parses an XML section from a string constant, and also returns a dictionary
which maps from element id:s to elements. *text* is a string containing XML which maps from element id:s to elements. *text* is a string containing XML
data. *parser* is an optional parser instance. If not given, the standard data. *parser* is an optional parser instance. If not given, the standard
:class:`XMLParser` parser is used. Returns a tuple containing an Element :class:`XMLParser` parser is used. Returns a tuple containing an
instance and a dictionary. :class:`Element` instance and a dictionary.
.. _elementtree-element-objects: .. _elementtree-element-objects:
...@@ -188,42 +197,43 @@ Element Objects ...@@ -188,42 +197,43 @@ Element Objects
Element class. This class defines the Element interface, and provides a Element class. This class defines the Element interface, and provides a
reference implementation of this interface. reference implementation of this interface.
The element name, attribute names, and attribute values can be either 8-bit The element name, attribute names, and attribute values can be either
ASCII strings or Unicode strings. *tag* is the element name. *attrib* is an bytestrings or Unicode strings. *tag* is the element name. *attrib* is
optional dictionary, containing element attributes. *extra* contains additional an optional dictionary, containing element attributes. *extra* contains
attributes, given as keyword arguments. additional attributes, given as keyword arguments.
.. attribute:: tag .. attribute:: tag
A string identifying what kind of data this element represents (the element A string identifying what kind of data this element represents (the
type, in other words). element type, in other words).
.. attribute:: text .. attribute:: text
The *text* attribute can be used to hold additional data associated with the The *text* attribute can be used to hold additional data associated with
element. As the name implies this attribute is usually a string but may be the element. As the name implies this attribute is usually a string but
any application-specific object. If the element is created from an XML file may be any application-specific object. If the element is created from
the attribute will contain any text found between the element tags. an XML file the attribute will contain any text found between the element
tags.
.. attribute:: tail .. attribute:: tail
The *tail* attribute can be used to hold additional data associated with the The *tail* attribute can be used to hold additional data associated with
element. This attribute is usually a string but may be any the element. This attribute is usually a string but may be any
application-specific object. If the element is created from an XML file the application-specific object. If the element is created from an XML file
attribute will contain any text found after the element's end tag and before the attribute will contain any text found after the element's end tag and
the next tag. before the next tag.
.. attribute:: attrib .. attribute:: attrib
A dictionary containing the element's attributes. Note that while the A dictionary containing the element's attributes. Note that while the
*attrib* value is always a real mutable Python dictionary, an ElementTree *attrib* value is always a real mutable Python dictionary, an ElementTree
implementation may choose to use another internal representation, and create implementation may choose to use another internal representation, and
the dictionary only if someone asks for it. To take advantage of such create the dictionary only if someone asks for it. To take advantage of
implementations, use the dictionary methods below whenever possible. such implementations, use the dictionary methods below whenever possible.
The following dictionary-like methods work on the element attributes. The following dictionary-like methods work on the element attributes.
...@@ -249,8 +259,8 @@ Element Objects ...@@ -249,8 +259,8 @@ Element Objects
.. method:: keys() .. method:: keys()
Returns the elements attribute names as a list. The names are returned in an Returns the elements attribute names as a list. The names are returned
arbitrary order. in an arbitrary order.
.. method:: set(key, value) .. method:: set(key, value)
...@@ -262,8 +272,8 @@ Element Objects ...@@ -262,8 +272,8 @@ Element Objects
.. method:: append(subelement) .. method:: append(subelement)
Adds the element *subelement* to the end of this elements internal list of Adds the element *subelement* to the end of this elements internal list
subelements. of subelements.
.. method:: extend(subelements) .. method:: extend(subelements)
...@@ -276,22 +286,22 @@ Element Objects ...@@ -276,22 +286,22 @@ Element Objects
.. method:: find(match) .. method:: find(match)
Finds the first subelement matching *match*. *match* may be a tag name or path. Finds the first subelement matching *match*. *match* may be a tag name
Returns an element instance or ``None``. or path. Returns an element instance or ``None``.
.. method:: findall(match) .. method:: findall(match)
Finds all subelements matching *match*. *match* may be a tag name or path. Finds all matching subelements, by tag name or path. Returns a list
Returns an iterable yielding all matching elements in document order. containing all matching elements in document order.
.. method:: findtext(condition[, default]) .. method:: findtext(match[, default])
Finds text for the first subelement matching *condition*. *condition* may be Finds text for the first subelement matching *match*. *match* may be
a tag name or path. Returns the text content of the first matching element, a tag name or path. Returns the text content of the first matching
or *default* if no element was found. Note that if the matching element has element, or *default* if no element was found. Note that if the matching
no text content an empty string is returned. element has no text content an empty string is returned.
.. method:: getchildren() .. method:: getchildren()
...@@ -313,31 +323,48 @@ Element Objects ...@@ -313,31 +323,48 @@ Element Objects
.. method:: iter([tag]) .. method:: iter([tag])
Creates a tree iterator with the current element as the root. The iterator Creates a tree :term:`iterator` with the current element as the root.
iterates over this element and all elements below it, in document (depth The iterator iterates over this element and all elements below it, in
first) order. If *tag* is not ``None`` or ``'*'``, only elements whose tag document (depth first) order. If *tag* is not ``None`` or ``'*'``, only
equals *tag* are returned from the iterator. If the tree structure is elements whose tag equals *tag* are returned from the iterator. If the
modified during iteration, the result is undefined. tree structure is modified during iteration, the result is undefined.
.. method:: iterfind(match)
Finds all matching subelements, by tag name or path. Returns an iterable
yielding all matching elements in document order.
.. versionadded:: 2.7
.. method:: itertext()
Creates a text iterator. The iterator loops over this element and all
subelements, in document order, and returns all inner text.
.. versionadded:: 2.7
.. method:: makeelement(tag, attrib) .. method:: makeelement(tag, attrib)
Creates a new element object of the same type as this element. Do not call Creates a new element object of the same type as this element. Do not
this method, use the SubElement factory function instead. call this method, use the :func:`SubElement` factory function instead.
.. method:: remove(subelement) .. method:: remove(subelement)
Removes *subelement* from the element. Unlike the findXYZ methods this Removes *subelement* from the element. Unlike the find\* methods this
method compares elements based on the instance identity, not on tag value method compares elements based on the instance identity, not on tag value
or contents. or contents.
Element objects also support the following sequence type methods for working :class:`Element` objects also support the following sequence type methods
with subelements: :meth:`__delitem__`, :meth:`__getitem__`, :meth:`__setitem__`, for working with subelements: :meth:`__delitem__`, :meth:`__getitem__`,
:meth:`__len__`. :meth:`__setitem__`, :meth:`__len__`.
Caution: Because Element objects do not define a :meth:`__nonzero__` method, Caution: Elements with no subelements will test as ``False``. This behavior
elements with no subelements will test as ``False``. :: will change in future versions. Use specific ``len(elem)`` or ``elem is
None`` test instead. ::
element = root.find('foo') element = root.find('foo')
...@@ -356,11 +383,12 @@ ElementTree Objects ...@@ -356,11 +383,12 @@ ElementTree Objects
.. class:: ElementTree([element,] [file]) .. class:: ElementTree([element,] [file])
ElementTree wrapper class. This class represents an entire element hierarchy, ElementTree wrapper class. This class represents an entire element
and adds some extra support for serialization to and from standard XML. 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 *element* is the root element. The tree is initialized with the contents
XML *file* if given. of the XML *file* if given.
.. method:: _setroot(element) .. method:: _setroot(element)
...@@ -370,29 +398,28 @@ ElementTree Objects ...@@ -370,29 +398,28 @@ ElementTree Objects
care. *element* is an element instance. care. *element* is an element instance.
.. method:: find(path) .. method:: find(match)
Finds the first toplevel element with given tag. Same as Finds the first toplevel element matching *match*. *match* may be a tag
getroot().find(path). *path* is the element to look for. Returns the name or path. Same as getroot().find(match). Returns the first matching
first matching element, or ``None`` if no element was found. element, or ``None`` if no element was found.
.. method:: findall(path) .. method:: findall(match)
Finds all toplevel elements with the given tag. Same as Finds all matching subelements, by tag name or path. Same as
getroot().findall(path). *path* is the element to look for. Returns a getroot().findall(match). *match* may be a tag name or path. Returns a
list or :term:`iterator` containing all matching elements, in document list containing all matching elements, in document order.
order.
.. method:: findtext(path[, default]) .. method:: findtext(match[, default])
Finds the element text for the first toplevel element with given tag. Finds the element text for the first toplevel element with given tag.
Same as getroot().findtext(path). *path* is the toplevel element to look Same as getroot().findtext(match). *match* may be a tag name or path.
for. *default* is the value to return if the element was not *default* is the value to return if the element was not found. Returns
found. Returns the text content of the first matching element, or the the text content of the first matching element, or the default value no
default value no element was found. Note that if the element has is element was found. Note that if the element is found, but has no text
found, but has no text content, this method returns an empty string. content, this method returns an empty string.
.. method:: getiterator([tag]) .. method:: getiterator([tag])
...@@ -413,6 +440,15 @@ ElementTree Objects ...@@ -413,6 +440,15 @@ ElementTree Objects
to look for (default is to return all elements) to look for (default is to return all elements)
.. method:: iterfind(match)
Finds all matching subelements, by tag name or path. Same as
getroot().iterfind(match). Returns an iterable yielding all matching
elements in document order.
.. versionadded:: 2.7
.. method:: parse(source[, parser]) .. method:: parse(source[, parser])
Loads an external XML section into this element tree. *source* is a file Loads an external XML section into this element tree. *source* is a file
...@@ -446,13 +482,13 @@ Example of changing the attribute "target" of every link in first paragraph:: ...@@ -446,13 +482,13 @@ Example of changing the attribute "target" of every link in first paragraph::
>>> from xml.etree.ElementTree import ElementTree >>> from xml.etree.ElementTree import ElementTree
>>> tree = ElementTree() >>> tree = ElementTree()
>>> tree.parse("index.xhtml") >>> tree.parse("index.xhtml")
<Element 'html' at b7d3f1ec> <Element 'html' at 0xb77e6fac>
>>> p = tree.find("body/p") # Finds first occurrence of tag p in body >>> p = tree.find("body/p") # Finds first occurrence of tag p in body
>>> p >>> p
<Element 'p' at 8416e0c> <Element 'p' at 0xb77ec26c>
>>> links = list(p.iter("a")) # Returns list of all links >>> links = list(p.iter("a")) # Returns list of all links
>>> links >>> links
[<Element 'a' at b7d4f9ec>, <Element 'a' at b7d4fb0c>] [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
>>> for i in links: # Iterates through all found links >>> for i in links: # Iterates through all found links
... i.attrib["target"] = "blank" ... i.attrib["target"] = "blank"
>>> tree.write("output.xhtml") >>> tree.write("output.xhtml")
...@@ -465,12 +501,12 @@ QName Objects ...@@ -465,12 +501,12 @@ QName Objects
.. class:: QName(text_or_uri[, tag]) .. class:: QName(text_or_uri[, tag])
QName wrapper. This can be used to wrap a QName attribute value, in order to QName wrapper. This can be used to wrap a QName attribute value, in order
get proper namespace handling on output. *text_or_uri* is a string containing to get proper namespace handling on output. *text_or_uri* is a string
the QName value, in the form {uri}local, or, if the tag argument is given, the containing the QName value, in the form {uri}local, or, if the tag argument
URI part of a QName. If *tag* is given, the first argument is interpreted as an is given, the URI part of a QName. If *tag* is given, the first argument is
URI, and this argument is interpreted as a local name. :class:`QName` instances interpreted as an URI, and this argument is interpreted as a local name.
are opaque. :class:`QName` instances are opaque.
.. _elementtree-treebuilder-objects: .. _elementtree-treebuilder-objects:
...@@ -481,29 +517,29 @@ TreeBuilder Objects ...@@ -481,29 +517,29 @@ TreeBuilder Objects
.. class:: TreeBuilder([element_factory]) .. class:: TreeBuilder([element_factory])
Generic element structure builder. This builder converts a sequence of start, Generic element structure builder. This builder converts a sequence of
data, and end method calls to a well-formed element structure. You can use this start, data, and end method calls to a well-formed element structure. You
class to build an element structure using a custom XML parser, or a parser for can use this class to build an element structure using a custom XML parser,
some other XML-like format. The *element_factory* is called to create new or a parser for some other XML-like format. The *element_factory* is called
Element instances when given. to create new :class:`Element` instances when given.
.. method:: close() .. method:: close()
Flushes the builder buffers, and returns the toplevel document Flushes the builder buffers, and returns the toplevel document
element. Returns an Element instance. element. Returns an :class:`Element` instance.
.. method:: data(data) .. method:: data(data)
Adds text to the current element. *data* is a string. This should be Adds text to the current element. *data* is a string. This should be
either an 8-bit string containing ASCII text, or a Unicode string. either a bytestring, or a Unicode string.
.. method:: end(tag) .. method:: end(tag)
Closes the current element. *tag* is the element name. Returns the closed Closes the current element. *tag* is the element name. Returns the
element. closed element.
.. method:: start(tag, attrs) .. method:: start(tag, attrs)
...@@ -517,9 +553,9 @@ TreeBuilder Objects ...@@ -517,9 +553,9 @@ TreeBuilder Objects
.. method:: doctype(name, pubid, system) .. method:: doctype(name, pubid, system)
Handles a doctype declaration. *name* is the doctype name. *pubid* is the Handles a doctype declaration. *name* is the doctype name. *pubid* is
public identifier. *system* is the system identifier. This method does not the public identifier. *system* is the system identifier. This method
exist on the default :class:`TreeBuilder` class. does not exist on the default :class:`TreeBuilder` class.
.. versionadded:: 2.7 .. versionadded:: 2.7
...@@ -532,11 +568,12 @@ XMLParser Objects ...@@ -532,11 +568,12 @@ XMLParser Objects
.. class:: XMLParser([html [, target[, encoding]]]) .. class:: XMLParser([html [, target[, encoding]]])
Element structure builder for XML source data, based on the expat parser. *html* :class:`Element` structure builder for XML source data, based on the expat
are predefined HTML entities. This flag is not supported by the current parser. *html* are predefined HTML entities. This flag is not supported by
implementation. *target* is the target object. If omitted, the builder uses an the current implementation. *target* is the target object. If omitted, the
instance of the standard TreeBuilder class. *encoding* [1]_ is optional. builder uses an instance of the standard TreeBuilder class. *encoding* [1]_
If given, the value overrides the encoding specified in the XML file. is optional. If given, the value overrides the encoding specified in the
XML file.
.. method:: close() .. method:: close()
......
:mod:`xml.etree` --- The ElementTree API for XML
================================================
.. module:: xml.etree
:synopsis: Package containing common ElementTree modules.
.. moduleauthor:: Fredrik Lundh <fredrik@pythonware.com>
.. versionadded:: 2.5
The ElementTree package is a simple, efficient, and quite popular library for
XML manipulation in Python. The :mod:`xml.etree` package contains the most
common components from the ElementTree API library. In the current release,
this package contains the :mod:`ElementTree`, :mod:`ElementPath`, and
:mod:`ElementInclude` modules from the full ElementTree distribution.
.. XXX To be continued!
.. seealso::
`ElementTree Overview <http://effbot.org/tag/elementtree>`_
The home page for :mod:`ElementTree`. This includes links to additional
documentation, alternative implementations, and other add-ons.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment