blob: 74c92fb07f15fa6613c2c3fd9e22b39eaf6aa7a5 [file] [log] [blame]
Fred Drakeeaf57aa2000-11-29 06:10:22 +00001\section{\module{xml.dom} ---
2 The Document Object Model API}
Fred Drake669d36f2000-10-24 02:34:45 +00003
Fred Drakeeaf57aa2000-11-29 06:10:22 +00004\declaremodule{standard}{xml.dom}
5\modulesynopsis{Document Object Model API for Python.}
Fred Drake669d36f2000-10-24 02:34:45 +00006\sectionauthor{Paul Prescod}{paul@prescod.net}
7\sectionauthor{Martin v. L\"owis}{loewis@informatik.hu-berlin.de}
8
9\versionadded{2.0}
10
Fred Drakeeaf57aa2000-11-29 06:10:22 +000011The Document Object Model, or ``DOM,'' is a cross-language API from
12the World Wide Web Consortium (W3C) for accessing and modifying XML
13documents. A DOM implementation presents an XML document as a tree
14structure, or allows client code to build such a structure from
15scratch. It then gives access to the structure through a set of
16objects which provided well-known interfaces.
Fred Drake669d36f2000-10-24 02:34:45 +000017
Fred Drakeeaf57aa2000-11-29 06:10:22 +000018The DOM is extremely useful for random-access applications. SAX only
19allows you a view of one bit of the document at a time. If you are
20looking at one SAX element, you have no access to another. If you are
21looking at a text node, you have no access to a containing element.
22When you write a SAX application, you need to keep track of your
23program's position in the document somewhere in your own code. SAX
24does not do it for you. Also, if you need to look ahead in the XML
25document, you are just out of luck.
Fred Drake669d36f2000-10-24 02:34:45 +000026
27Some applications are simply impossible in an event driven model with
Fred Drakeeaf57aa2000-11-29 06:10:22 +000028no access to a tree. Of course you could build some sort of tree
Fred Drake669d36f2000-10-24 02:34:45 +000029yourself in SAX events, but the DOM allows you to avoid writing that
Fred Drakeeaf57aa2000-11-29 06:10:22 +000030code. The DOM is a standard tree representation for XML data.
Fred Drake669d36f2000-10-24 02:34:45 +000031
Fred Drakeeaf57aa2000-11-29 06:10:22 +000032%What if your needs are somewhere between SAX and the DOM? Perhaps
33%you cannot afford to load the entire tree in memory but you find the
34%SAX model somewhat cumbersome and low-level. There is also a module
35%called xml.dom.pulldom that allows you to build trees of only the
36%parts of a document that you need structured access to. It also has
37%features that allow you to find your way around the DOM.
Fred Drake669d36f2000-10-24 02:34:45 +000038% See http://www.prescod.net/python/pulldom
39
Fred Drakeeaf57aa2000-11-29 06:10:22 +000040The Document Object Model is being defined by the W3C in stages, or
41``levels'' in their terminology. The Python mapping of the API is
42substantially based on the DOM Level 2 recommendation. Some aspects
Fred Drake66f98b42001-01-26 20:51:32 +000043of the API will only become available in Python 2.1, or may only be
Fred Drakeeaf57aa2000-11-29 06:10:22 +000044available in particular DOM implementations.
Fred Drake669d36f2000-10-24 02:34:45 +000045
Fred Drakeeaf57aa2000-11-29 06:10:22 +000046DOM applications typically start by parsing some XML into a DOM. How
47this is accomplished is not covered at all by DOM Level 1, and Level 2
48provides only limited improvements. There is a
49\class{DOMImplementation} object class which provides access to
50\class{Document} creation methods, but these methods were only added
51in DOM Level 2 and were not implemented in time for Python 2.0. There
Fred Drake66f98b42001-01-26 20:51:32 +000052is also no well-defined way to access these methods without an
Fred Drakeeaf57aa2000-11-29 06:10:22 +000053existing \class{Document} object. For Python 2.0, consult the
54documentation for each particular DOM implementation to determine the
55bootstrap procedure needed to create and initialize \class{Document}
Fred Drake66f98b42001-01-26 20:51:32 +000056and \class{DocumentType} instances.
Fred Drake669d36f2000-10-24 02:34:45 +000057
58Once you have a DOM document object, you can access the parts of your
59XML document through its properties and methods. These properties are
Fred Drakeeaf57aa2000-11-29 06:10:22 +000060defined in the DOM specification; this portion of the reference manual
61describes the interpretation of the specification in Python.
Fred Drake669d36f2000-10-24 02:34:45 +000062
Fred Drakeeaf57aa2000-11-29 06:10:22 +000063The specification provided by the W3C defines the DOM API for Java,
64ECMAScript, and OMG IDL. The Python mapping defined here is based in
65large part on the IDL version of the specification, but strict
66compliance is not required (though implementations are free to support
67the strict mapping from IDL). See section \ref{dom-conformance},
68``Conformance,'' for a detailed discussion of mapping requirements.
Fred Drake669d36f2000-10-24 02:34:45 +000069
Fred Drake669d36f2000-10-24 02:34:45 +000070
71\begin{seealso}
Fred Drakeeaf57aa2000-11-29 06:10:22 +000072 \seetitle[http://www.w3.org/TR/DOM-Level-2-Core/]{Document Object
73 Model (DOM) Level 2 Specification}
74 {The W3C recommendation upon which the Python DOM API is
75 based.}
76 \seetitle[http://www.w3.org/TR/REC-DOM-Level-1/]{Document Object
77 Model (DOM) Level 1 Specification}
78 {The W3C recommendation for the
Fred Drake669d36f2000-10-24 02:34:45 +000079 DOM supported by \module{xml.dom.minidom}.}
80 \seetitle[http://pyxml.sourceforge.net]{PyXML}{Users that require a
81 full-featured implementation of DOM should use the PyXML
82 package.}
Fred Drakeeaf57aa2000-11-29 06:10:22 +000083 \seetitle[http://cgi.omg.org/cgi-bin/doc?orbos/99-08-02.pdf]{CORBA
84 Scripting with Python}
85 {This specifies the mapping from OMG IDL to Python.}
Fred Drake669d36f2000-10-24 02:34:45 +000086\end{seealso}
87
88
Fred Drakeeaf57aa2000-11-29 06:10:22 +000089\subsection{Objects in the DOM \label{dom-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +000090
91The definitive documentation for the DOM is the DOM specification from
Fred Drake16942f22000-12-07 04:47:51 +000092the W3C.
Fred Drake669d36f2000-10-24 02:34:45 +000093
Fred Drakeeaf57aa2000-11-29 06:10:22 +000094Note that DOM attributes may also be manipulated as nodes instead of
95as simple strings. It is fairly rare that you must do this, however,
96so this usage is not yet documented.
97
98
99\begin{tableiii}{l|l|l}{class}{Interface}{Section}{Purpose}
Fred Drake16942f22000-12-07 04:47:51 +0000100 \lineiii{DOMImplementation}{\ref{dom-implementation-objects}}
101 {Interface to the underlying implementation.}
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000102 \lineiii{Node}{\ref{dom-node-objects}}
103 {Base interface for most objects in a document.}
Fred Drake16942f22000-12-07 04:47:51 +0000104 \lineiii{NodeList}{\ref{dom-nodelist-objects}}
105 {Interface for a sequence of nodes.}
106 \lineiii{DocumentType}{\ref{dom-documenttype-objects}}
107 {Information about the declarations needed to process a document.}
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000108 \lineiii{Document}{\ref{dom-document-objects}}
109 {Object which represents an entire document.}
110 \lineiii{Element}{\ref{dom-element-objects}}
111 {Element nodes in the document hierarchy.}
112 \lineiii{Attr}{\ref{dom-attr-objects}}
113 {Attribute value nodes on element nodes.}
114 \lineiii{Comment}{\ref{dom-comment-objects}}
115 {Representation of comments in the source document.}
116 \lineiii{Text}{\ref{dom-text-objects}}
117 {Nodes containing textual content from the document.}
118 \lineiii{ProcessingInstruction}{\ref{dom-pi-objects}}
119 {Processing instruction representation.}
120\end{tableiii}
121
Fred Drakebc9c1b12000-12-13 17:38:02 +0000122An additional section describes the exceptions defined for working
123with the DOM in Python.
124
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000125
Fred Drake16942f22000-12-07 04:47:51 +0000126\subsubsection{DOMImplementation Objects
127 \label{dom-implementation-objects}}
128
129The \class{DOMImplementation} interface provides a way for
130applications to determine the availability of particular features in
131the DOM they are using. DOM Level 2 added the ability to create new
132\class{Document} and \class{DocumentType} objects using the
133\class{DOMImplementation} as well.
134
135\begin{methoddesc}[DOMImplementation]{hasFeature}{feature, version}
136\end{methoddesc}
137
138
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000139\subsubsection{Node Objects \label{dom-node-objects}}
140
Fred Drake669d36f2000-10-24 02:34:45 +0000141All of the components of an XML document are subclasses of
142\class{Node}.
143
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000144\begin{memberdesc}[Node]{nodeType}
Fred Drake669d36f2000-10-24 02:34:45 +0000145An integer representing the node type. Symbolic constants for the
Fred Drake66f98b42001-01-26 20:51:32 +0000146types are on the \class{Node} object:
Fred Drake669d36f2000-10-24 02:34:45 +0000147\constant{ELEMENT_NODE}, \constant{ATTRIBUTE_NODE},
148\constant{TEXT_NODE}, \constant{CDATA_SECTION_NODE},
149\constant{ENTITY_NODE}, \constant{PROCESSING_INSTRUCTION_NODE},
150\constant{COMMENT_NODE}, \constant{DOCUMENT_NODE},
151\constant{DOCUMENT_TYPE_NODE}, \constant{NOTATION_NODE}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000152This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000153\end{memberdesc}
154
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000155\begin{memberdesc}[Node]{parentNode}
Fred Drake16942f22000-12-07 04:47:51 +0000156The parent of the current node, or \code{None} for the document node.
157The value is always a \class{Node} object or \code{None}. For
158\class{Element} nodes, this will be the parent element, except for the
159root element, in which case it will be the \class{Document} object.
160For \class{Attr} nodes, this is always \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000161This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000162\end{memberdesc}
163
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000164\begin{memberdesc}[Node]{attributes}
Fred Drake9368a122001-01-24 18:19:40 +0000165A \class{NamedNodeList} of attribute objects. Only elements have
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000166actual values for this; others provide \code{None} for this attribute.
Fred Drake9a29dd62000-12-08 06:54:51 +0000167This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000168\end{memberdesc}
169
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000170\begin{memberdesc}[Node]{previousSibling}
Fred Drake669d36f2000-10-24 02:34:45 +0000171The node that immediately precedes this one with the same parent. For
172instance the element with an end-tag that comes just before the
173\var{self} element's start-tag. Of course, XML documents are made
174up of more than just elements so the previous sibling could be text, a
Fred Drake16942f22000-12-07 04:47:51 +0000175comment, or something else. If this node is the first child of the
176parent, this attribute will be \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000177This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000178\end{memberdesc}
179
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000180\begin{memberdesc}[Node]{nextSibling}
Fred Drake669d36f2000-10-24 02:34:45 +0000181The node that immediately follows this one with the same parent. See
Fred Drake16942f22000-12-07 04:47:51 +0000182also \member{previousSibling}. If this is the last child of the
183parent, this attribute will be \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000184This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000185\end{memberdesc}
186
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000187\begin{memberdesc}[Node]{childNodes}
Fred Drake669d36f2000-10-24 02:34:45 +0000188A list of nodes contained within this node.
Fred Drake9a29dd62000-12-08 06:54:51 +0000189This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000190\end{memberdesc}
191
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000192\begin{memberdesc}[Node]{firstChild}
193The first child of the node, if there are any, or \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000194This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000195\end{memberdesc}
196
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000197\begin{memberdesc}[Node]{lastChild}
198The last child of the node, if there are any, or \code{None}.
Fred Drake9a29dd62000-12-08 06:54:51 +0000199This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000200\end{memberdesc}
201
Fred Drake9a29dd62000-12-08 06:54:51 +0000202\begin{memberdesc}[Node]{localName}
203The part of the \member{tagName} following the colon if there is one,
204else the entire \member{tagName}. The value is a string.
205\end{memberdesc}
206
207\begin{memberdesc}[Node]{prefix}
208The part of the \member{tagName} preceding the colon if there is one,
209else the empty string. The value is a string, or \code{None}
210\end{memberdesc}
211
212\begin{memberdesc}[Node]{namespaceURI}
Fred Drake16942f22000-12-07 04:47:51 +0000213The namespace associated with the element name. This will be a
Fred Drake9a29dd62000-12-08 06:54:51 +0000214string or \code{None}. This is a read-only attribute.
Fred Drake16942f22000-12-07 04:47:51 +0000215\end{memberdesc}
216
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000217\begin{memberdesc}[Node]{nodeName}
Fred Drake9a29dd62000-12-08 06:54:51 +0000218This has a different meaning for each node type; see the DOM
219specification for details. You can always get the information you
220would get here from another property such as the \member{tagName}
221property for elements or the \member{name} property for attributes.
222For all node types, the value of this attribute will be either a
223string or \code{None}. This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000224\end{memberdesc}
225
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000226\begin{memberdesc}[Node]{nodeValue}
Fred Drake9a29dd62000-12-08 06:54:51 +0000227This has a different meaning for each node type; see the DOM
228specification for details. The situation is similar to that with
229\member{nodeName}. The value is a string or \code{None}.
Fred Drake669d36f2000-10-24 02:34:45 +0000230\end{memberdesc}
231
Fred Drake9a29dd62000-12-08 06:54:51 +0000232\begin{methoddesc}[Node]{hasAttributes}{}
233Returns true if the node has any attributes.
234\end{methoddesc}
235
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000236\begin{methoddesc}[Node]{hasChildNodes}{}
237Returns true if the node has any child nodes.
Fred Drake669d36f2000-10-24 02:34:45 +0000238\end{methoddesc}
239
Fred Drake40e43bf2001-02-03 01:20:01 +0000240\begin{methoddesc}[Node]{isSameNode}{other}
241Returns true if \var{other} refers to the same node as this node.
242This is especially useful for DOM implementations which use any sort
243of proxy architecture (because more than one object can refer to the
244same node).
245\end{methoddesc}
246
Fred Drake9a29dd62000-12-08 06:54:51 +0000247\begin{methoddesc}[Node]{appendChild}{newChild}
248Add a new child node to this node at the end of the list of children,
249returning \var{newChild}.
250\end{methoddesc}
251
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000252\begin{methoddesc}[Node]{insertBefore}{newChild, refChild}
Fred Drake669d36f2000-10-24 02:34:45 +0000253Insert a new child node before an existing child. It must be the case
254that \var{refChild} is a child of this node; if not,
Fred Drake9a29dd62000-12-08 06:54:51 +0000255\exception{ValueError} is raised. \var{newChild} is returned.
Fred Drake669d36f2000-10-24 02:34:45 +0000256\end{methoddesc}
257
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000258\begin{methoddesc}[Node]{removeChild}{oldChild}
Fred Drake669d36f2000-10-24 02:34:45 +0000259Remove a child node. \var{oldChild} must be a child of this node; if
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000260not, \exception{ValueError} is raised. \var{oldChild} is returned on
261success. If \var{oldChild} will not be used further, its
262\method{unlink()} method should be called.
Fred Drake669d36f2000-10-24 02:34:45 +0000263\end{methoddesc}
264
Fred Drake9a29dd62000-12-08 06:54:51 +0000265\begin{methoddesc}[Node]{replaceChild}{newChild, oldChild}
266Replace an existing node with a new node. It must be the case that
267\var{oldChild} is a child of this node; if not,
268\exception{ValueError} is raised.
Fred Drake669d36f2000-10-24 02:34:45 +0000269\end{methoddesc}
270
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000271\begin{methoddesc}[Node]{normalize}{}
272Join adjacent text nodes so that all stretches of text are stored as
273single \class{Text} instances. This simplifies processing text from a
274DOM tree for many applications.
275\versionadded{2.1}
Fred Drake669d36f2000-10-24 02:34:45 +0000276\end{methoddesc}
277
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000278\begin{methoddesc}[Node]{cloneNode}{deep}
279Clone this node. Setting \var{deep} means to clone all child nodes as
Fred Drake16942f22000-12-07 04:47:51 +0000280well. This returns the clone.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000281\end{methoddesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000282
283
Fred Drake16942f22000-12-07 04:47:51 +0000284\subsubsection{NodeList Objects \label{dom-nodelist-objects}}
285
286A \class{NodeList} represents a sequence of nodes. These objects are
287used in two ways in the DOM Core recommendation: the
288\class{Element} objects provides one as it's list of child nodes, and
289the \method{getElementsByTagName()} and
290\method{getElementsByTagNameNS()} methods of \class{Node} return
291objects with this interface to represent query results.
292
293The DOM Level 2 recommendation defines one method and one attribute
294for these objects:
295
296\begin{methoddesc}[NodeList]{item}{i}
297 Return the \var{i}'th item from the sequence, if there is one, or
298 \code{None}. The index \var{i} is not allowed to be less then zero
299 or greater than or equal to the length of the sequence.
300\end{methoddesc}
301
302\begin{memberdesc}[NodeList]{length}
303 The number of nodes in the sequence.
304\end{memberdesc}
305
306In addition, the Python DOM interface requires that some additional
307support is provided to allow \class{NodeList} objects to be used as
308Python sequences. All \class{NodeList} implementations must include
309support for \method{__len__()} and \method{__getitem__()}; this allows
310iteration over the \class{NodeList} in \keyword{for} statements and
311proper support for the \function{len()} built-in function.
312
313If a DOM implementation supports modification of the document, the
314\class{NodeList} implementation must also support the
315\method{__setitem__()} and \method{__delitem__()} methods.
316
317
318\subsubsection{DocumentType Objects \label{dom-documenttype-objects}}
319
320Information about the notations and entities declared by a document
321(including the external subset if the parser uses it and can provide
322the information) is available from a \class{DocumentType} object. The
323\class{DocumentType} for a document is available from the
324\class{Document} object's \member{doctype} attribute.
325
326\class{DocumentType} is a specialization of \class{Node}, and adds the
327following attributes:
328
329\begin{memberdesc}[DocumentType]{publicId}
330 The public identifier for the external subset of the document type
331 definition. This will be a string or \code{None}.
332\end{memberdesc}
333
334\begin{memberdesc}[DocumentType]{systemId}
335 The system identifier for the external subset of the document type
336 definition. This will be a URI as a string, or \code{None}.
337\end{memberdesc}
338
339\begin{memberdesc}[DocumentType]{internalSubset}
340 A string giving the complete internal subset from the document.
341\end{memberdesc}
342
343\begin{memberdesc}[DocumentType]{name}
344 The name of the root element as given in the \code{DOCTYPE}
345 declaration, if present. If the was no \code{DOCTYPE} declaration,
346 this will be \code{None}.
347\end{memberdesc}
348
349\begin{memberdesc}[DocumentType]{entities}
350 This is a \class{NamedNodeMap} giving the definitions of external
351 entities. For entity names defined more than once, only the first
352 definition is provided (others are ignored as required by the XML
353 recommendation). This may be \code{None} if the information is not
354 provided by the parser, or if no entities are defined.
355\end{memberdesc}
356
357\begin{memberdesc}[DocumentType]{notations}
358 This is a \class{NamedNodeMap} giving the definitions of notations.
359 For notation names defined more than once, only the first definition
360 is provided (others are ignored as required by the XML
361 recommendation). This may be \code{None} if the information is not
362 provided by the parser, or if no notations are defined.
363\end{memberdesc}
364
365
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000366\subsubsection{Document Objects \label{dom-document-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000367
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000368A \class{Document} represents an entire XML document, including its
369constituent elements, attributes, processing instructions, comments
370etc. Remeber that it inherits properties from \class{Node}.
371
372\begin{memberdesc}[Document]{documentElement}
Fred Drake669d36f2000-10-24 02:34:45 +0000373The one and only root element of the document.
374\end{memberdesc}
375
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000376\begin{methoddesc}[Document]{createElement}{tagName}
Fred Drake16942f22000-12-07 04:47:51 +0000377Create and return a new element node. The element is not inserted
378into the document when it is created. You need to explicitly insert
379it with one of the other methods such as \method{insertBefore()} or
Fred Drake669d36f2000-10-24 02:34:45 +0000380\method{appendChild()}.
381\end{methoddesc}
382
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000383\begin{methoddesc}[Document]{createElementNS}{namespaceURI, tagName}
Fred Drake16942f22000-12-07 04:47:51 +0000384Create and return a new element with a namespace. The
385\var{tagName} may have a prefix. The element is not inserted into the
386document when it is created. You need to explicitly insert it with
387one of the other methods such as \method{insertBefore()} or
388\method{appendChild()}.
Fred Drake669d36f2000-10-24 02:34:45 +0000389\end{methoddesc}
390
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000391\begin{methoddesc}[Document]{createTextNode}{data}
Fred Drake16942f22000-12-07 04:47:51 +0000392Create and return a text node containing the data passed as a
393parameter. As with the other creation methods, this one does not
394insert the node into the tree.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000395\end{methoddesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000396
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000397\begin{methoddesc}[Document]{createComment}{data}
Fred Drake16942f22000-12-07 04:47:51 +0000398Create and return a comment node containing the data passed as a
399parameter. As with the other creation methods, this one does not
400insert the node into the tree.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000401\end{methoddesc}
402
403\begin{methoddesc}[Document]{createProcessingInstruction}{target, data}
Fred Drake16942f22000-12-07 04:47:51 +0000404Create and return a processing instruction node containing the
405\var{target} and \var{data} passed as parameters. As with the other
406creation methods, this one does not insert the node into the tree.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000407\end{methoddesc}
408
409\begin{methoddesc}[Document]{createAttribute}{name}
Fred Drake16942f22000-12-07 04:47:51 +0000410Create and return an attribute node. This method does not associate
411the attribute node with any particular element. You must use
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000412\method{setAttributeNode()} on the appropriate \class{Element} object
413to use the newly created attribute instance.
414\end{methoddesc}
415
416\begin{methoddesc}[Document]{createAttributeNS}{namespaceURI, qualifiedName}
Fred Drake16942f22000-12-07 04:47:51 +0000417Create and return an attribute node with a namespace. The
418\var{tagName} may have a prefix. This method does not associate the
419attribute node with any particular element. You must use
420\method{setAttributeNode()} on the appropriate \class{Element} object
421to use the newly created attribute instance.
Fred Drake669d36f2000-10-24 02:34:45 +0000422\end{methoddesc}
423
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000424\begin{methoddesc}[Document]{getElementsByTagName}{tagName}
Fred Drake669d36f2000-10-24 02:34:45 +0000425Search for all descendants (direct children, children's children,
426etc.) with a particular element type name.
427\end{methoddesc}
428
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000429\begin{methoddesc}[Document]{getElementsByTagNameNS}{namespaceURI, localName}
Fred Drake669d36f2000-10-24 02:34:45 +0000430Search for all descendants (direct children, children's children,
431etc.) with a particular namespace URI and localname. The localname is
432the part of the namespace after the prefix.
433\end{methoddesc}
434
Fred Drake669d36f2000-10-24 02:34:45 +0000435
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000436\subsubsection{Element Objects \label{dom-element-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000437
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000438\class{Element} is a subclass of \class{Node}, so inherits all the
439attributes of that class.
440
441\begin{memberdesc}[Element]{tagName}
Fred Drake669d36f2000-10-24 02:34:45 +0000442The element type name. In a namespace-using document it may have
Fred Drake16942f22000-12-07 04:47:51 +0000443colons in it. The value is a string.
Fred Drake669d36f2000-10-24 02:34:45 +0000444\end{memberdesc}
445
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000446\begin{methoddesc}[Element]{getElementsByTagName}{tagName}
Fred Drake669d36f2000-10-24 02:34:45 +0000447Same as equivalent method in the \class{Document} class.
448\end{methoddesc}
449
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000450\begin{methoddesc}[Element]{getElementsByTagNameNS}{tagName}
Fred Drake669d36f2000-10-24 02:34:45 +0000451Same as equivalent method in the \class{Document} class.
452\end{methoddesc}
453
Fred Drake9a29dd62000-12-08 06:54:51 +0000454\begin{methoddesc}[Element]{getAttribute}{attname}
455Return an attribute value as a string.
456\end{methoddesc}
457
458\begin{methoddesc}[Element]{getAttributeNode}{attrname}
459Return the \class{Attr} node for the attribute named by
460\var{attrname}.
461\end{methoddesc}
462
463\begin{methoddesc}[Element]{getAttributeNS}{namespaceURI, localName}
464Return an attribute value as a string, given a \var{namespaceURI} and
465\var{localName}.
466\end{methoddesc}
467
468\begin{methoddesc}[Element]{getAttributeNodeNS}{namespaceURI, localName}
469Return an attribute value as a node, given a \var{namespaceURI} and
470\var{localName}.
471\end{methoddesc}
472
473\begin{methoddesc}[Element]{removeAttribute}{attname}
474Remove an attribute by name. No exception is raised if there is no
475matching attribute.
476\end{methoddesc}
477
478\begin{methoddesc}[Element]{removeAttributeNode}{oldAttr}
479Remove and return \var{oldAttr} from the attribute list, if present.
480If \var{oldAttr} is not present, \exception{NotFoundErr} is raised.
481\end{methoddesc}
482
483\begin{methoddesc}[Element]{removeAttributeNS}{namespaceURI, localName}
484Remove an attribute by name. Note that it uses a localName, not a
485qname. No exception is raised if there is no matching attribute.
486\end{methoddesc}
487
488\begin{methoddesc}[Element]{setAttribute}{attname, value}
489Set an attribute value from a string.
490\end{methoddesc}
491
492\begin{methoddesc}[Element]{setAttributeNode}{newAttr}
493Add a new attibute node to the element, replacing an existing
494attribute if necessary if the \member{name} attribute matches. If a
495replacement occurs, the old attribute node will be returned. If
496\var{newAttr} is already in use, \exception{InuseAttributeErr} will be
497raised.
498\end{methoddesc}
499
500\begin{methoddesc}[Element]{setAttributeNodeNS}{newAttr}
501Add a new attibute node to the element, replacing an existing
502attribute if necessary if the \member{namespaceURI} and
503\member{localName} attributes match. If a replacement occurs, the old
504attribute node will be returned. If \var{newAttr} is already in use,
505\exception{InuseAttributeErr} will be raised.
506\end{methoddesc}
507
508\begin{methoddesc}[Element]{setAttributeNS}{namespaceURI, qname, value}
509Set an attribute value from a string, given a \var{namespaceURI} and a
510\var{qname}. Note that a qname is the whole attribute name. This is
511different than above.
512\end{methoddesc}
513
Fred Drake669d36f2000-10-24 02:34:45 +0000514
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000515\subsubsection{Attr Objects \label{dom-attr-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000516
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000517\class{Attr} inherits from \class{Node}, so inherits all its
518attributes.
Fred Drake669d36f2000-10-24 02:34:45 +0000519
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000520\begin{memberdesc}[Attr]{name}
Fred Drake669d36f2000-10-24 02:34:45 +0000521The attribute name. In a namespace-using document it may have colons
522in it.
523\end{memberdesc}
524
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000525\begin{memberdesc}[Attr]{localName}
Fred Drake669d36f2000-10-24 02:34:45 +0000526The part of the name following the colon if there is one, else the
Fred Drake9a29dd62000-12-08 06:54:51 +0000527entire name. This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000528\end{memberdesc}
529
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000530\begin{memberdesc}[Attr]{prefix}
Fred Drake669d36f2000-10-24 02:34:45 +0000531The part of the name preceding the colon if there is one, else the
532empty string.
533\end{memberdesc}
534
Fred Drake669d36f2000-10-24 02:34:45 +0000535
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000536\subsubsection{NamedNodeMap Objects \label{dom-attributelist-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000537
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000538\class{NamedNodeMap} does \emph{not} inherit from \class{Node}.
Fred Drake669d36f2000-10-24 02:34:45 +0000539
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000540\begin{memberdesc}[NamedNodeMap]{length}
Fred Drake669d36f2000-10-24 02:34:45 +0000541The length of the attribute list.
542\end{memberdesc}
543
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000544\begin{methoddesc}[NamedNodeMap]{item}{index}
Fred Drake669d36f2000-10-24 02:34:45 +0000545Return an attribute with a particular index. The order you get the
546attributes in is arbitrary but will be consistent for the life of a
547DOM. Each item is an attribute node. Get its value with the
548\member{value} attribbute.
549\end{methoddesc}
550
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000551There are also experimental methods that give this class more mapping
552behavior. You can use them or you can use the standardized
553\method{getAttribute*()}-family methods on the \class{Element} objects.
Fred Drake669d36f2000-10-24 02:34:45 +0000554
555
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000556\subsubsection{Comment Objects \label{dom-comment-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000557
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000558\class{Comment} represents a comment in the XML document. It is a
Fred Drake9a29dd62000-12-08 06:54:51 +0000559subclass of \class{Node}, but cannot have child nodes.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000560
561\begin{memberdesc}[Comment]{data}
Fred Drake9a29dd62000-12-08 06:54:51 +0000562The content of the comment as a string. The attribute contains all
563characters between the leading \code{<!-}\code{-} and trailing
564\code{-}\code{->}, but does not include them.
Fred Drake669d36f2000-10-24 02:34:45 +0000565\end{memberdesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000566
567
Fred Drake9a29dd62000-12-08 06:54:51 +0000568\subsubsection{Text and CDATASection Objects \label{dom-text-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000569
Fred Drake9a29dd62000-12-08 06:54:51 +0000570The \class{Text} interface represents text in the XML document. If
571the parser and DOM implementation support the DOM's XML extension,
572portions of the text enclosed in CDATA marked sections are stored in
573\class{CDATASection} objects. These two interfaces are identical, but
574provide different values for the \member{nodeType} attribute.
575
576These interfaces extend the \class{Node} interface. They cannot have
577child nodes.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000578
579\begin{memberdesc}[Text]{data}
Fred Drake9a29dd62000-12-08 06:54:51 +0000580The content of the text node as a string.
Fred Drake669d36f2000-10-24 02:34:45 +0000581\end{memberdesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000582
Fred Drake9a29dd62000-12-08 06:54:51 +0000583\strong{Note:} The use of a \class{CDATASection} node does not
584indicate that the node represents a complete CDATA marked section,
585only that the content of the node was part of a CDATA section. A
586single CDATA section may be represented by more than one node in the
587document tree. There is no way to determine whether two adjacent
588\class{CDATASection} nodes represent different CDATA marked sections.
589
Fred Drake669d36f2000-10-24 02:34:45 +0000590
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000591\subsubsection{ProcessingInstruction Objects \label{dom-pi-objects}}
Fred Drake669d36f2000-10-24 02:34:45 +0000592
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000593Represents a processing instruction in the XML document; this inherits
Fred Drake9a29dd62000-12-08 06:54:51 +0000594from the \class{Node} interface and cannot have child nodes.
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000595
596\begin{memberdesc}[ProcessingInstruction]{target}
Fred Drake669d36f2000-10-24 02:34:45 +0000597The content of the processing instruction up to the first whitespace
Fred Drake9a29dd62000-12-08 06:54:51 +0000598character. This is a read-only attribute.
Fred Drake669d36f2000-10-24 02:34:45 +0000599\end{memberdesc}
600
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000601\begin{memberdesc}[ProcessingInstruction]{data}
Fred Drake669d36f2000-10-24 02:34:45 +0000602The content of the processing instruction following the first
603whitespace character.
604\end{memberdesc}
Fred Drake669d36f2000-10-24 02:34:45 +0000605
606
Fred Drakebc9c1b12000-12-13 17:38:02 +0000607\subsubsection{Exceptions \label{dom-exceptions}}
608
609\versionadded{2.1}
610
611The DOM Level 2 recommendation defines a single exception,
612\exception{DOMException}, and a number of constants that allow
613applications to determine what sort of error occurred.
614\exception{DOMException} instances carry a \member{code} attribute
615that provides the appropriate value for the specific exception.
616
617The Python DOM interface provides the constants, but also expands the
618set of exceptions so that a specific exception exists for each of the
619exception codes defined by the DOM. The implementations must raise
620the appropriate specific exception, each of which carries the
621appropriate value for the \member{code} attribute.
622
623\begin{excdesc}{DOMException}
624 Base exception class used for all specific DOM exceptions. This
625 exception class cannot be directly instantiated.
626\end{excdesc}
627
628\begin{excdesc}{DomstringSizeErr}
629 Raised when a specified range of text does not fit into a string.
630 This is not known to be used in the Python DOM implementations, but
631 may be received from DOM implementations not written in Python.
632\end{excdesc}
633
634\begin{excdesc}{HierarchyRequestErr}
635 Raised when an attempt is made to insert a node where the node type
636 is not allowed.
637\end{excdesc}
638
639\begin{excdesc}{IndexSizeErr}
640 Raised when an index or size parameter to a method is negative or
641 exceeds the allowed values.
642\end{excdesc}
643
644\begin{excdesc}{InuseAttributeErr}
645 Raised when an attempt is made to insert an \class{Attr} node that
646 is already present elsewhere in the document.
647\end{excdesc}
648
649\begin{excdesc}{InvalidAccessErr}
650 Raised if a parameter or an operation is not supported on the
651 underlying object.
652\end{excdesc}
653
654\begin{excdesc}{InvalidCharacterErr}
655 This exception is raised when a string parameter contains a
656 character that is not permitted in the context it's being used in by
657 the XML 1.0 recommendation. For example, attempting to create an
658 \class{Element} node with a space in the element type name will
659 cause this error to be raised.
660\end{excdesc}
661
662\begin{excdesc}{InvalidModificationErr}
663 Raised when an attempt is made to modify the type of a node.
664\end{excdesc}
665
666\begin{excdesc}{InvalidStateErr}
667 Raised when an attempt is made to use an object that is not or is no
668 longer usable.
669\end{excdesc}
670
671\begin{excdesc}{NamespaceErr}
672 If an attempt is made to change any object in a way that is not
673 permitted with regard to the
674 \citetitle[http://www.w3.org/TR/REC-xml-names/]{Namespaces in XML}
675 recommendation, this exception is raised.
676\end{excdesc}
677
678\begin{excdesc}{NotFoundErr}
679 Exception when a node does not exist in the referenced context. For
680 example, \method{NamedNodeMap.removeNamedItem()} will raise this if
681 the node passed in does not exist in the map.
682\end{excdesc}
683
684\begin{excdesc}{NotSupportedErr}
685 Raised when the implementation does not support the requested type
686 of object or operation.
687\end{excdesc}
688
689\begin{excdesc}{NoDataAllowedErr}
690 This is raised if data is specified for a node which does not
691 support data.
692 % XXX a better explanation is needed!
693\end{excdesc}
694
695\begin{excdesc}{NoModificationAllowedErr}
696 Raised on attempts to modify an object where modifications are not
697 allowed (such as for read-only nodes).
698\end{excdesc}
699
700\begin{excdesc}{SyntaxErr}
701 Raised when an invalid or illegal string is specified.
702 % XXX how is this different from InvalidCharacterErr ???
703\end{excdesc}
704
705\begin{excdesc}{WrongDocumentErr}
706 Raised when a node is inserted in a different document than it
707 currently belongs to, and the implementation does not support
708 migrating the node from one document to the other.
709\end{excdesc}
710
711The exception codes defined in the DOM recommendation map to the
712exceptions described above according to this table:
713
714\begin{tableii}{l|l}{constant}{Constant}{Exception}
715 \lineii{DOMSTRING_SIZE_ERR}{\exception{DomstringSizeErr}}
716 \lineii{HIERARCHY_REQUEST_ERR}{\exception{HierarchyRequestErr}}
717 \lineii{INDEX_SIZE_ERR}{\exception{IndexSizeErr}}
718 \lineii{INUSE_ATTRIBUTE_ERR}{\exception{InuseAttributeErr}}
719 \lineii{INVALID_ACCESS_ERR}{\exception{InvalidAccessErr}}
720 \lineii{INVALID_CHARACTER_ERR}{\exception{InvalidCharacterErr}}
721 \lineii{INVALID_MODIFICATION_ERR}{\exception{InvalidModificationErr}}
722 \lineii{INVALID_STATE_ERR}{\exception{InvalidStateErr}}
723 \lineii{NAMESPACE_ERR}{\exception{NamespaceErr}}
724 \lineii{NOT_FOUND_ERR}{\exception{NotFoundErr}}
725 \lineii{NOT_SUPPORTED_ERR}{\exception{NotSupportedErr}}
726 \lineii{NO_DATA_ALLOWED_ERR}{\exception{NoDataAllowedErr}}
727 \lineii{NO_MODIFICATION_ALLOWED_ERR}{\exception{NoModificationAllowedErr}}
728 \lineii{SYNTAX_ERR}{\exception{SyntaxErr}}
729 \lineii{WRONG_DOCUMENT_ERR}{\exception{WrongDocumentErr}}
730\end{tableii}
731
732
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000733\subsection{Conformance \label{dom-conformance}}
Fred Drake669d36f2000-10-24 02:34:45 +0000734
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000735This section describes the conformance requirements and relationships
736between the Python DOM API, the W3C DOM recommendations, and the OMG
737IDL mapping for Python.
Fred Drake669d36f2000-10-24 02:34:45 +0000738
Fred Drake16942f22000-12-07 04:47:51 +0000739
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000740\subsubsection{Type Mapping \label{dom-type-mapping}}
Fred Drake669d36f2000-10-24 02:34:45 +0000741
Fred Drake16942f22000-12-07 04:47:51 +0000742The primitive IDL types used in the DOM specification are mapped to
743Python types according to the following table.
744
745\begin{tableii}{l|l}{code}{IDL Type}{Python Type}
746 \lineii{boolean}{\code{IntegerType} (with a value of \code{0} or \code{1})}
747 \lineii{int}{\code{IntegerType}}
748 \lineii{long int}{\code{IntegerType}}
749 \lineii{unsigned int}{\code{IntegerType}}
750\end{tableii}
751
752Additionally, the \class{DOMString} defined in the recommendation is
753mapped to a Python string or Unicode string. Applications should
754be able to handle Unicode whenever a string is returned from the DOM.
755
756The IDL \keyword{null} value is mapped to \code{None}, which may be
757accepted or provided by the implementation whenever \keyword{null} is
758allowed by the API.
759
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000760
761\subsubsection{Accessor Methods \label{dom-accessor-methods}}
762
763The mapping from OMG IDL to Python defines accessor functions for IDL
764\keyword{attribute} declarations in much the way the Java mapping
765does. Mapping the IDL declarations
Fred Drake669d36f2000-10-24 02:34:45 +0000766
767\begin{verbatim}
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000768readonly attribute string someValue;
769 attribute string anotherValue;
Fred Drake669d36f2000-10-24 02:34:45 +0000770\end{verbatim}
771
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000772yeilds three accessor functions: a ``get'' method for
773\member{someValue} (\method{_get_someValue()}), and ``get'' and
774``set'' methods for
775\member{anotherValue} (\method{_get_anotherValue()} and
776\method{_set_anotherValue()}). The mapping, in particular, does not
777require that the IDL attributes are accessible as normal Python
778attributes: \code{\var{object}.someValue} is \emph{not} required to
779work, and may raise an \exception{AttributeError}.
Fred Drake669d36f2000-10-24 02:34:45 +0000780
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000781The Python DOM API, however, \emph{does} require that normal attribute
782access work. This means that the typical surrogates generated by
783Python IDL compilers are not likely to work, and wrapper objects may
784be needed on the client if the DOM objects are accessed via CORBA.
785While this does require some additional consideration for CORBA DOM
786clients, the implementers with experience using DOM over CORBA from
787Python do not consider this a problem. Attributes that are declared
788\keyword{readonly} may not restrict write access in all DOM
789implementations.
Fred Drake669d36f2000-10-24 02:34:45 +0000790
Fred Drakeeaf57aa2000-11-29 06:10:22 +0000791Additionally, the accessor functions are not required. If provided,
792they should take the form defined by the Python IDL mapping, but
793these methods are considered unnecessary since the attributes are
Fred Drake16942f22000-12-07 04:47:51 +0000794accessible directly from Python. ``Set'' accessors should never be
795provided for \keyword{readonly} attributes.