Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 1 | \section{\module{xml.dom} --- |
| 2 | The Document Object Model API} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 3 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{xml.dom} |
| 5 | \modulesynopsis{Document Object Model API for Python.} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 6 | \sectionauthor{Paul Prescod}{paul@prescod.net} |
| 7 | \sectionauthor{Martin v. L\"owis}{loewis@informatik.hu-berlin.de} |
| 8 | |
| 9 | \versionadded{2.0} |
| 10 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 11 | The Document Object Model, or ``DOM,'' is a cross-language API from |
| 12 | the World Wide Web Consortium (W3C) for accessing and modifying XML |
| 13 | documents. A DOM implementation presents an XML document as a tree |
| 14 | structure, or allows client code to build such a structure from |
| 15 | scratch. It then gives access to the structure through a set of |
| 16 | objects which provided well-known interfaces. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 17 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 18 | The DOM is extremely useful for random-access applications. SAX only |
| 19 | allows you a view of one bit of the document at a time. If you are |
| 20 | looking at one SAX element, you have no access to another. If you are |
| 21 | looking at a text node, you have no access to a containing element. |
| 22 | When you write a SAX application, you need to keep track of your |
| 23 | program's position in the document somewhere in your own code. SAX |
| 24 | does not do it for you. Also, if you need to look ahead in the XML |
| 25 | document, you are just out of luck. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 26 | |
| 27 | Some applications are simply impossible in an event driven model with |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 28 | no access to a tree. Of course you could build some sort of tree |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 29 | yourself in SAX events, but the DOM allows you to avoid writing that |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 30 | code. The DOM is a standard tree representation for XML data. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 31 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 32 | %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 Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 38 | % See http://www.prescod.net/python/pulldom |
| 39 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 40 | The Document Object Model is being defined by the W3C in stages, or |
| 41 | ``levels'' in their terminology. The Python mapping of the API is |
| 42 | substantially based on the DOM Level 2 recommendation. Some aspects |
Fred Drake | 66f98b4 | 2001-01-26 20:51:32 +0000 | [diff] [blame] | 43 | of the API will only become available in Python 2.1, or may only be |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 44 | available in particular DOM implementations. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 45 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 46 | DOM applications typically start by parsing some XML into a DOM. How |
| 47 | this is accomplished is not covered at all by DOM Level 1, and Level 2 |
| 48 | provides 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 |
| 51 | in DOM Level 2 and were not implemented in time for Python 2.0. There |
Fred Drake | 66f98b4 | 2001-01-26 20:51:32 +0000 | [diff] [blame] | 52 | is also no well-defined way to access these methods without an |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 53 | existing \class{Document} object. For Python 2.0, consult the |
| 54 | documentation for each particular DOM implementation to determine the |
| 55 | bootstrap procedure needed to create and initialize \class{Document} |
Fred Drake | 66f98b4 | 2001-01-26 20:51:32 +0000 | [diff] [blame] | 56 | and \class{DocumentType} instances. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 57 | |
| 58 | Once you have a DOM document object, you can access the parts of your |
| 59 | XML document through its properties and methods. These properties are |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 60 | defined in the DOM specification; this portion of the reference manual |
| 61 | describes the interpretation of the specification in Python. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 62 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 63 | The specification provided by the W3C defines the DOM API for Java, |
| 64 | ECMAScript, and OMG IDL. The Python mapping defined here is based in |
| 65 | large part on the IDL version of the specification, but strict |
| 66 | compliance is not required (though implementations are free to support |
| 67 | the strict mapping from IDL). See section \ref{dom-conformance}, |
| 68 | ``Conformance,'' for a detailed discussion of mapping requirements. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 69 | |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 70 | |
| 71 | \begin{seealso} |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 72 | \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 Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 79 | 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 Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 83 | \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 Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 86 | \end{seealso} |
| 87 | |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 88 | \subsection{Module Contents} |
| 89 | |
| 90 | The \module{xml.dom} contains the following functions: |
| 91 | |
| 92 | \begin{funcdesc}{registerDOMImplementation}{name, factory} |
Fred Drake | 07e6c50 | 2001-02-23 19:15:56 +0000 | [diff] [blame] | 93 | Register the \var{factory} function with the name \var{name}. The |
| 94 | factory function should return an object which implements the |
| 95 | \class{DOMImplementation} interface. The factory function can return |
| 96 | the same object every time, or a new one for each call, as appropriate |
| 97 | for the specific implementation (e.g. if that implementation supports |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 98 | some customization). |
| 99 | \end{funcdesc} |
| 100 | |
Fred Drake | 89d63cc | 2001-11-30 16:58:15 +0000 | [diff] [blame] | 101 | \begin{funcdesc}{getDOMImplementation}{\optional{name\optional{, features}}} |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 102 | Return a suitable DOM implementation. The \var{name} is either |
| 103 | well-known, the module name of a DOM implementation, or |
Fred Drake | 89d63cc | 2001-11-30 16:58:15 +0000 | [diff] [blame] | 104 | \code{None}. If it is not \code{None}, imports the corresponding |
| 105 | module and returns a \class{DOMImplementation} object if the import |
| 106 | succeeds. If no name is given, and if the environment variable |
| 107 | \envvar{PYTHON_DOM} is set, this variable is used to find the |
| 108 | implementation. |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 109 | |
Fred Drake | 89d63cc | 2001-11-30 16:58:15 +0000 | [diff] [blame] | 110 | If name is not given, this examines the available implementations to |
| 111 | find one with the required feature set. If no implementation can be |
| 112 | found, raise an \exception{ImportError}. The features list must be a |
| 113 | sequence of \code{(\var{feature}, \var{version})} pairs which are |
| 114 | passed to the \method{hasFeature()} method on available |
| 115 | \class{DOMImplementation} objects. |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 116 | \end{funcdesc} |
| 117 | |
Fred Drake | bd34b6b | 2001-11-30 15:37:33 +0000 | [diff] [blame] | 118 | |
| 119 | Some convenience constants are also provided: |
| 120 | |
| 121 | \begin{datadesc}{EMPTY_NAMESPACE} |
| 122 | The value used to indicate that no namespace is associated with a |
| 123 | node in the DOM. This is typically found as the |
| 124 | \member{namespaceURI} of a node, or used as the \var{namespaceURI} |
| 125 | parameter to a namespaces-specific method. |
| 126 | \versionadded{2.2} |
| 127 | \end{datadesc} |
| 128 | |
| 129 | \begin{datadesc}{XML_NAMESPACE} |
| 130 | The namespace URI associated with the reserved prefix \code{xml}, as |
| 131 | defined by |
| 132 | \citetitle[http://www.w3.org/TR/REC-xml-names/]{Namespaces in XML} |
| 133 | (section~4). |
| 134 | \versionadded{2.2} |
| 135 | \end{datadesc} |
| 136 | |
| 137 | \begin{datadesc}{XMLNS_NAMESPACE} |
| 138 | The namespace URI for namespace declarations, as defined by |
| 139 | \citetitle[http://www.w3.org/TR/DOM-Level-2-Core/core.html]{Document |
| 140 | Object Model (DOM) Level 2 Core Specification} (section~1.1.8). |
| 141 | \versionadded{2.2} |
| 142 | \end{datadesc} |
| 143 | |
| 144 | \begin{datadesc}{XHTML_NAMESPACE} |
| 145 | The URI of the XHTML namespace as defined by |
| 146 | \citetitle[http://www.w3.org/TR/xhtml1/]{XHTML 1.0: The Extensible |
| 147 | HyperText Markup Language} (section~3.1.1). |
| 148 | \versionadded{2.2} |
| 149 | \end{datadesc} |
| 150 | |
| 151 | |
Martin v. Löwis | 7edbd4f | 2001-02-22 14:05:50 +0000 | [diff] [blame] | 152 | % Should the Node documentation go here? |
| 153 | |
Fred Drake | e21e2bb | 2001-10-26 20:09:49 +0000 | [diff] [blame] | 154 | In addition, \module{xml.dom} contains a base \class{Node} class and |
| 155 | the DOM exception classes. The \class{Node} class provided by this |
| 156 | module does not implement any of the methods or attributes defined by |
| 157 | the DOM specification; concrete DOM implementations must provide |
| 158 | those. The \class{Node} class provided as part of this module does |
| 159 | provide the constants used for the \member{nodeType} attribute on |
| 160 | concrete \class{Node} objects; they are located within the class |
| 161 | rather than at the module level to conform with the DOM |
| 162 | specifications. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 163 | |
Fred Drake | bd34b6b | 2001-11-30 15:37:33 +0000 | [diff] [blame] | 164 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 165 | \subsection{Objects in the DOM \label{dom-objects}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 166 | |
| 167 | The definitive documentation for the DOM is the DOM specification from |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 168 | the W3C. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 169 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 170 | Note that DOM attributes may also be manipulated as nodes instead of |
| 171 | as simple strings. It is fairly rare that you must do this, however, |
| 172 | so this usage is not yet documented. |
| 173 | |
| 174 | |
| 175 | \begin{tableiii}{l|l|l}{class}{Interface}{Section}{Purpose} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 176 | \lineiii{DOMImplementation}{\ref{dom-implementation-objects}} |
| 177 | {Interface to the underlying implementation.} |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 178 | \lineiii{Node}{\ref{dom-node-objects}} |
| 179 | {Base interface for most objects in a document.} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 180 | \lineiii{NodeList}{\ref{dom-nodelist-objects}} |
| 181 | {Interface for a sequence of nodes.} |
| 182 | \lineiii{DocumentType}{\ref{dom-documenttype-objects}} |
| 183 | {Information about the declarations needed to process a document.} |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 184 | \lineiii{Document}{\ref{dom-document-objects}} |
| 185 | {Object which represents an entire document.} |
| 186 | \lineiii{Element}{\ref{dom-element-objects}} |
| 187 | {Element nodes in the document hierarchy.} |
| 188 | \lineiii{Attr}{\ref{dom-attr-objects}} |
| 189 | {Attribute value nodes on element nodes.} |
| 190 | \lineiii{Comment}{\ref{dom-comment-objects}} |
| 191 | {Representation of comments in the source document.} |
| 192 | \lineiii{Text}{\ref{dom-text-objects}} |
| 193 | {Nodes containing textual content from the document.} |
| 194 | \lineiii{ProcessingInstruction}{\ref{dom-pi-objects}} |
| 195 | {Processing instruction representation.} |
| 196 | \end{tableiii} |
| 197 | |
Fred Drake | bc9c1b1 | 2000-12-13 17:38:02 +0000 | [diff] [blame] | 198 | An additional section describes the exceptions defined for working |
| 199 | with the DOM in Python. |
| 200 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 201 | |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 202 | \subsubsection{DOMImplementation Objects |
| 203 | \label{dom-implementation-objects}} |
| 204 | |
| 205 | The \class{DOMImplementation} interface provides a way for |
| 206 | applications to determine the availability of particular features in |
| 207 | the DOM they are using. DOM Level 2 added the ability to create new |
| 208 | \class{Document} and \class{DocumentType} objects using the |
| 209 | \class{DOMImplementation} as well. |
| 210 | |
| 211 | \begin{methoddesc}[DOMImplementation]{hasFeature}{feature, version} |
| 212 | \end{methoddesc} |
| 213 | |
| 214 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 215 | \subsubsection{Node Objects \label{dom-node-objects}} |
| 216 | |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 217 | All of the components of an XML document are subclasses of |
| 218 | \class{Node}. |
| 219 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 220 | \begin{memberdesc}[Node]{nodeType} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 221 | An integer representing the node type. Symbolic constants for the |
Fred Drake | 66f98b4 | 2001-01-26 20:51:32 +0000 | [diff] [blame] | 222 | types are on the \class{Node} object: |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 223 | \constant{ELEMENT_NODE}, \constant{ATTRIBUTE_NODE}, |
| 224 | \constant{TEXT_NODE}, \constant{CDATA_SECTION_NODE}, |
| 225 | \constant{ENTITY_NODE}, \constant{PROCESSING_INSTRUCTION_NODE}, |
| 226 | \constant{COMMENT_NODE}, \constant{DOCUMENT_NODE}, |
| 227 | \constant{DOCUMENT_TYPE_NODE}, \constant{NOTATION_NODE}. |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 228 | This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 229 | \end{memberdesc} |
| 230 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 231 | \begin{memberdesc}[Node]{parentNode} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 232 | The parent of the current node, or \code{None} for the document node. |
| 233 | The value is always a \class{Node} object or \code{None}. For |
| 234 | \class{Element} nodes, this will be the parent element, except for the |
| 235 | root element, in which case it will be the \class{Document} object. |
| 236 | For \class{Attr} nodes, this is always \code{None}. |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 237 | This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 238 | \end{memberdesc} |
| 239 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 240 | \begin{memberdesc}[Node]{attributes} |
Fred Drake | 61f7949 | 2001-10-25 20:42:57 +0000 | [diff] [blame] | 241 | A \class{NamedNodeMap} of attribute objects. Only elements have |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 242 | actual values for this; others provide \code{None} for this attribute. |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 243 | This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 244 | \end{memberdesc} |
| 245 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 246 | \begin{memberdesc}[Node]{previousSibling} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 247 | The node that immediately precedes this one with the same parent. For |
| 248 | instance the element with an end-tag that comes just before the |
| 249 | \var{self} element's start-tag. Of course, XML documents are made |
| 250 | up of more than just elements so the previous sibling could be text, a |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 251 | comment, or something else. If this node is the first child of the |
| 252 | parent, this attribute will be \code{None}. |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 253 | This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 254 | \end{memberdesc} |
| 255 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 256 | \begin{memberdesc}[Node]{nextSibling} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 257 | The node that immediately follows this one with the same parent. See |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 258 | also \member{previousSibling}. If this is the last child of the |
| 259 | parent, this attribute will be \code{None}. |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 260 | This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 261 | \end{memberdesc} |
| 262 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 263 | \begin{memberdesc}[Node]{childNodes} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 264 | A list of nodes contained within this node. |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 265 | This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 266 | \end{memberdesc} |
| 267 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 268 | \begin{memberdesc}[Node]{firstChild} |
| 269 | The first child of the node, if there are any, or \code{None}. |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 270 | This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 271 | \end{memberdesc} |
| 272 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 273 | \begin{memberdesc}[Node]{lastChild} |
| 274 | The last child of the node, if there are any, or \code{None}. |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 275 | This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 276 | \end{memberdesc} |
| 277 | |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 278 | \begin{memberdesc}[Node]{localName} |
| 279 | The part of the \member{tagName} following the colon if there is one, |
| 280 | else the entire \member{tagName}. The value is a string. |
| 281 | \end{memberdesc} |
| 282 | |
| 283 | \begin{memberdesc}[Node]{prefix} |
| 284 | The part of the \member{tagName} preceding the colon if there is one, |
| 285 | else the empty string. The value is a string, or \code{None} |
| 286 | \end{memberdesc} |
| 287 | |
| 288 | \begin{memberdesc}[Node]{namespaceURI} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 289 | The namespace associated with the element name. This will be a |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 290 | string or \code{None}. This is a read-only attribute. |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 291 | \end{memberdesc} |
| 292 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 293 | \begin{memberdesc}[Node]{nodeName} |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 294 | This has a different meaning for each node type; see the DOM |
| 295 | specification for details. You can always get the information you |
| 296 | would get here from another property such as the \member{tagName} |
| 297 | property for elements or the \member{name} property for attributes. |
| 298 | For all node types, the value of this attribute will be either a |
| 299 | string or \code{None}. This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 300 | \end{memberdesc} |
| 301 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 302 | \begin{memberdesc}[Node]{nodeValue} |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 303 | This has a different meaning for each node type; see the DOM |
| 304 | specification for details. The situation is similar to that with |
| 305 | \member{nodeName}. The value is a string or \code{None}. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 306 | \end{memberdesc} |
| 307 | |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 308 | \begin{methoddesc}[Node]{hasAttributes}{} |
| 309 | Returns true if the node has any attributes. |
| 310 | \end{methoddesc} |
| 311 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 312 | \begin{methoddesc}[Node]{hasChildNodes}{} |
| 313 | Returns true if the node has any child nodes. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 314 | \end{methoddesc} |
| 315 | |
Fred Drake | 40e43bf | 2001-02-03 01:20:01 +0000 | [diff] [blame] | 316 | \begin{methoddesc}[Node]{isSameNode}{other} |
| 317 | Returns true if \var{other} refers to the same node as this node. |
| 318 | This is especially useful for DOM implementations which use any sort |
| 319 | of proxy architecture (because more than one object can refer to the |
| 320 | same node). |
Fred Drake | 15862f5 | 2001-02-14 20:39:15 +0000 | [diff] [blame] | 321 | |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 322 | \note{This is based on a proposed DOM Level 3 API which is |
Fred Drake | 15862f5 | 2001-02-14 20:39:15 +0000 | [diff] [blame] | 323 | still in the ``working draft'' stage, but this particular interface |
| 324 | appears uncontroversial. Changes from the W3C will not necessarily |
| 325 | affect this method in the Python DOM interface (though any new W3C |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 326 | API for this would also be supported).} |
Fred Drake | 40e43bf | 2001-02-03 01:20:01 +0000 | [diff] [blame] | 327 | \end{methoddesc} |
| 328 | |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 329 | \begin{methoddesc}[Node]{appendChild}{newChild} |
| 330 | Add a new child node to this node at the end of the list of children, |
| 331 | returning \var{newChild}. |
| 332 | \end{methoddesc} |
| 333 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 334 | \begin{methoddesc}[Node]{insertBefore}{newChild, refChild} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 335 | Insert a new child node before an existing child. It must be the case |
| 336 | that \var{refChild} is a child of this node; if not, |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 337 | \exception{ValueError} is raised. \var{newChild} is returned. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 338 | \end{methoddesc} |
| 339 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 340 | \begin{methoddesc}[Node]{removeChild}{oldChild} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 341 | Remove a child node. \var{oldChild} must be a child of this node; if |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 342 | not, \exception{ValueError} is raised. \var{oldChild} is returned on |
| 343 | success. If \var{oldChild} will not be used further, its |
| 344 | \method{unlink()} method should be called. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 345 | \end{methoddesc} |
| 346 | |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 347 | \begin{methoddesc}[Node]{replaceChild}{newChild, oldChild} |
| 348 | Replace an existing node with a new node. It must be the case that |
| 349 | \var{oldChild} is a child of this node; if not, |
| 350 | \exception{ValueError} is raised. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 351 | \end{methoddesc} |
| 352 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 353 | \begin{methoddesc}[Node]{normalize}{} |
| 354 | Join adjacent text nodes so that all stretches of text are stored as |
| 355 | single \class{Text} instances. This simplifies processing text from a |
| 356 | DOM tree for many applications. |
| 357 | \versionadded{2.1} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 358 | \end{methoddesc} |
| 359 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 360 | \begin{methoddesc}[Node]{cloneNode}{deep} |
| 361 | Clone this node. Setting \var{deep} means to clone all child nodes as |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 362 | well. This returns the clone. |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 363 | \end{methoddesc} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 364 | |
| 365 | |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 366 | \subsubsection{NodeList Objects \label{dom-nodelist-objects}} |
| 367 | |
| 368 | A \class{NodeList} represents a sequence of nodes. These objects are |
| 369 | used in two ways in the DOM Core recommendation: the |
| 370 | \class{Element} objects provides one as it's list of child nodes, and |
| 371 | the \method{getElementsByTagName()} and |
| 372 | \method{getElementsByTagNameNS()} methods of \class{Node} return |
| 373 | objects with this interface to represent query results. |
| 374 | |
| 375 | The DOM Level 2 recommendation defines one method and one attribute |
| 376 | for these objects: |
| 377 | |
| 378 | \begin{methoddesc}[NodeList]{item}{i} |
| 379 | Return the \var{i}'th item from the sequence, if there is one, or |
| 380 | \code{None}. The index \var{i} is not allowed to be less then zero |
| 381 | or greater than or equal to the length of the sequence. |
| 382 | \end{methoddesc} |
| 383 | |
| 384 | \begin{memberdesc}[NodeList]{length} |
| 385 | The number of nodes in the sequence. |
| 386 | \end{memberdesc} |
| 387 | |
| 388 | In addition, the Python DOM interface requires that some additional |
| 389 | support is provided to allow \class{NodeList} objects to be used as |
| 390 | Python sequences. All \class{NodeList} implementations must include |
| 391 | support for \method{__len__()} and \method{__getitem__()}; this allows |
| 392 | iteration over the \class{NodeList} in \keyword{for} statements and |
| 393 | proper support for the \function{len()} built-in function. |
| 394 | |
| 395 | If a DOM implementation supports modification of the document, the |
| 396 | \class{NodeList} implementation must also support the |
| 397 | \method{__setitem__()} and \method{__delitem__()} methods. |
| 398 | |
| 399 | |
| 400 | \subsubsection{DocumentType Objects \label{dom-documenttype-objects}} |
| 401 | |
| 402 | Information about the notations and entities declared by a document |
| 403 | (including the external subset if the parser uses it and can provide |
| 404 | the information) is available from a \class{DocumentType} object. The |
| 405 | \class{DocumentType} for a document is available from the |
Fred Drake | e21e2bb | 2001-10-26 20:09:49 +0000 | [diff] [blame] | 406 | \class{Document} object's \member{doctype} attribute; if there is no |
| 407 | \code{DOCTYPE} declaration for the document, the document's |
| 408 | \member{doctype} attribute will be set to \code{None} instead of an |
| 409 | instance of this interface. |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 410 | |
| 411 | \class{DocumentType} is a specialization of \class{Node}, and adds the |
| 412 | following attributes: |
| 413 | |
| 414 | \begin{memberdesc}[DocumentType]{publicId} |
| 415 | The public identifier for the external subset of the document type |
| 416 | definition. This will be a string or \code{None}. |
| 417 | \end{memberdesc} |
| 418 | |
| 419 | \begin{memberdesc}[DocumentType]{systemId} |
| 420 | The system identifier for the external subset of the document type |
| 421 | definition. This will be a URI as a string, or \code{None}. |
| 422 | \end{memberdesc} |
| 423 | |
| 424 | \begin{memberdesc}[DocumentType]{internalSubset} |
| 425 | A string giving the complete internal subset from the document. |
Fred Drake | f459d85 | 2001-04-05 18:30:04 +0000 | [diff] [blame] | 426 | This does not include the brackets which enclose the subset. If the |
| 427 | document has no internal subset, this should be \code{None}. |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 428 | \end{memberdesc} |
| 429 | |
| 430 | \begin{memberdesc}[DocumentType]{name} |
| 431 | The name of the root element as given in the \code{DOCTYPE} |
Fred Drake | e21e2bb | 2001-10-26 20:09:49 +0000 | [diff] [blame] | 432 | declaration, if present. |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 433 | \end{memberdesc} |
| 434 | |
| 435 | \begin{memberdesc}[DocumentType]{entities} |
| 436 | This is a \class{NamedNodeMap} giving the definitions of external |
| 437 | entities. For entity names defined more than once, only the first |
| 438 | definition is provided (others are ignored as required by the XML |
| 439 | recommendation). This may be \code{None} if the information is not |
| 440 | provided by the parser, or if no entities are defined. |
| 441 | \end{memberdesc} |
| 442 | |
| 443 | \begin{memberdesc}[DocumentType]{notations} |
| 444 | This is a \class{NamedNodeMap} giving the definitions of notations. |
| 445 | For notation names defined more than once, only the first definition |
| 446 | is provided (others are ignored as required by the XML |
| 447 | recommendation). This may be \code{None} if the information is not |
| 448 | provided by the parser, or if no notations are defined. |
| 449 | \end{memberdesc} |
| 450 | |
| 451 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 452 | \subsubsection{Document Objects \label{dom-document-objects}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 453 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 454 | A \class{Document} represents an entire XML document, including its |
| 455 | constituent elements, attributes, processing instructions, comments |
| 456 | etc. Remeber that it inherits properties from \class{Node}. |
| 457 | |
| 458 | \begin{memberdesc}[Document]{documentElement} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 459 | The one and only root element of the document. |
| 460 | \end{memberdesc} |
| 461 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 462 | \begin{methoddesc}[Document]{createElement}{tagName} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 463 | Create and return a new element node. The element is not inserted |
| 464 | into the document when it is created. You need to explicitly insert |
| 465 | it with one of the other methods such as \method{insertBefore()} or |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 466 | \method{appendChild()}. |
| 467 | \end{methoddesc} |
| 468 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 469 | \begin{methoddesc}[Document]{createElementNS}{namespaceURI, tagName} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 470 | Create and return a new element with a namespace. The |
| 471 | \var{tagName} may have a prefix. The element is not inserted into the |
| 472 | document when it is created. You need to explicitly insert it with |
| 473 | one of the other methods such as \method{insertBefore()} or |
| 474 | \method{appendChild()}. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 475 | \end{methoddesc} |
| 476 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 477 | \begin{methoddesc}[Document]{createTextNode}{data} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 478 | Create and return a text node containing the data passed as a |
| 479 | parameter. As with the other creation methods, this one does not |
| 480 | insert the node into the tree. |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 481 | \end{methoddesc} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 482 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 483 | \begin{methoddesc}[Document]{createComment}{data} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 484 | Create and return a comment node containing the data passed as a |
| 485 | parameter. As with the other creation methods, this one does not |
| 486 | insert the node into the tree. |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 487 | \end{methoddesc} |
| 488 | |
| 489 | \begin{methoddesc}[Document]{createProcessingInstruction}{target, data} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 490 | Create and return a processing instruction node containing the |
| 491 | \var{target} and \var{data} passed as parameters. As with the other |
| 492 | creation methods, this one does not insert the node into the tree. |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 493 | \end{methoddesc} |
| 494 | |
| 495 | \begin{methoddesc}[Document]{createAttribute}{name} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 496 | Create and return an attribute node. This method does not associate |
| 497 | the attribute node with any particular element. You must use |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 498 | \method{setAttributeNode()} on the appropriate \class{Element} object |
| 499 | to use the newly created attribute instance. |
| 500 | \end{methoddesc} |
| 501 | |
| 502 | \begin{methoddesc}[Document]{createAttributeNS}{namespaceURI, qualifiedName} |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 503 | Create and return an attribute node with a namespace. The |
| 504 | \var{tagName} may have a prefix. This method does not associate the |
| 505 | attribute node with any particular element. You must use |
| 506 | \method{setAttributeNode()} on the appropriate \class{Element} object |
| 507 | to use the newly created attribute instance. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 508 | \end{methoddesc} |
| 509 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 510 | \begin{methoddesc}[Document]{getElementsByTagName}{tagName} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 511 | Search for all descendants (direct children, children's children, |
| 512 | etc.) with a particular element type name. |
| 513 | \end{methoddesc} |
| 514 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 515 | \begin{methoddesc}[Document]{getElementsByTagNameNS}{namespaceURI, localName} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 516 | Search for all descendants (direct children, children's children, |
| 517 | etc.) with a particular namespace URI and localname. The localname is |
| 518 | the part of the namespace after the prefix. |
| 519 | \end{methoddesc} |
| 520 | |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 521 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 522 | \subsubsection{Element Objects \label{dom-element-objects}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 523 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 524 | \class{Element} is a subclass of \class{Node}, so inherits all the |
| 525 | attributes of that class. |
| 526 | |
| 527 | \begin{memberdesc}[Element]{tagName} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 528 | The element type name. In a namespace-using document it may have |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 529 | colons in it. The value is a string. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 530 | \end{memberdesc} |
| 531 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 532 | \begin{methoddesc}[Element]{getElementsByTagName}{tagName} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 533 | Same as equivalent method in the \class{Document} class. |
| 534 | \end{methoddesc} |
| 535 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 536 | \begin{methoddesc}[Element]{getElementsByTagNameNS}{tagName} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 537 | Same as equivalent method in the \class{Document} class. |
| 538 | \end{methoddesc} |
| 539 | |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 540 | \begin{methoddesc}[Element]{getAttribute}{attname} |
| 541 | Return an attribute value as a string. |
| 542 | \end{methoddesc} |
| 543 | |
| 544 | \begin{methoddesc}[Element]{getAttributeNode}{attrname} |
| 545 | Return the \class{Attr} node for the attribute named by |
| 546 | \var{attrname}. |
| 547 | \end{methoddesc} |
| 548 | |
| 549 | \begin{methoddesc}[Element]{getAttributeNS}{namespaceURI, localName} |
| 550 | Return an attribute value as a string, given a \var{namespaceURI} and |
| 551 | \var{localName}. |
| 552 | \end{methoddesc} |
| 553 | |
| 554 | \begin{methoddesc}[Element]{getAttributeNodeNS}{namespaceURI, localName} |
| 555 | Return an attribute value as a node, given a \var{namespaceURI} and |
| 556 | \var{localName}. |
| 557 | \end{methoddesc} |
| 558 | |
| 559 | \begin{methoddesc}[Element]{removeAttribute}{attname} |
| 560 | Remove an attribute by name. No exception is raised if there is no |
| 561 | matching attribute. |
| 562 | \end{methoddesc} |
| 563 | |
| 564 | \begin{methoddesc}[Element]{removeAttributeNode}{oldAttr} |
| 565 | Remove and return \var{oldAttr} from the attribute list, if present. |
| 566 | If \var{oldAttr} is not present, \exception{NotFoundErr} is raised. |
| 567 | \end{methoddesc} |
| 568 | |
| 569 | \begin{methoddesc}[Element]{removeAttributeNS}{namespaceURI, localName} |
| 570 | Remove an attribute by name. Note that it uses a localName, not a |
| 571 | qname. No exception is raised if there is no matching attribute. |
| 572 | \end{methoddesc} |
| 573 | |
| 574 | \begin{methoddesc}[Element]{setAttribute}{attname, value} |
| 575 | Set an attribute value from a string. |
| 576 | \end{methoddesc} |
| 577 | |
| 578 | \begin{methoddesc}[Element]{setAttributeNode}{newAttr} |
| 579 | Add a new attibute node to the element, replacing an existing |
| 580 | attribute if necessary if the \member{name} attribute matches. If a |
| 581 | replacement occurs, the old attribute node will be returned. If |
| 582 | \var{newAttr} is already in use, \exception{InuseAttributeErr} will be |
| 583 | raised. |
| 584 | \end{methoddesc} |
| 585 | |
| 586 | \begin{methoddesc}[Element]{setAttributeNodeNS}{newAttr} |
| 587 | Add a new attibute node to the element, replacing an existing |
| 588 | attribute if necessary if the \member{namespaceURI} and |
| 589 | \member{localName} attributes match. If a replacement occurs, the old |
| 590 | attribute node will be returned. If \var{newAttr} is already in use, |
| 591 | \exception{InuseAttributeErr} will be raised. |
| 592 | \end{methoddesc} |
| 593 | |
| 594 | \begin{methoddesc}[Element]{setAttributeNS}{namespaceURI, qname, value} |
| 595 | Set an attribute value from a string, given a \var{namespaceURI} and a |
| 596 | \var{qname}. Note that a qname is the whole attribute name. This is |
| 597 | different than above. |
| 598 | \end{methoddesc} |
| 599 | |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 600 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 601 | \subsubsection{Attr Objects \label{dom-attr-objects}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 602 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 603 | \class{Attr} inherits from \class{Node}, so inherits all its |
| 604 | attributes. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 605 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 606 | \begin{memberdesc}[Attr]{name} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 607 | The attribute name. In a namespace-using document it may have colons |
| 608 | in it. |
| 609 | \end{memberdesc} |
| 610 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 611 | \begin{memberdesc}[Attr]{localName} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 612 | The part of the name following the colon if there is one, else the |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 613 | entire name. This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 614 | \end{memberdesc} |
| 615 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 616 | \begin{memberdesc}[Attr]{prefix} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 617 | The part of the name preceding the colon if there is one, else the |
| 618 | empty string. |
| 619 | \end{memberdesc} |
| 620 | |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 621 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 622 | \subsubsection{NamedNodeMap Objects \label{dom-attributelist-objects}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 623 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 624 | \class{NamedNodeMap} does \emph{not} inherit from \class{Node}. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 625 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 626 | \begin{memberdesc}[NamedNodeMap]{length} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 627 | The length of the attribute list. |
| 628 | \end{memberdesc} |
| 629 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 630 | \begin{methoddesc}[NamedNodeMap]{item}{index} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 631 | Return an attribute with a particular index. The order you get the |
| 632 | attributes in is arbitrary but will be consistent for the life of a |
| 633 | DOM. Each item is an attribute node. Get its value with the |
| 634 | \member{value} attribbute. |
| 635 | \end{methoddesc} |
| 636 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 637 | There are also experimental methods that give this class more mapping |
| 638 | behavior. You can use them or you can use the standardized |
Fred Drake | e21e2bb | 2001-10-26 20:09:49 +0000 | [diff] [blame] | 639 | \method{getAttribute*()} family of methods on the \class{Element} |
| 640 | objects. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 641 | |
| 642 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 643 | \subsubsection{Comment Objects \label{dom-comment-objects}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 644 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 645 | \class{Comment} represents a comment in the XML document. It is a |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 646 | subclass of \class{Node}, but cannot have child nodes. |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 647 | |
| 648 | \begin{memberdesc}[Comment]{data} |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 649 | The content of the comment as a string. The attribute contains all |
| 650 | characters between the leading \code{<!-}\code{-} and trailing |
| 651 | \code{-}\code{->}, but does not include them. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 652 | \end{memberdesc} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 653 | |
| 654 | |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 655 | \subsubsection{Text and CDATASection Objects \label{dom-text-objects}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 656 | |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 657 | The \class{Text} interface represents text in the XML document. If |
| 658 | the parser and DOM implementation support the DOM's XML extension, |
| 659 | portions of the text enclosed in CDATA marked sections are stored in |
| 660 | \class{CDATASection} objects. These two interfaces are identical, but |
| 661 | provide different values for the \member{nodeType} attribute. |
| 662 | |
| 663 | These interfaces extend the \class{Node} interface. They cannot have |
| 664 | child nodes. |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 665 | |
| 666 | \begin{memberdesc}[Text]{data} |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 667 | The content of the text node as a string. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 668 | \end{memberdesc} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 669 | |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 670 | \note{The use of a \class{CDATASection} node does not |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 671 | indicate that the node represents a complete CDATA marked section, |
| 672 | only that the content of the node was part of a CDATA section. A |
| 673 | single CDATA section may be represented by more than one node in the |
| 674 | document tree. There is no way to determine whether two adjacent |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 675 | \class{CDATASection} nodes represent different CDATA marked sections.} |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 676 | |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 677 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 678 | \subsubsection{ProcessingInstruction Objects \label{dom-pi-objects}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 679 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 680 | Represents a processing instruction in the XML document; this inherits |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 681 | from the \class{Node} interface and cannot have child nodes. |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 682 | |
| 683 | \begin{memberdesc}[ProcessingInstruction]{target} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 684 | The content of the processing instruction up to the first whitespace |
Fred Drake | 9a29dd6 | 2000-12-08 06:54:51 +0000 | [diff] [blame] | 685 | character. This is a read-only attribute. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 686 | \end{memberdesc} |
| 687 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 688 | \begin{memberdesc}[ProcessingInstruction]{data} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 689 | The content of the processing instruction following the first |
| 690 | whitespace character. |
| 691 | \end{memberdesc} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 692 | |
| 693 | |
Fred Drake | bc9c1b1 | 2000-12-13 17:38:02 +0000 | [diff] [blame] | 694 | \subsubsection{Exceptions \label{dom-exceptions}} |
| 695 | |
| 696 | \versionadded{2.1} |
| 697 | |
| 698 | The DOM Level 2 recommendation defines a single exception, |
| 699 | \exception{DOMException}, and a number of constants that allow |
| 700 | applications to determine what sort of error occurred. |
| 701 | \exception{DOMException} instances carry a \member{code} attribute |
| 702 | that provides the appropriate value for the specific exception. |
| 703 | |
| 704 | The Python DOM interface provides the constants, but also expands the |
| 705 | set of exceptions so that a specific exception exists for each of the |
| 706 | exception codes defined by the DOM. The implementations must raise |
| 707 | the appropriate specific exception, each of which carries the |
| 708 | appropriate value for the \member{code} attribute. |
| 709 | |
| 710 | \begin{excdesc}{DOMException} |
| 711 | Base exception class used for all specific DOM exceptions. This |
| 712 | exception class cannot be directly instantiated. |
| 713 | \end{excdesc} |
| 714 | |
| 715 | \begin{excdesc}{DomstringSizeErr} |
| 716 | Raised when a specified range of text does not fit into a string. |
| 717 | This is not known to be used in the Python DOM implementations, but |
| 718 | may be received from DOM implementations not written in Python. |
| 719 | \end{excdesc} |
| 720 | |
| 721 | \begin{excdesc}{HierarchyRequestErr} |
| 722 | Raised when an attempt is made to insert a node where the node type |
| 723 | is not allowed. |
| 724 | \end{excdesc} |
| 725 | |
| 726 | \begin{excdesc}{IndexSizeErr} |
| 727 | Raised when an index or size parameter to a method is negative or |
| 728 | exceeds the allowed values. |
| 729 | \end{excdesc} |
| 730 | |
| 731 | \begin{excdesc}{InuseAttributeErr} |
| 732 | Raised when an attempt is made to insert an \class{Attr} node that |
| 733 | is already present elsewhere in the document. |
| 734 | \end{excdesc} |
| 735 | |
| 736 | \begin{excdesc}{InvalidAccessErr} |
| 737 | Raised if a parameter or an operation is not supported on the |
| 738 | underlying object. |
| 739 | \end{excdesc} |
| 740 | |
| 741 | \begin{excdesc}{InvalidCharacterErr} |
| 742 | This exception is raised when a string parameter contains a |
| 743 | character that is not permitted in the context it's being used in by |
| 744 | the XML 1.0 recommendation. For example, attempting to create an |
| 745 | \class{Element} node with a space in the element type name will |
| 746 | cause this error to be raised. |
| 747 | \end{excdesc} |
| 748 | |
| 749 | \begin{excdesc}{InvalidModificationErr} |
| 750 | Raised when an attempt is made to modify the type of a node. |
| 751 | \end{excdesc} |
| 752 | |
| 753 | \begin{excdesc}{InvalidStateErr} |
| 754 | Raised when an attempt is made to use an object that is not or is no |
| 755 | longer usable. |
| 756 | \end{excdesc} |
| 757 | |
| 758 | \begin{excdesc}{NamespaceErr} |
| 759 | If an attempt is made to change any object in a way that is not |
| 760 | permitted with regard to the |
| 761 | \citetitle[http://www.w3.org/TR/REC-xml-names/]{Namespaces in XML} |
| 762 | recommendation, this exception is raised. |
| 763 | \end{excdesc} |
| 764 | |
| 765 | \begin{excdesc}{NotFoundErr} |
| 766 | Exception when a node does not exist in the referenced context. For |
| 767 | example, \method{NamedNodeMap.removeNamedItem()} will raise this if |
| 768 | the node passed in does not exist in the map. |
| 769 | \end{excdesc} |
| 770 | |
| 771 | \begin{excdesc}{NotSupportedErr} |
| 772 | Raised when the implementation does not support the requested type |
| 773 | of object or operation. |
| 774 | \end{excdesc} |
| 775 | |
| 776 | \begin{excdesc}{NoDataAllowedErr} |
| 777 | This is raised if data is specified for a node which does not |
| 778 | support data. |
| 779 | % XXX a better explanation is needed! |
| 780 | \end{excdesc} |
| 781 | |
| 782 | \begin{excdesc}{NoModificationAllowedErr} |
| 783 | Raised on attempts to modify an object where modifications are not |
| 784 | allowed (such as for read-only nodes). |
| 785 | \end{excdesc} |
| 786 | |
| 787 | \begin{excdesc}{SyntaxErr} |
| 788 | Raised when an invalid or illegal string is specified. |
| 789 | % XXX how is this different from InvalidCharacterErr ??? |
| 790 | \end{excdesc} |
| 791 | |
| 792 | \begin{excdesc}{WrongDocumentErr} |
| 793 | Raised when a node is inserted in a different document than it |
| 794 | currently belongs to, and the implementation does not support |
| 795 | migrating the node from one document to the other. |
| 796 | \end{excdesc} |
| 797 | |
| 798 | The exception codes defined in the DOM recommendation map to the |
| 799 | exceptions described above according to this table: |
| 800 | |
| 801 | \begin{tableii}{l|l}{constant}{Constant}{Exception} |
| 802 | \lineii{DOMSTRING_SIZE_ERR}{\exception{DomstringSizeErr}} |
| 803 | \lineii{HIERARCHY_REQUEST_ERR}{\exception{HierarchyRequestErr}} |
| 804 | \lineii{INDEX_SIZE_ERR}{\exception{IndexSizeErr}} |
| 805 | \lineii{INUSE_ATTRIBUTE_ERR}{\exception{InuseAttributeErr}} |
| 806 | \lineii{INVALID_ACCESS_ERR}{\exception{InvalidAccessErr}} |
| 807 | \lineii{INVALID_CHARACTER_ERR}{\exception{InvalidCharacterErr}} |
| 808 | \lineii{INVALID_MODIFICATION_ERR}{\exception{InvalidModificationErr}} |
| 809 | \lineii{INVALID_STATE_ERR}{\exception{InvalidStateErr}} |
| 810 | \lineii{NAMESPACE_ERR}{\exception{NamespaceErr}} |
| 811 | \lineii{NOT_FOUND_ERR}{\exception{NotFoundErr}} |
| 812 | \lineii{NOT_SUPPORTED_ERR}{\exception{NotSupportedErr}} |
| 813 | \lineii{NO_DATA_ALLOWED_ERR}{\exception{NoDataAllowedErr}} |
| 814 | \lineii{NO_MODIFICATION_ALLOWED_ERR}{\exception{NoModificationAllowedErr}} |
| 815 | \lineii{SYNTAX_ERR}{\exception{SyntaxErr}} |
| 816 | \lineii{WRONG_DOCUMENT_ERR}{\exception{WrongDocumentErr}} |
| 817 | \end{tableii} |
| 818 | |
| 819 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 820 | \subsection{Conformance \label{dom-conformance}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 821 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 822 | This section describes the conformance requirements and relationships |
| 823 | between the Python DOM API, the W3C DOM recommendations, and the OMG |
| 824 | IDL mapping for Python. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 825 | |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 826 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 827 | \subsubsection{Type Mapping \label{dom-type-mapping}} |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 828 | |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 829 | The primitive IDL types used in the DOM specification are mapped to |
| 830 | Python types according to the following table. |
| 831 | |
| 832 | \begin{tableii}{l|l}{code}{IDL Type}{Python Type} |
| 833 | \lineii{boolean}{\code{IntegerType} (with a value of \code{0} or \code{1})} |
| 834 | \lineii{int}{\code{IntegerType}} |
| 835 | \lineii{long int}{\code{IntegerType}} |
| 836 | \lineii{unsigned int}{\code{IntegerType}} |
| 837 | \end{tableii} |
| 838 | |
| 839 | Additionally, the \class{DOMString} defined in the recommendation is |
| 840 | mapped to a Python string or Unicode string. Applications should |
| 841 | be able to handle Unicode whenever a string is returned from the DOM. |
| 842 | |
| 843 | The IDL \keyword{null} value is mapped to \code{None}, which may be |
| 844 | accepted or provided by the implementation whenever \keyword{null} is |
| 845 | allowed by the API. |
| 846 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 847 | |
| 848 | \subsubsection{Accessor Methods \label{dom-accessor-methods}} |
| 849 | |
| 850 | The mapping from OMG IDL to Python defines accessor functions for IDL |
| 851 | \keyword{attribute} declarations in much the way the Java mapping |
| 852 | does. Mapping the IDL declarations |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 853 | |
| 854 | \begin{verbatim} |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 855 | readonly attribute string someValue; |
| 856 | attribute string anotherValue; |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 857 | \end{verbatim} |
| 858 | |
Andrew M. Kuchling | e7e03cd | 2001-06-23 16:26:44 +0000 | [diff] [blame] | 859 | yields three accessor functions: a ``get'' method for |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 860 | \member{someValue} (\method{_get_someValue()}), and ``get'' and |
| 861 | ``set'' methods for |
| 862 | \member{anotherValue} (\method{_get_anotherValue()} and |
| 863 | \method{_set_anotherValue()}). The mapping, in particular, does not |
| 864 | require that the IDL attributes are accessible as normal Python |
| 865 | attributes: \code{\var{object}.someValue} is \emph{not} required to |
| 866 | work, and may raise an \exception{AttributeError}. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 867 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 868 | The Python DOM API, however, \emph{does} require that normal attribute |
| 869 | access work. This means that the typical surrogates generated by |
| 870 | Python IDL compilers are not likely to work, and wrapper objects may |
| 871 | be needed on the client if the DOM objects are accessed via CORBA. |
| 872 | While this does require some additional consideration for CORBA DOM |
| 873 | clients, the implementers with experience using DOM over CORBA from |
| 874 | Python do not consider this a problem. Attributes that are declared |
| 875 | \keyword{readonly} may not restrict write access in all DOM |
| 876 | implementations. |
Fred Drake | 669d36f | 2000-10-24 02:34:45 +0000 | [diff] [blame] | 877 | |
Fred Drake | eaf57aa | 2000-11-29 06:10:22 +0000 | [diff] [blame] | 878 | Additionally, the accessor functions are not required. If provided, |
| 879 | they should take the form defined by the Python IDL mapping, but |
| 880 | these methods are considered unnecessary since the attributes are |
Fred Drake | 16942f2 | 2000-12-07 04:47:51 +0000 | [diff] [blame] | 881 | accessible directly from Python. ``Set'' accessors should never be |
| 882 | provided for \keyword{readonly} attributes. |