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