diff options
author | Christian Heimes <christian@cheimes.de> | 2007-12-02 15:22:16 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2007-12-02 15:22:16 (GMT) |
commit | d8654cf758c730af430026a5b20810bebceba4d7 (patch) | |
tree | cba77a4cce70773ed2bf00d183459101c53dfd26 /Doc/library/xml.etree.elementtree.rst | |
parent | b27ce7e46843841e8e8f2c9e5108044d022ae248 (diff) | |
download | cpython-d8654cf758c730af430026a5b20810bebceba4d7.zip cpython-d8654cf758c730af430026a5b20810bebceba4d7.tar.gz cpython-d8654cf758c730af430026a5b20810bebceba4d7.tar.bz2 |
Merged revisions 59259-59274 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59260 | lars.gustaebel | 2007-12-01 22:02:12 +0100 (Sat, 01 Dec 2007) | 5 lines
Issue #1531: Read fileobj from the current offset, do not seek to
the start.
(will backport to 2.5)
........
r59262 | georg.brandl | 2007-12-01 23:24:47 +0100 (Sat, 01 Dec 2007) | 4 lines
Document PyEval_* functions from ceval.c.
Credits to Michael Sloan from GHOP.
........
r59263 | georg.brandl | 2007-12-01 23:27:56 +0100 (Sat, 01 Dec 2007) | 2 lines
Add a few refcount data entries.
........
r59264 | georg.brandl | 2007-12-01 23:38:48 +0100 (Sat, 01 Dec 2007) | 4 lines
Add test suite for cmd module.
Written by Michael Schneider for GHOP.
........
r59265 | georg.brandl | 2007-12-01 23:42:46 +0100 (Sat, 01 Dec 2007) | 3 lines
Add examples to the ElementTree documentation.
Written by h4wk.cz for GHOP.
........
r59266 | georg.brandl | 2007-12-02 00:12:45 +0100 (Sun, 02 Dec 2007) | 3 lines
Add "Using Python on Windows" document, by Robert Lehmann.
Written for GHOP.
........
r59271 | georg.brandl | 2007-12-02 15:34:34 +0100 (Sun, 02 Dec 2007) | 3 lines
Add example to mmap docs.
Written for GHOP by Rafal Rawicki.
........
r59272 | georg.brandl | 2007-12-02 15:37:29 +0100 (Sun, 02 Dec 2007) | 2 lines
Convert bdb.rst line endings to Unix style.
........
r59274 | georg.brandl | 2007-12-02 15:58:50 +0100 (Sun, 02 Dec 2007) | 4 lines
Add more entries to the glossary.
Written by Jeff Wheeler for GHOP.
........
Diffstat (limited to 'Doc/library/xml.etree.elementtree.rst')
-rw-r--r-- | Doc/library/xml.etree.elementtree.rst | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Doc/library/xml.etree.elementtree.rst b/Doc/library/xml.etree.elementtree.rst index 81a9316..172a2a0 100644 --- a/Doc/library/xml.etree.elementtree.rst +++ b/Doc/library/xml.etree.elementtree.rst @@ -31,6 +31,9 @@ convert it from and to XML. 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 +docs. Fredrik Lundh's page is also the location of the development version of the +xml.etree.ElementTree. .. _elementtree-functions: @@ -355,6 +358,33 @@ ElementTree Objects object opened for writing. *encoding* is the output encoding (default is US-ASCII). +This is the XML file that is going to be manipulated:: + + <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:: + + >>> from xml.etree.ElementTree import ElementTree + >>> tree = ElementTree() + >>> tree.parse("index.xhtml") + <Element html at b7d3f1ec> + >>> p = tree.find("body/p") # Finds first occurrence of tag p in body + >>> p + <Element p at 8416e0c> + >>> links = p.getiterator("a") # Returns list of all links + >>> links + [<Element a at b7d4f9ec>, <Element a at b7d4fb0c>] + >>> for i in links: # Iterates through all found links + ... i.attrib["target"] = "blank" + >>> tree.write("output.xhtml") .. _elementtree-qname-objects: @@ -440,3 +470,41 @@ XMLTreeBuilder Objects Feeds data to the parser. *data* is encoded data. +:meth:`XMLTreeBuilder.feed` calls *target*\'s :meth:`start` method +for each opening tag, its :meth:`end` method for each closing tag, +and data is processed by method :meth:`data`. :meth:`XMLTreeBuilder.close` +calls *target*\'s method :meth:`close`. +:class:`XMLTreeBuilder` can be used not only for building a tree structure. +This is an example of counting the maximum depth of an XML file:: + + >>> from xml.etree.ElementTree import XMLTreeBuilder + >>> class MaxDepth: # The target object of the parser + ... maxDepth = 0 + ... depth = 0 + ... def start(self, tag, attrib): # Called for each opening tag. + ... self.depth += 1 + ... if self.depth > self.maxDepth: + ... self.maxDepth = self.depth + ... def end(self, tag): # Called for each closing tag. + ... self.depth -= 1 + ... def data(self, data): + ... pass # We do not need to do anything with data. + ... def close(self): # Called when all data has been parsed. + ... return self.maxDepth + ... + >>> target = MaxDepth() + >>> parser = XMLTreeBuilder(target=target) + >>> exampleXml = """ + ... <a> + ... <b> + ... </b> + ... <b> + ... <c> + ... <d> + ... </d> + ... </c> + ... </b> + ... </a>""" + >>> parser.feed(exampleXml) + >>> parser.close() + 4 |