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