blob: 21b2d6cb61f10cdf807508bf14efd5f31a42dc98 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`xml.dom.minidom` --- Lightweight DOM implementation
3=========================================================
4
5.. module:: xml.dom.minidom
6 :synopsis: Lightweight Document Object Model (DOM) implementation.
7.. moduleauthor:: Paul Prescod <paul@prescod.net>
8.. sectionauthor:: Paul Prescod <paul@prescod.net>
9.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
10
11
12.. versionadded:: 2.0
13
14:mod:`xml.dom.minidom` is a light-weight implementation of the Document Object
15Model interface. It is intended to be simpler than the full DOM and also
16significantly smaller.
17
18DOM applications typically start by parsing some XML into a DOM. With
19:mod:`xml.dom.minidom`, this is done through the parse functions::
20
21 from xml.dom.minidom import parse, parseString
22
23 dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name
24
25 datasource = open('c:\\temp\\mydata.xml')
26 dom2 = parse(datasource) # parse an open file
27
28 dom3 = parseString('<myxml>Some data<empty/> some more data</myxml>')
29
30The :func:`parse` function can take either a filename or an open file object.
31
32
33.. function:: parse(filename_or_file, parser)
34
35 Return a :class:`Document` from the given input. *filename_or_file* may be
36 either a file name, or a file-like object. *parser*, if given, must be a SAX2
37 parser object. This function will change the document handler of the parser and
38 activate namespace support; other parser configuration (like setting an entity
39 resolver) must have been done in advance.
40
41If you have XML in a string, you can use the :func:`parseString` function
42instead:
43
44
45.. function:: parseString(string[, parser])
46
47 Return a :class:`Document` that represents the *string*. This method creates a
48 :class:`StringIO` object for the string and passes that on to :func:`parse`.
49
50Both functions return a :class:`Document` object representing the content of the
51document.
52
53What the :func:`parse` and :func:`parseString` functions do is connect an XML
54parser with a "DOM builder" that can accept parse events from any SAX parser and
55convert them into a DOM tree. The name of the functions are perhaps misleading,
56but are easy to grasp when learning the interfaces. The parsing of the document
57will be completed before these functions return; it's simply that these
58functions do not provide a parser implementation themselves.
59
60You can also create a :class:`Document` by calling a method on a "DOM
61Implementation" object. You can get this object either by calling the
62:func:`getDOMImplementation` function in the :mod:`xml.dom` package or the
63:mod:`xml.dom.minidom` module. Using the implementation from the
64:mod:`xml.dom.minidom` module will always return a :class:`Document` instance
65from the minidom implementation, while the version from :mod:`xml.dom` may
66provide an alternate implementation (this is likely if you have the `PyXML
67package <http://pyxml.sourceforge.net/>`_ installed). Once you have a
68:class:`Document`, you can add child nodes to it to populate the DOM::
69
70 from xml.dom.minidom import getDOMImplementation
71
72 impl = getDOMImplementation()
73
74 newdoc = impl.createDocument(None, "some_tag", None)
75 top_element = newdoc.documentElement
76 text = newdoc.createTextNode('Some textual content.')
77 top_element.appendChild(text)
78
79Once you have a DOM document object, you can access the parts of your XML
80document through its properties and methods. These properties are defined in
81the DOM specification. The main property of the document object is the
82:attr:`documentElement` property. It gives you the main element in the XML
83document: the one that holds all others. Here is an example program::
84
85 dom3 = parseString("<myxml>Some data</myxml>")
86 assert dom3.documentElement.tagName == "myxml"
87
88When you are finished with a DOM, you should clean it up. This is necessary
89because some versions of Python do not support garbage collection of objects
90that refer to each other in a cycle. Until this restriction is removed from all
91versions of Python, it is safest to write your code as if cycles would not be
92cleaned up.
93
94The way to clean up a DOM is to call its :meth:`unlink` method::
95
96 dom1.unlink()
97 dom2.unlink()
98 dom3.unlink()
99
100:meth:`unlink` is a :mod:`xml.dom.minidom`\ -specific extension to the DOM API.
101After calling :meth:`unlink` on a node, the node and its descendants are
102essentially useless.
103
104
105.. seealso::
106
107 `Document Object Model (DOM) Level 1 Specification <http://www.w3.org/TR/REC-DOM-Level-1/>`_
108 The W3C recommendation for the DOM supported by :mod:`xml.dom.minidom`.
109
110
111.. _minidom-objects:
112
113DOM Objects
114-----------
115
116The definition of the DOM API for Python is given as part of the :mod:`xml.dom`
117module documentation. This section lists the differences between the API and
118:mod:`xml.dom.minidom`.
119
120
121.. method:: Node.unlink()
122
123 Break internal references within the DOM so that it will be garbage collected on
124 versions of Python without cyclic GC. Even when cyclic GC is available, using
125 this can make large amounts of memory available sooner, so calling this on DOM
126 objects as soon as they are no longer needed is good practice. This only needs
127 to be called on the :class:`Document` object, but may be called on child nodes
128 to discard children of that node.
129
130
131.. method:: Node.writexml(writer[,indent=""[,addindent=""[,newl=""]]])
132
133 Write XML to the writer object. The writer should have a :meth:`write` method
134 which matches that of the file object interface. The *indent* parameter is the
135 indentation of the current node. The *addindent* parameter is the incremental
136 indentation to use for subnodes of the current one. The *newl* parameter
137 specifies the string to use to terminate newlines.
138
139 .. versionchanged:: 2.1
140 The optional keyword parameters *indent*, *addindent*, and *newl* were added to
141 support pretty output.
142
143 .. versionchanged:: 2.3
Mark Summerfield43da35d2008-03-17 08:28:15 +0000144 For the :class:`Document` node, an additional keyword argument
145 *encoding* [1]_ can be
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146 used to specify the encoding field of the XML header.
147
148
149.. method:: Node.toxml([encoding])
150
151 Return the XML that the DOM represents as a string.
152
153 With no argument, the XML header does not specify an encoding, and the result is
154 Unicode string if the default encoding cannot represent all characters in the
155 document. Encoding this string in an encoding other than UTF-8 is likely
156 incorrect, since UTF-8 is the default encoding of XML.
157
Mark Summerfield43da35d2008-03-17 08:28:15 +0000158 With an explicit *encoding* [1]_ argument, the result is a byte string in the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000159 specified encoding. It is recommended that this argument is always specified. To
160 avoid :exc:`UnicodeError` exceptions in case of unrepresentable text data, the
161 encoding argument should be specified as "utf-8".
162
163 .. versionchanged:: 2.3
164 the *encoding* argument was introduced.
165
166
167.. method:: Node.toprettyxml([indent[, newl]])
168
169 Return a pretty-printed version of the document. *indent* specifies the
170 indentation string and defaults to a tabulator; *newl* specifies the string
171 emitted at the end of each line and defaults to ``\n``.
172
173 .. versionadded:: 2.1
174
175 .. versionchanged:: 2.3
Mark Summerfield43da35d2008-03-17 08:28:15 +0000176 the encoding [1]_ argument; see :meth:`toxml`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000177
178The following standard DOM methods have special considerations with
179:mod:`xml.dom.minidom`:
180
181
182.. method:: Node.cloneNode(deep)
183
184 Although this method was present in the version of :mod:`xml.dom.minidom`
185 packaged with Python 2.0, it was seriously broken. This has been corrected for
186 subsequent releases.
187
188
189.. _dom-example:
190
191DOM Example
192-----------
193
194This example program is a fairly realistic example of a simple program. In this
195particular case, we do not take much advantage of the flexibility of the DOM.
196
197.. literalinclude:: ../includes/minidom-example.py
198
199
200.. _minidom-and-dom:
201
202minidom and the DOM standard
203----------------------------
204
205The :mod:`xml.dom.minidom` module is essentially a DOM 1.0-compatible DOM with
206some DOM 2 features (primarily namespace features).
207
208Usage of the DOM interface in Python is straight-forward. The following mapping
209rules apply:
210
211* Interfaces are accessed through instance objects. Applications should not
212 instantiate the classes themselves; they should use the creator functions
213 available on the :class:`Document` object. Derived interfaces support all
214 operations (and attributes) from the base interfaces, plus any new operations.
215
216* Operations are used as methods. Since the DOM uses only :keyword:`in`
217 parameters, the arguments are passed in normal order (from left to right).
Georg Brandlb19be572007-12-29 10:57:00 +0000218 There are no optional arguments. ``void`` operations return ``None``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000219
220* IDL attributes map to instance attributes. For compatibility with the OMG IDL
221 language mapping for Python, an attribute ``foo`` can also be accessed through
Georg Brandlb19be572007-12-29 10:57:00 +0000222 accessor methods :meth:`_get_foo` and :meth:`_set_foo`. ``readonly``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000223 attributes must not be changed; this is not enforced at runtime.
224
225* The types ``short int``, ``unsigned int``, ``unsigned long long``, and
226 ``boolean`` all map to Python integer objects.
227
228* The type ``DOMString`` maps to Python strings. :mod:`xml.dom.minidom` supports
229 either byte or Unicode strings, but will normally produce Unicode strings.
230 Values of type ``DOMString`` may also be ``None`` where allowed to have the IDL
231 ``null`` value by the DOM specification from the W3C.
232
Georg Brandlb19be572007-12-29 10:57:00 +0000233* ``const`` declarations map to variables in their respective scope (e.g.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000234 ``xml.dom.minidom.Node.PROCESSING_INSTRUCTION_NODE``); they must not be changed.
235
236* ``DOMException`` is currently not supported in :mod:`xml.dom.minidom`.
237 Instead, :mod:`xml.dom.minidom` uses standard Python exceptions such as
238 :exc:`TypeError` and :exc:`AttributeError`.
239
240* :class:`NodeList` objects are implemented using Python's built-in list type.
241 Starting with Python 2.2, these objects provide the interface defined in the DOM
242 specification, but with earlier versions of Python they do not support the
243 official API. They are, however, much more "Pythonic" than the interface
244 defined in the W3C recommendations.
245
246The following interfaces have no implementation in :mod:`xml.dom.minidom`:
247
248* :class:`DOMTimeStamp`
249
250* :class:`DocumentType` (added in Python 2.1)
251
252* :class:`DOMImplementation` (added in Python 2.1)
253
254* :class:`CharacterData`
255
256* :class:`CDATASection`
257
258* :class:`Notation`
259
260* :class:`Entity`
261
262* :class:`EntityReference`
263
264* :class:`DocumentFragment`
265
266Most of these reflect information in the XML document that is not of general
267utility to most DOM users.
268
Mark Summerfield43da35d2008-03-17 08:28:15 +0000269.. rubric:: Footnotes
270
271.. [#] The encoding string included in XML output should conform to the
272 appropriate standards. For example, "UTF-8" is valid, but "UTF8" is
273 not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl
274 and http://www.iana.org/assignments/character-sets .