annotate emeraldtree/ElementTree.py @ 46:c228aba7258c

ElementTree - Also check nodes in children attribute
author Bastian Blank <bblank@thinkmo.de>
date Sun, 27 Jul 2008 20:57:32 +0200
parents 722101222098
children e647f30cc08e
rev   line source
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
2 # ElementTree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
3 # $Id: ElementTree.py 3276 2007-09-12 06:52:30Z fredrik $
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
4 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
5 # light-weight XML support for Python 2.2 and later.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
6 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
7 # history:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
8 # 2001-10-20 fl created (from various sources)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
9 # 2001-11-01 fl return root from parse method
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
10 # 2002-02-16 fl sort attributes in lexical order
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
11 # 2002-04-06 fl TreeBuilder refactoring, added PythonDoc markup
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
12 # 2002-05-01 fl finished TreeBuilder refactoring
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
13 # 2002-07-14 fl added basic namespace support to ElementTree.write
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
14 # 2002-07-25 fl added QName attribute support
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
15 # 2002-10-20 fl fixed encoding in write
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
16 # 2002-11-24 fl changed default encoding to ascii; fixed attribute encoding
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
17 # 2002-11-27 fl accept file objects or file names for parse/write
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
18 # 2002-12-04 fl moved XMLTreeBuilder back to this module
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
19 # 2003-01-11 fl fixed entity encoding glitch for us-ascii
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
20 # 2003-02-13 fl added XML literal factory
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
21 # 2003-02-21 fl added ProcessingInstruction/PI factory
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
22 # 2003-05-11 fl added tostring/fromstring helpers
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
23 # 2003-05-26 fl added ElementPath support
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
24 # 2003-07-05 fl added makeelement factory method
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
25 # 2003-07-28 fl added more well-known namespace prefixes
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
26 # 2003-08-15 fl fixed typo in ElementTree.findtext (Thomas Dartsch)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
27 # 2003-09-04 fl fall back on emulator if ElementPath is not installed
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
28 # 2003-10-31 fl markup updates
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
29 # 2003-11-15 fl fixed nested namespace bug
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
30 # 2004-03-28 fl added XMLID helper
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
31 # 2004-06-02 fl added default support to findtext
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
32 # 2004-06-08 fl fixed encoding of non-ascii element/attribute names
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
33 # 2004-08-23 fl take advantage of post-2.1 expat features
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
34 # 2004-09-03 fl made Element class visible; removed factory
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
35 # 2005-02-01 fl added iterparse implementation
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
36 # 2005-03-02 fl fixed iterparse support for pre-2.2 versions
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
37 # 2005-11-12 fl added tostringlist/fromstringlist helpers
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
38 # 2006-07-05 fl merged in selected changes from the 1.3 sandbox
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
39 # 2006-07-05 fl removed support for 2.1 and earlier
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
40 # 2007-06-21 fl added deprecation/future warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
41 # 2007-08-25 fl added doctype hook, added parser version attribute etc
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
42 # 2007-08-26 fl added new serializer code (better namespace handling, etc)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
43 # 2007-08-27 fl warn for broken /tag searches on tree level
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
44 # 2007-09-02 fl added html/text methods to serializer (experimental)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
45 # 2007-09-05 fl added method argument to tostring/tostringlist
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
46 # 2007-09-06 fl improved error handling
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
47 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
48 # Copyright (c) 1999-2007 by Fredrik Lundh. All rights reserved.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
49 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
50 # fredrik@pythonware.com
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
51 # http://www.pythonware.com
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
52 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
53 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
54 # The ElementTree toolkit is
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
55 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
56 # Copyright (c) 1999-2007 by Fredrik Lundh
44
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
57 # 2008 Bastian Blank <bblank@thinkmo.de>
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
58 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
59 # By obtaining, using, and/or copying this software and/or its
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
60 # associated documentation, you agree that you have read, understood,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
61 # and will comply with the following terms and conditions:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
62 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
63 # Permission to use, copy, modify, and distribute this software and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
64 # its associated documentation for any purpose and without fee is
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
65 # hereby granted, provided that the above copyright notice appears in
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
66 # all copies, and that both that copyright notice and this permission
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
67 # notice appear in supporting documentation, and that the name of
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
68 # Secret Labs AB or the author not be used in advertising or publicity
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
69 # pertaining to distribution of the software without specific, written
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
70 # prior permission.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
71 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
72 # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
73 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
74 # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
75 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
76 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
77 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
78 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
79 # OF THIS SOFTWARE.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
80 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
81
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
82 from __future__ import generators
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
83
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
84 __all__ = [
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
85 # public symbols
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
86 "Comment",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
87 "dump",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
88 "Element", "ElementTree",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
89 "fromstring", "fromstringlist",
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
90 "iterparse",
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
91 "Node",
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
92 "parse", "ParseError",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
93 "PI", "ProcessingInstruction",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
94 "QName",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
95 "SubElement",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
96 "tostring", "tostringlist",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
97 "TreeBuilder",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
98 "XML",
14
3d7555e0be1b Remove several backward compatibility entries.
Bastian Blank <bblank@thinkmo.de>
parents: 13
diff changeset
99 "XMLParser",
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
100 ]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
101
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
102 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
103 # The <b>Element</b> type is a flexible container object, designed to
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
104 # store hierarchical data structures in memory. The type can be
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
105 # described as a cross between a list and a dictionary.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
106 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
107 # Each element has a number of properties associated with it:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
108 # <ul>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
109 # <li>a <i>tag</i>. This is a string identifying what kind of data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
110 # this element represents (the element type, in other words).</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
111 # <li>a number of <i>attributes</i>, stored in a Python dictionary.</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
112 # <li>a <i>text</i> string.</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
113 # <li>an optional <i>tail</i> string.</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
114 # <li>a number of <i>child elements</i>, stored in a Python sequence</li>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
115 # </ul>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
116 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
117 # To create an element instance, use the {@link #Element} constructor
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
118 # or the {@link #SubElement} factory function.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
119 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
120 # The {@link #ElementTree} class can be used to wrap an element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
121 # structure, and convert it from and to XML.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
122 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
123
14
3d7555e0be1b Remove several backward compatibility entries.
Bastian Blank <bblank@thinkmo.de>
parents: 13
diff changeset
124 import ElementPath
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
125
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
126 class ParseError(SyntaxError):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
127 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
128
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
129 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
130
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
131 class Node(object):
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
132 """
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
133 Node class.
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
134 """
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
135
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
136 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
137 # Element class. This class defines the Element interface, and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
138 # provides a reference implementation of this interface.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
139 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
140 # The element name, attribute names, and attribute values can be
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
141 # either 8-bit ASCII strings or Unicode strings.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
142 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
143 # @param tag The element name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
144 # @param attrib An optional dictionary, containing element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
145 # @param **extra Additional attributes, given as keyword arguments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
146 # @see Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
147 # @see SubElement
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
148 # @see Comment
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
149 # @see ProcessingInstruction
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
150
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 1
diff changeset
151 class Element(Node):
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
152 # <tag attrib>text<child/>...</tag>tail
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
153
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
154 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
155 # (Attribute) Element tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
156
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
157 tag = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
158
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
159 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
160 # (Attribute) Element attribute dictionary. Where possible, use
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
161 # {@link #Element.get},
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
162 # {@link #Element.set},
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
163 # {@link #Element.keys}, and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
164 # {@link #Element.items} to access
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
165 # element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
166
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
167 attrib = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
168
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
169 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
170 # (Attribute) Text before first subelement. This is either a
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
171 # string or the value None, if there was no text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
172
7
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
173 @property
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
174 def text(self):
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
175 if len(self) and isinstance(self[0], basestring):
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
176 return self[0]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
177
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
178 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
179 # (Attribute) Text after this element's end tag, but before the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
180 # next sibling element's start tag. This is either a string or
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
181 # the value None, if there was no text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
182
7
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
183 @property
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
184 def tail(self):
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
185 raise RuntimeError('The tail argument is not supported')
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
186
39
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
187 def __init__(self, tag, attrib=None, children=(), **extra):
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
188 attrib = attrib and attrib.copy() or {}
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
189 attrib.update(extra)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
190 self.tag = tag
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
191 self.attrib = attrib
46
c228aba7258c ElementTree - Also check nodes in children attribute
Bastian Blank <bblank@thinkmo.de>
parents: 45
diff changeset
192 self._children = [self._check_node(i) for i in children]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
193
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
194 def __repr__(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
195 return "<Element %s at %x>" % (repr(self.tag), id(self))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
196
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
197 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
198 # Returns the number of subelements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
199 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
200 # @return The number of subelements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
201
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
202 def __len__(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
203 return len(self._children)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
204
20
56cb25aba1e6 Make Element always true.
Bastian Blank <bblank@thinkmo.de>
parents: 19
diff changeset
205 def __nonzero__(self):
56cb25aba1e6 Make Element always true.
Bastian Blank <bblank@thinkmo.de>
parents: 19
diff changeset
206 return True
56cb25aba1e6 Make Element always true.
Bastian Blank <bblank@thinkmo.de>
parents: 19
diff changeset
207
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
208 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
209 # Returns the given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
210 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
211 # @param index What subelement to return.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
212 # @return The given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
213 # @exception IndexError If the given element does not exist.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
214
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
215 def __getitem__(self, index):
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
216 return self._children.__getitem__(index)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
217
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
218 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
219 # Replaces the given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
220 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
221 # @param index What subelement to replace.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
222 # @param element The new element value.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
223 # @exception IndexError If the given element does not exist.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
224 # @exception AssertionError If element is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
225
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
226 def __setitem__(self, index, element):
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
227 if isinstance(index, slice):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
228 element = [self._check_node(i) for i in element]
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
229 else:
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
230 element = self._check_node(element)
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
231 self._children.__setitem__(index, element)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
232
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
233 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
234 # Deletes the given subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
235 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
236 # @param index What subelement to delete.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
237 # @exception IndexError If the given element does not exist.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
238
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
239 def __delitem__(self, index):
19
28fcf9993b62 Remove not longer used functions.
Bastian Blank <bblank@thinkmo.de>
parents: 17
diff changeset
240 self._children.__delitem__(index)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
241
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
242 @staticmethod
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
243 def _check_node(node):
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
244 if isinstance(node, (Node, unicode)):
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
245 return node
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
246 if isinstance(node, str):
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
247 return unicode(node)
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
248 raise TypeError
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
249
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
250 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
251 # Adds a subelement to the end of this element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
252 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
253 # @param element The element to add.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
254 # @exception AssertionError If a sequence member is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
255
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
256 def append(self, element):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
257 element = self._check_node(element)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
258 self._children.append(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
259
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
260 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
261 # Appends subelements from a sequence.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
262 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
263 # @param elements A sequence object with zero or more elements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
264 # @exception AssertionError If a subelement is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
265 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
266
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
267 def extend(self, elements):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
268 elements = [self._check_node(i) for i in elements]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
269 self._children.extend(elements)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
270
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
271 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
272 # Inserts a subelement at the given position in this element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
273 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
274 # @param index Where to insert the new subelement.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
275 # @exception AssertionError If the element is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
276
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
277 def insert(self, index, element):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
278 element = self._check_node(element)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
279 self._children.insert(index, element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
280
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
281 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
282 # Removes a matching subelement. Unlike the <b>find</b> methods,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
283 # this method compares elements based on identity, not on tag
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
284 # value or contents.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
285 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
286 # @param element What element to remove.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
287 # @exception ValueError If a matching element could not be found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
288 # @exception AssertionError If the element is not a valid object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
289
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
290 def remove(self, element):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
291 self._children.remove(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
292
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
293 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
294 # Finds the first matching subelement, by tag name or path.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
295 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
296 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
297 # @return The first matching element, or None if no element was found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
298 # @defreturn Element or None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
299
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
300 def find(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
301 return ElementPath.find(self, path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
302
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
303 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
304 # Finds text for the first matching subelement, by tag name or path.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
305 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
306 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
307 # @param default What to return if the element was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
308 # @return The text content of the first matching element, or the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
309 # default value no element was found. Note that if the element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
310 # has is found, but has no text content, this method returns an
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
311 # empty string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
312 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
313
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
314 def findtext(self, path, default=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
315 return ElementPath.findtext(self, path, default)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
316
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
317 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
318 # Finds all matching subelements, by tag name or path.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
319 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
320 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
321 # @return A list or iterator containing all matching elements,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
322 # in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
323 # @defreturn list of Element instances
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
324
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
325 def findall(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
326 return ElementPath.findall(self, path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
327
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
328 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
329 # Resets an element. This function removes all subelements, clears
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
330 # all attributes, and sets the text and tail attributes to None.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
331
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
332 def clear(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
333 self.attrib.clear()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
334 self._children = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
335
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
336 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
337 # Gets an element attribute.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
338 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
339 # @param key What attribute to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
340 # @param default What to return if the attribute was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
341 # @return The attribute value, or the default value, if the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
342 # attribute was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
343
33
0d172e5766fc Revert 29:3a9710b7a785: Element - Use standard get behaviour
Bastian Blank <bblank@thinkmo.de>
parents: 29
diff changeset
344 def get(self, key, default=None):
0d172e5766fc Revert 29:3a9710b7a785: Element - Use standard get behaviour
Bastian Blank <bblank@thinkmo.de>
parents: 29
diff changeset
345 return self.attrib.get(key, default)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
346
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
347 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
348 # Sets an element attribute.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
349 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
350 # @param key What attribute to set.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
351 # @param value The attribute value.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
352
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
353 def set(self, key, value):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
354 self.attrib[key] = value
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
355
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
356 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
357 # Gets a list of attribute names. The names are returned in an
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
358 # arbitrary order (just like for an ordinary Python dictionary).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
359 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
360 # @return A list of element attribute names.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
361 # @defreturn list of strings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
362
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
363 def keys(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
364 return self.attrib.keys()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
365
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
366 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
367 # Gets element attributes, as a sequence. The attributes are
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
368 # returned in an arbitrary order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
369 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
370 # @return A list of (name, value) tuples for all attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
371 # @defreturn list of (string, string) tuples
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
372
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
373 def items(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
374 return self.attrib.items()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
375
37
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
376 def __iter__(self):
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
377 """
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
378 Creates a element iterator. The iterator loops over all children.
37
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
379 """
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
380 return self._children.__iter__()
37
4bb8fc436265 ElementTree - Add real iterator to Element
Bastian Blank <bblank@thinkmo.de>
parents: 36
diff changeset
381
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
382 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
383 # Creates a tree iterator. The iterator loops over this element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
384 # and all subelements, in document order, and returns all elements
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
385 # with a matching tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
386 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
387 # If the tree structure is modified during iteration, new or removed
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
388 # elements may or may not be included. To get a stable set, use the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
389 # list() function on the iterator, and loop over the resulting list.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
390 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
391 # @param tag What tags to look for (default is to return all elements).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
392 # @return An iterator containing all the matching elements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
393 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
394
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
395 def iter(self, tag=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
396 if tag == "*":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
397 tag = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
398 if tag is None or self.tag == tag:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
399 yield self
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
400 for e in self._children:
5
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
401 if isinstance(e, Element):
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
402 for e in e.iter(tag):
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
403 yield e
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 37
diff changeset
404 else:
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
405 yield e
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
406
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
407 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
408 # Creates a text iterator. The iterator loops over this element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
409 # and all subelements, in document order, and returns all inner
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
410 # text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
411 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
412 # @return An iterator containing all inner text.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
413 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
414
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
415 def itertext(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
416 for e in self:
9
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
417 if isinstance(e, Element):
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
418 for s in e.itertext():
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
419 yield s
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
420 elif isinstance(e, basestring):
e0965f86912a Fix text iterator.
Bastian Blank <bblank@thinkmo.de>
parents: 7
diff changeset
421 yield e
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
422
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
423 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
424 # Subelement factory. This function creates an element instance, and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
425 # appends it to an existing element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
426 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
427 # The element name, attribute names, and attribute values can be
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
428 # either 8-bit ASCII strings or Unicode strings.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
429 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
430 # @param parent The parent element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
431 # @param tag The subelement name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
432 # @param attrib An optional dictionary, containing element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
433 # @param **extra Additional attributes, given as keyword arguments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
434 # @return An element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
435 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
436
39
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
437 def SubElement(parent, tag, attrib=None, **extra):
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
438 attrib = attrib and attrib.copy() or {}
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
439 attrib.update(extra)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
440 element = parent.makeelement(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
441 parent.append(element)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
442 return element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
443
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
444 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
445 # Comment element factory. This factory function creates a special
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
446 # element that will be serialized as an XML comment by the standard
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
447 # serializer.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
448 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
449 # The comment string can be either an 8-bit ASCII string or a Unicode
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
450 # string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
451 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
452 # @param text A string containing the comment string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
453 # @return An element instance, representing a comment.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
454 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
455
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
456 class Comment(Node):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
457 def __init__(self, text = None):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
458 self.text = text
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
459
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
460 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
461 # PI element factory. This factory function creates a special element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
462 # that will be serialized as an XML processing instruction by the standard
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
463 # serializer.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
464 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
465 # @param target A string containing the PI target.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
466 # @param text A string containing the PI contents, if any.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
467 # @return An element instance, representing a PI.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
468 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
469
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
470 class ProcessingInstruction(Node):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
471 def __init__(self, target, text = None):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
472 self.target, self.text = target, text
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
473
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
474 PI = ProcessingInstruction
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
475
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
476 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
477 # QName wrapper. This can be used to wrap a QName attribute value, in
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
478 # order to get proper namespace handling on output.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
479 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
480 # @return An opaque object, representing the QName.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
481
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
482 class QName(unicode):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
483 __slots__ = 'name', 'uri'
35
00ee3db419a9 ElementTree - Remove text property, the value is static anyway
Bastian Blank <bblank@thinkmo.de>
parents: 34
diff changeset
484
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
485 def __new__(cls, name, uri=None):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
486 text = name = unicode(name)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
487
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
488 if name[0] == '{':
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
489 if uri is not None:
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
490 raise ValueError
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
491 i = name.find('}')
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
492 if i == -1:
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
493 raise ValueError
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
494 uri = name[1:i]
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
495 name = name[i + 1:]
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 14
diff changeset
496
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
497 if uri is not None:
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
498 text = '{' + uri + '}' + name
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
499
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
500 ret = unicode.__new__(cls, text)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
501 unicode.__setattr__(ret, 'name', name)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
502 unicode.__setattr__(ret, 'uri', uri)
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
503
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
504 return ret
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
505
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
506 def __getnewargs__(self):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
507 return self.name, self.uri
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
508
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
509 def __getstate__(self):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
510 pass
35
00ee3db419a9 ElementTree - Remove text property, the value is static anyway
Bastian Blank <bblank@thinkmo.de>
parents: 34
diff changeset
511
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
512 def __repr__(self):
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
513 return '%s(%r, %r)' % (self.__class__.__name__, self.name, self.uri)
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
514
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
515 def __setattr__(self, key, value):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
516 raise AttributeError('read-only')
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
517
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
518 def copy(self):
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
519 return self.__class__(self.name, self.uri)
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
520
41
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
521 @property
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
522 def text(self):
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
523 return self
f9fe97f25af0 ElementTree - Derive QName from unicode
Bastian Blank <bblank@thinkmo.de>
parents: 39
diff changeset
524
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
525 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
526
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
527 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
528 # ElementTree wrapper class. This class represents an entire element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
529 # hierarchy, and adds some extra support for serialization to and from
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
530 # standard XML.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
531 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
532 # @param element Optional root element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
533 # @keyparam file Optional file handle or file name. If given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
534 # tree is initialized with the contents of this XML file.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
535
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
536 class ElementTree(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
537
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
538 def __init__(self, element=None, file=None):
22
0aa00b020bc1 Always add unicode objects.
Bastian Blank <bblank@thinkmo.de>
parents: 21
diff changeset
539 assert element is None or isinstance(element, Node)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
540 self._root = element # first node
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
541 if file:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
542 self.parse(file)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
543
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
544 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
545 # Gets the root element for this tree.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
546 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
547 # @return An element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
548 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
549
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
550 def getroot(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
551 return self._root
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
552
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
553 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
554 # Loads an external XML document into this element tree.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
555 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
556 # @param source A file name or file object.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
557 # @keyparam parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
558 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
559 # @return The document root element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
560 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
561
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
562 def parse(self, source, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
563 if not hasattr(source, "read"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
564 source = open(source, "rb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
565 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
566 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
567 while 1:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
568 data = source.read(32768)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
569 if not data:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
570 break
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
571 parser.feed(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
572 self._root = parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
573 return self._root
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
574
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
575 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
576 # Creates a tree iterator for the root element. The iterator loops
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
577 # over all elements in this tree, in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
578 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
579 # @param tag What tags to look for (default is to return all elements)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
580 # @return An iterator.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
581 # @defreturn iterator
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
582
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
583 def iter(self, tag=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
584 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
585 return self._root.iter(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
586
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
587 getiterator = iter
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
588
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
589 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
590 # Finds the first toplevel element with given tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
591 # Same as getroot().find(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
592 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
593 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
594 # @return The first matching element, or None if no element was found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
595 # @defreturn Element or None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
596
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
597 def find(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
598 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
599 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
600 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
601 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
602 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
603 "This search is broken in 1.3 and earlier; if you rely "
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
604 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
605 FutureWarning
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
606 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
607 return self._root.find(path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
608
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
609 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
610 # Finds the element text for the first toplevel element with given
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
611 # tag. Same as getroot().findtext(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
612 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
613 # @param path What toplevel element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
614 # @param default What to return if the element was not found.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
615 # @return The text content of the first matching element, or the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
616 # default value no element was found. Note that if the element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
617 # has is found, but has no text content, this method returns an
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
618 # empty string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
619 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
620
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
621 def findtext(self, path, default=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
622 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
623 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
624 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
625 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
626 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
627 "This search is broken in 1.3 and earlier; if you rely "
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
628 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
629 FutureWarning
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
630 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
631 return self._root.findtext(path, default)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
632
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
633 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
634 # Finds all toplevel elements with the given tag.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
635 # Same as getroot().findall(path).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
636 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
637 # @param path What element to look for.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
638 # @return A list or iterator containing all matching elements,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
639 # in document order.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
640 # @defreturn list of Element instances
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
641
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
642 def findall(self, path):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
643 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
644 if path[:1] == "/":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
645 path = "." + path
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
646 import warnings
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
647 warnings.warn(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
648 "This search is broken in 1.3 and earlier; if you rely "
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
649 "on the current behaviour, change it to %r" % path,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
650 FutureWarning
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
651 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
652 return self._root.findall(path)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
653
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
654 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
655 # Writes the element tree to a file, as XML.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
656 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
657 # @param file A file name, or a file object opened for writing.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
658 # @keyparam encoding Optional output encoding (default is US-ASCII).
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
659 # @keyparam method Optional output method ("xml" or "html"; default
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
660 # is "xml".
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
661 # @keyparam xml_declaration Controls if an XML declaration should
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
662 # be added to the file. Use False for never, True for always,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
663 # None for only if not US-ASCII or UTF-8. None is default.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
664
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
665 def write(self, file,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
666 # keyword arguments
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
667 encoding="us-ascii",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
668 xml_declaration=None,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
669 default_namespace=None,
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
670 method=None,
39
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
671 namespaces=None):
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
672 assert self._root is not None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
673 if not hasattr(file, "write"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
674 file = open(file, "wb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
675 write = file.write
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
676 if not method:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
677 method = "xml"
44
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
678 elif method == 'html':
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
679 default_namespace = "http://www.w3.org/1999/xhtml"
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
680 if not encoding:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
681 encoding = "us-ascii"
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
682 elif xml_declaration or (xml_declaration is None and
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
683 encoding not in ("utf-8", "us-ascii")):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
684 write("<?xml version='1.0' encoding='%s'?>\n" % encoding)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
685 if method == "text":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
686 _serialize_text(write, self._root, encoding)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
687 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
688 qnames, namespaces = _namespaces(
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
689 self._root, encoding, default_namespace, namespaces
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
690 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
691 if method == "xml":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
692 _serialize_xml(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
693 write, self._root, encoding, qnames, namespaces
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
694 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
695 elif method == "html":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
696 _serialize_html(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
697 write, self._root, encoding, qnames, namespaces
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
698 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
699 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
700 raise ValueError("unknown method %r" % method)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
701
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
702 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
703 # serialization support
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
704
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
705 def _namespaces(elem, encoding, default_namespace, namespaces):
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
706 # identify namespaces used in this tree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
707
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
708 # maps qnames to *encoded* prefix:local names
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
709 qnames = {None: None}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
710
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
711 # maps uri:s to prefixes
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
712 candidate_namespaces = _namespace_map.copy()
39
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
713 if namespaces:
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
714 candidate_namespaces.update(namespaces)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
715 if default_namespace:
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
716 candidate_namespaces[default_namespace] = ""
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
717 used_namespaces = {}
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
718
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
719 def encode(text):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
720 return text.encode(encoding)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
721
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
722 def add_qname(qname):
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
723 if qname in qnames:
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
724 return
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
725
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
726 # calculate serialized qname representation
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
727 try:
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
728 if qname.uri is not None:
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
729 uri = qname.uri
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
730 prefix = used_namespaces.get(uri, None)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
731 if prefix is None:
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
732 prefix = candidate_namespaces.get(uri, None)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
733 if prefix is None:
39
5784bad989d1 ElementTree - Don't use dict and list as argument default value
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
734 prefix = "ns%d" % len(used_namespaces)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
735 if prefix != "xml":
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
736 used_namespaces[uri] = prefix
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
737 if prefix:
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
738 qnames[qname] = encode("%s:%s" % (prefix, qname.name))
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
739 else:
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
740 qnames[qname] = encode(qname.name)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
741 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
742 if default_namespace:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
743 # FIXME: can this be handled in XML 1.0?
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
744 raise ValueError(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
745 "cannot use non-qualified names with "
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
746 "default_namespace option"
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
747 )
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
748 qnames[qname] = encode(qname.name)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
749 except TypeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
750 _raise_serialization_error(qname)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
751
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
752 # populate qname and namespaces table
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
753 if isinstance(elem, Element):
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
754 for elem in elem.iter():
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
755 if isinstance(elem, Element):
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
756 tag = elem.tag
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
757 if isinstance(tag, QName):
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
758 add_qname(tag)
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
759 elif isinstance(tag, basestring):
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
760 add_qname(QName(tag))
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
761 elif tag is not None:
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
762 _raise_serialization_error(tag)
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
763
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
764 for key in elem.keys():
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
765 if isinstance(key, QName):
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
766 add_qname(key)
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
767 elif isinstance(key, basestring):
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
768 add_qname(QName(key))
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
769 elif key is not None:
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
770 _raise_serialization_error(key)
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
771
28
21cef3864c33 Add namespace list to XML serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
772 return qnames, used_namespaces
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
773
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
774 def _serialize_xml(write, elem, encoding, qnames, namespaces):
34
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
775 if isinstance(elem, Element):
11
ec93cdb38b9e Convert Comment and ProcessingInstruction to classes.
Bastian Blank <bblank@thinkmo.de>
parents: 9
diff changeset
776 tag = qnames[elem.tag]
34
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
777
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
778 if tag is not None:
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
779 write("<" + tag)
34
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
780
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
781 if elem.attrib:
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
782 items = elem.attrib.items()
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
783 items.sort(key=lambda x: x[0])
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
784 for k, v in items:
34
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
785 k = qnames[k]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
786 if isinstance(v, QName):
34
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
787 v = qnames[v]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
788 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
789 v = _escape_attrib(v, encoding)
34
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
790 write(' ' + k + '="' + v + '"')
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
791 if namespaces:
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
792 items = namespaces.items()
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
793 items.sort(key=lambda x: x[1]) # sort on prefix
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
794 for v, k in items:
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
795 if k:
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
796 k = ":" + k
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
797 write(" xmlns%s=\"%s\"" % (
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
798 k.encode(encoding),
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
799 _escape_attrib(v, encoding)
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
800 ))
7
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
801 if len(elem):
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
802 write(">")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
803 for e in elem:
12
1eb02b658530 Simplify serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 11
diff changeset
804 _serialize_xml(write, e, encoding, qnames, None)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
805 write("</" + tag + ">")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
806 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
807 write(" />")
34
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
808
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
809 else:
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
810 for e in elem:
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
811 _serialize_xml(write, e, encoding, qnames, None)
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
812
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
813 elif isinstance(elem, Comment):
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
814 write("<!--%s-->" % _escape_cdata(elem.text, encoding))
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
815
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
816 elif isinstance(elem, ProcessingInstruction):
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
817 text = _escape_cdata(elem.target, encoding)
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
818 if elem.text is not None:
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
819 text += ' ' + _escape_cdata(elem.text, encoding)
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
820 write("<?%s?>" % text)
296964441d48 ElementTree - Optimize XML serializer a little bit
Bastian Blank <bblank@thinkmo.de>
parents: 33
diff changeset
821
12
1eb02b658530 Simplify serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 11
diff changeset
822 else:
1eb02b658530 Simplify serializer.
Bastian Blank <bblank@thinkmo.de>
parents: 11
diff changeset
823 write(_escape_cdata(unicode(elem), encoding))
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
824
44
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
825 HTML_EMPTY = set(("area", "base", "basefont", "br", "col", "frame", "hr",
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
826 "img", "input", "isindex", "link", "meta" "param"))
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
827
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
828 def _serialize_html(write, elem, encoding, qnames, namespaces):
44
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
829 if isinstance(elem, Element):
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
830 tag = qnames[elem.tag]
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
831
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
832 if tag is not None:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
833 write("<" + tag)
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
834
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
835 if elem.attrib:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
836 items = elem.attrib.items()
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
837 items.sort(key=lambda x: x[0])
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
838 for k, v in items:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
839 k = qnames[k]
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
840 if isinstance(v, QName):
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
841 v = qnames[v]
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
842 else:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
843 v = _escape_attrib(v, encoding)
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
844 # FIXME: handle boolean attributes
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
845 write(' ' + k + '="' + v + '"')
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
846 if namespaces:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
847 items = namespaces.items()
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
848 items.sort(key=lambda x: x[1]) # sort on prefix
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
849 for v, k in items:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
850 if k:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
851 k = ":" + k
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
852 write(" xmlns%s=\"%s\"" % (
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
853 k.encode(encoding),
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
854 _escape_attrib(v, encoding)
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
855 ))
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
856 write(">")
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
857
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
858 if tag.lower() in ('script', 'style'):
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
859 write(_encode(''.join(elem.itertext()), encoding))
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
860 else:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
861 for e in elem:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
862 _serialize_html(write, e, encoding, qnames, None)
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
863
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
864 if tag not in HTML_EMPTY:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
865 write("</" + tag + ">")
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
866
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
867 else:
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
868 for e in elem:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
869 _serialize_html(write, e, encoding, qnames, None)
44
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
870
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
871 elif isinstance(elem, Comment):
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
872 write("<!--%s-->" % _escape_cdata(elem.text, encoding))
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
873
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
874 elif isinstance(elem, ProcessingInstruction):
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
875 text = _escape_cdata(elem.target, encoding)
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
876 if elem.text is not None:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
877 text += ' ' + _escape_cdata(elem.text, encoding)
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
878 write("<?%s?>" % text)
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
879
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
880 else:
53378e9b1814 ElementTree - Update copyright, fix html output, force default namespace for html output
Bastian Blank <bblank@thinkmo.de>
parents: 41
diff changeset
881 write(_escape_cdata(elem, encoding))
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
882
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
883 def _serialize_text(write, elem, encoding):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
884 for part in elem.itertext():
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
885 write(part.encode(encoding))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
886
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
887 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
888 # Registers a namespace prefix. The registry is global, and any
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
889 # existing mapping for either the given prefix or the namespace URI
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
890 # will be removed.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
891 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
892 # @param prefix Namespace prefix.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
893 # @param uri Namespace uri. Tags and attributes in this namespace
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
894 # will be serialized with the given prefix, if at all possible.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
895 # @raise ValueError If the prefix is reserved, or is otherwise
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
896 # invalid.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
897
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
898 def register_namespace(prefix, uri):
13
99619772e57c Move imports.
Bastian Blank <bblank@thinkmo.de>
parents: 12
diff changeset
899 import re
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
900 if re.match("ns\d+$", prefix):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
901 raise ValueError("Prefix format reserved for internal use")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
902 for k, v in _namespace_map.items():
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
903 if k == uri or v == prefix:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
904 del _namespace_map[k]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
905 _namespace_map[uri] = prefix
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
906
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
907 _namespace_map = {
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
908 # "well-known" namespace prefixes
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
909 "http://www.w3.org/XML/1998/namespace": "xml",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
910 "http://www.w3.org/1999/xhtml": "html",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
911 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
912 "http://schemas.xmlsoap.org/wsdl/": "wsdl",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
913 # xml schema
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
914 "http://www.w3.org/2001/XMLSchema": "xs",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
915 "http://www.w3.org/2001/XMLSchema-instance": "xsi",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
916 # dublic core
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
917 "http://purl.org/dc/elements/1.1/": "dc",
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
918 }
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
919
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
920 def _raise_serialization_error(text):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
921 raise TypeError(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
922 "cannot serialize %r (type %s)" % (text, type(text).__name__)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
923 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
924
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
925 def _encode(text, encoding):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
926 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
927 return text.encode(encoding, "xmlcharrefreplace")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
928 except (TypeError, AttributeError):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
929 _raise_serialization_error(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
930
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
931 def _escape_cdata(text, encoding):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
932 # escape character data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
933 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
934 # it's worth avoiding do-nothing calls for strings that are
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
935 # shorter than 500 character, or so. assume that's, by far,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
936 # the most common case in most applications.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
937 if "&" in text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
938 text = text.replace("&", "&amp;")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
939 if "<" in text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
940 text = text.replace("<", "&lt;")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
941 if ">" in text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
942 text = text.replace(">", "&gt;")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
943 return text.encode(encoding, "xmlcharrefreplace")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
944 except (TypeError, AttributeError):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
945 _raise_serialization_error(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
946
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
947 def _escape_attrib(text, encoding):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
948 # escape attribute value
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
949 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
950 if "&" in text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
951 text = text.replace("&", "&amp;")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
952 if "<" in text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
953 text = text.replace("<", "&lt;")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
954 if ">" in text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
955 text = text.replace(">", "&gt;")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
956 if "\"" in text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
957 text = text.replace("\"", "&quot;")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
958 if "\n" in text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
959 text = text.replace("\n", "&#10;")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
960 return text.encode(encoding, "xmlcharrefreplace")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
961 except (TypeError, AttributeError):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
962 _raise_serialization_error(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
963
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
964 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
965
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
966 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
967 # Generates a string representation of an XML element, including all
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
968 # subelements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
969 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
970 # @param element An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
971 # @return An encoded string containing the XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
972 # @defreturn string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
973
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
974 def tostring(element, encoding=None, method=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
975 class dummy:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
976 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
977 data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
978 file = dummy()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
979 file.write = data.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
980 ElementTree(element).write(file, encoding, method=method)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
981 return "".join(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
982
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
983 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
984 # Generates a string representation of an XML element, including all
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
985 # subelements. The string is returned as a sequence of string fragments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
986 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
987 # @param element An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
988 # @return A sequence object containing the XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
989 # @defreturn sequence
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
990 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
991
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
992 def tostringlist(element, encoding=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
993 class dummy:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
994 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
995 data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
996 file = dummy()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
997 file.write = data.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
998 ElementTree(element).write(file, encoding)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
999 # FIXME: merge small fragments into larger parts
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1000 return data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1001
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1002 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1003 # Writes an element tree or element structure to sys.stdout. This
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1004 # function should be used for debugging only.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1005 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1006 # The exact output format is implementation dependent. In this
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1007 # version, it's written as an ordinary XML file.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1008 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1009 # @param elem An element tree or an individual element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1010
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1011 def dump(elem):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1012 # debugging
13
99619772e57c Move imports.
Bastian Blank <bblank@thinkmo.de>
parents: 12
diff changeset
1013 import sys
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1014 if not isinstance(elem, ElementTree):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1015 elem = ElementTree(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1016 elem.write(sys.stdout)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1017 tail = elem.getroot().tail
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1018 if not tail or tail[-1] != "\n":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1019 sys.stdout.write("\n")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1020
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1021 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1022 # parsing
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1023
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1024 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1025 # Parses an XML document into an element tree.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1026 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1027 # @param source A filename or file object containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1028 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1029 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1030 # @return An ElementTree instance
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1031
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1032 def parse(source, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1033 tree = ElementTree()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1034 tree.parse(source, parser)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1035 return tree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1036
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1037 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1038 # Parses an XML document into an element tree incrementally, and reports
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1039 # what's going on to the user.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1040 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1041 # @param source A filename or file object containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1042 # @param events A list of events to report back. If omitted, only "end"
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1043 # events are reported.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1044 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1045 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1046 # @return A (event, elem) iterator.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1047
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1048 def iterparse(source, events=None, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1049 if not hasattr(source, "read"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1050 source = open(source, "rb")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1051 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1052 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1053 return _IterParseIterator(source, events, parser)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1054
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1055 class _IterParseIterator(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1056
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1057 def __init__(self, source, events, parser):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1058 self._file = source
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1059 self._events = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1060 self._index = 0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1061 self.root = self._root = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1062 self._parser = parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1063 # wire up the parser for event reporting
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1064 parser = self._parser._parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1065 append = self._events.append
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1066 if events is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1067 events = ["end"]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1068 for event in events:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1069 if event == "start":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1070 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1071 parser.ordered_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1072 parser.specified_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1073 def handler(tag, attrib_in, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1074 start=self._parser._start_list):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1075 append((event, start(tag, attrib_in)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1076 parser.StartElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1077 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1078 def handler(tag, attrib_in, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1079 start=self._parser._start):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1080 append((event, start(tag, attrib_in)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1081 parser.StartElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1082 elif event == "end":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1083 def handler(tag, event=event, append=append,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1084 end=self._parser._end):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1085 append((event, end(tag)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1086 parser.EndElementHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1087 elif event == "start-ns":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1088 def handler(prefix, uri, event=event, append=append):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1089 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1090 uri = uri.encode("ascii")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1091 except UnicodeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1092 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1093 append((event, (prefix or "", uri)))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1094 parser.StartNamespaceDeclHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1095 elif event == "end-ns":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1096 def handler(prefix, event=event, append=append):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1097 append((event, None))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1098 parser.EndNamespaceDeclHandler = handler
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1099
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1100 def next(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1101 while 1:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1102 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1103 item = self._events[self._index]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1104 except IndexError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1105 if self._parser is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1106 self.root = self._root
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1107 raise StopIteration
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1108 # load event buffer
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1109 del self._events[:]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1110 self._index = 0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1111 data = self._file.read(16384)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1112 if data:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1113 self._parser.feed(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1114 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1115 self._root = self._parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1116 self._parser = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1117 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1118 self._index = self._index + 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1119 return item
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1120
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1121 def __iter__(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1122 return self
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1123
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1124 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1125 # Parses an XML document from a string constant. This function can
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1126 # be used to embed "XML literals" in Python code.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1127 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1128 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1129 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1130 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1131 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1132 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1133
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1134 def XML(text, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1135 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1136 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1137 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1138 return parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1139
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1140 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1141 # Parses an XML document from a string constant, and also returns
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1142 # a dictionary which maps from element id:s to elements.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1143 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1144 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1145 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1146 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1147 # @return A tuple containing an Element instance and a dictionary.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1148 # @defreturn (Element, dictionary)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1149
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1150 def XMLID(text, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1151 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1152 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1153 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1154 tree = parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1155 ids = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1156 for elem in tree.getiterator():
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1157 id = elem.get("id")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1158 if id:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1159 ids[id] = elem
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1160 return tree, ids
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1161
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1162 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1163 # Parses an XML document from a string constant. Same as {@link #XML}.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1164 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1165 # @def fromstring(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1166 # @param source A string containing XML data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1167 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1168 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1169
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1170 fromstring = XML
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1171
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1172 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1173 # Parses an XML document from a sequence of string fragments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1174 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1175 # @param sequence A list or other sequence containing XML data fragments.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1176 # @param parser An optional parser instance. If not given, the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1177 # standard {@link XMLParser} parser is used.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1178 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1179 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1180 # @since 1.3
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1181
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1182 def fromstringlist(sequence, parser=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1183 if not parser:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1184 parser = XMLParser(target=TreeBuilder())
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1185 for text in sequence:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1186 parser.feed(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1187 return parser.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1188
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1189 # --------------------------------------------------------------------
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1190
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1191 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1192 # Generic element structure builder. This builder converts a sequence
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1193 # of {@link #TreeBuilder.start}, {@link #TreeBuilder.data}, and {@link
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1194 # #TreeBuilder.end} method calls to a well-formed element structure.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1195 # <p>
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1196 # You can use this class to build an element structure using a custom XML
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1197 # parser, or a parser for some other XML-like format.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1198 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1199 # @param element_factory Optional element factory. This factory
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1200 # is called to create new Element instances, as necessary.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1201
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1202 class TreeBuilder(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1203
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1204 def __init__(self, element_factory=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1205 self._data = [] # data collector
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1206 self._elem = [] # element stack
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1207 self._last = None # last element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1208 if element_factory is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1209 element_factory = Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1210 self._factory = element_factory
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1211
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1212 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1213 # Flushes the builder buffers, and returns the toplevel document
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1214 # element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1215 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1216 # @return An Element instance.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1217 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1218
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1219 def close(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1220 assert len(self._elem) == 0, "missing end tags"
21
7b33b90de8be some minor coding style cleanups
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 20
diff changeset
1221 assert self._last is not None, "missing toplevel element"
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1222 return self._last
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1223
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1224 def _flush(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1225 if self._data:
7
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
1226 text = "".join(self._data)
a30d65781076 Make parser add text as strings.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
1227 self._elem[-1].append(text)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1228 self._data = []
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1229
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1230 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1231 # Adds text to the current element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1232 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1233 # @param data A string. This should be either an 8-bit string
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1234 # containing ASCII text, or a Unicode string.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1235
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1236 def data(self, data):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1237 self._data.append(data)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1238
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1239 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1240 # Opens a new element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1241 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1242 # @param tag The element name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1243 # @param attrib A dictionary containing element attributes.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1244 # @return The opened element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1245 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1246
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1247 def start(self, tag, attrs):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1248 self._flush()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1249 self._last = elem = self._factory(tag, attrs)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1250 if self._elem:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1251 self._elem[-1].append(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1252 self._elem.append(elem)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1253 return elem
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1254
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1255 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1256 # Closes the current element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1257 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1258 # @param tag The element name.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1259 # @return The closed element.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1260 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1261
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1262 def end(self, tag):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1263 self._flush()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1264 self._last = self._elem.pop()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1265 assert self._last.tag == tag,\
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1266 "end tag mismatch (expected %s, got %s)" % (
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1267 self._last.tag, tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1268 return self._last
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1269
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1270 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1271 # Element structure builder for XML source data, based on the
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1272 # <b>expat</b> parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1273 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1274 # @keyparam target Target object. If omitted, the builder uses an
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1275 # instance of the standard {@link #TreeBuilder} class.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1276 # @keyparam html Predefine HTML entities. This flag is not supported
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1277 # by the current implementation.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1278 # @keyparam encoding Optional encoding. If given, the value overrides
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1279 # the encoding specified in the XML file.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1280 # @see #ElementTree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1281 # @see #TreeBuilder
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1282
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1283 class XMLParser(object):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1284
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1285 def __init__(self, html=0, target=None, encoding=None):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1286 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1287 from xml.parsers import expat
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1288 except ImportError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1289 try:
21
7b33b90de8be some minor coding style cleanups
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 20
diff changeset
1290 import pyexpat
7b33b90de8be some minor coding style cleanups
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 20
diff changeset
1291 expat = pyexpat
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1292 except ImportError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1293 raise ImportError(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1294 "No module named expat; use SimpleXMLTreeBuilder instead"
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1295 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1296 parser = expat.ParserCreate(encoding, "}")
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1297 if target is None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1298 target = TreeBuilder()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1299 # underscored names are provided for compatibility only
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1300 self.parser = self._parser = parser
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1301 self.target = self._target = target
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1302 self._error = expat.error
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1303 self._names = {} # name memo cache
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1304 # callbacks
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1305 parser.DefaultHandlerExpand = self._default
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1306 parser.StartElementHandler = self._start
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1307 parser.EndElementHandler = self._end
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1308 parser.CharacterDataHandler = self._data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1309 # let expat do the buffering, if supported
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1310 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1311 self._parser.buffer_text = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1312 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1313 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1314 # use new-style attribute handling, if supported
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1315 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1316 self._parser.ordered_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1317 self._parser.specified_attributes = 1
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1318 parser.StartElementHandler = self._start_list
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1319 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1320 pass
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1321 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1322 self.entity = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1323 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1324 self.version = "Expat %d.%d.%d" % expat.version_info
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1325 except AttributeError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1326 pass # unknown
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1327
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1328 def _raiseerror(self, value):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1329 err = ParseError(value)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1330 err.code = value.code
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1331 err.position = value.lineno, value.offset
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1332 raise err
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1333
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1334 def _fixname(self, key):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1335 # expand qname, and convert name string to ascii, if possible
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1336 if key in self._names:
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1337 return self._names[key].copy()
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1338 if '}' in key:
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
1339 uri, name = key.split('}', 1)
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
1340 name = QName(name, uri)
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1341 else:
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1342 name = QName(key)
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1343 self._names[key] = name
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1344 return name
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1345
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1346 def _start(self, tag, attrib_in):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1347 fixname = self._fixname
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1348 tag = fixname(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1349 attrib = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1350 for key, value in attrib_in.items():
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1351 attrib[fixname(key)] = value
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1352 return self.target.start(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1353
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1354 def _start_list(self, tag, attrib_in):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1355 fixname = self._fixname
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1356 tag = fixname(tag)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1357 attrib = {}
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1358 if attrib_in:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1359 for i in range(0, len(attrib_in), 2):
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1360 attrib[fixname(attrib_in[i])] = attrib_in[i+1]
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1361 return self.target.start(tag, attrib)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1362
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1363 def _data(self, text):
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
1364 return self.target.data(text)
0
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1365
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1366 def _end(self, tag):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1367 return self.target.end(self._fixname(tag))
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1368
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1369 def _default(self, text):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1370 prefix = text[:1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1371 if prefix == "&":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1372 # deal with undefined entities
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1373 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1374 self.target.data(self.entity[text[1:-1]])
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1375 except KeyError:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1376 from xml.parsers import expat
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1377 err = expat.error(
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1378 "undefined entity %s: line %d, column %d" %
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1379 (text, self._parser.ErrorLineNumber,
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1380 self._parser.ErrorColumnNumber)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1381 )
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1382 err.code = 11 # XML_ERROR_UNDEFINED_ENTITY
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1383 err.lineno = self._parser.ErrorLineNumber
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1384 err.offset = self._parser.ErrorColumnNumber
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1385 raise err
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1386 elif prefix == "<" and text[:9] == "<!DOCTYPE":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1387 self._doctype = [] # inside a doctype declaration
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1388 elif self._doctype is not None:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1389 # parse doctype contents
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1390 if prefix == ">":
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1391 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1392 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1393 text = text.strip()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1394 if not text:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1395 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1396 self._doctype.append(text)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1397 n = len(self._doctype)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1398 if n > 2:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1399 type = self._doctype[1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1400 if type == "PUBLIC" and n == 4:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1401 name, type, pubid, system = self._doctype
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1402 elif type == "SYSTEM" and n == 3:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1403 name, type, system = self._doctype
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1404 pubid = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1405 else:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1406 return
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1407 if pubid:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1408 pubid = pubid[1:-1]
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1409 if hasattr(self.target, "doctype"):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1410 self.target.doctype(name, pubid, system[1:-1])
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1411 self._doctype = None
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1412
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1413 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1414 # Feeds data to the parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1415 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1416 # @param data Encoded data.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1417
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1418 def feed(self, data):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1419 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1420 self._parser.Parse(data, 0)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1421 except self._error, v:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1422 self._raiseerror(v)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1423
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1424 ##
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1425 # Finishes feeding data to the parser.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1426 #
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1427 # @return An element structure.
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1428 # @defreturn Element
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1429
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1430 def close(self):
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1431 try:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1432 self._parser.Parse("", 1) # end of data
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1433 except self._error, v:
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1434 self._raiseerror(v)
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1435 tree = self.target.close()
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1436 del self.target, self._parser # get rid of circular references
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1437 return tree
5169fce2d144 Import ElementTree (1.3a3-20070912-preview).
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1438