blob: 5ddd6a82868683fe1cd1bed7d3042604a377d52b [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
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 Brandl116aa622007-08-15 14:28:22 +000011The Document Object Model, or "DOM," is a cross-language API from the World Wide
12Web Consortium (W3C) for accessing and modifying XML documents. A DOM
13implementation presents an XML document as a tree structure, or allows client
14code to build such a structure from scratch. It then gives access to the
15structure through a set of objects which provided well-known interfaces.
16
17The DOM is extremely useful for random-access applications. SAX only allows you
18a view of one bit of the document at a time. If you are looking at one SAX
19element, you have no access to another. If you are looking at a text node, you
20have no access to a containing element. When you write a SAX application, you
21need to keep track of your program's position in the document somewhere in your
22own code. SAX does not do it for you. Also, if you need to look ahead in the
23XML document, you are just out of luck.
24
25Some applications are simply impossible in an event driven model with no access
26to a tree. Of course you could build some sort of tree yourself in SAX events,
27but the DOM allows you to avoid writing that code. The DOM is a standard tree
28representation for XML data.
29
30The Document Object Model is being defined by the W3C in stages, or "levels" in
31their terminology. The Python mapping of the API is substantially based on the
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000032DOM 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 Brandl116aa622007-08-15 14:28:22 +000040
Christian Heimes5b5e81c2007-12-31 16:14:33 +000041.. 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 Brandl116aa622007-08-15 14:28:22 +000048
49DOM applications typically start by parsing some XML into a DOM. How this is
50accomplished is not covered at all by DOM Level 1, and Level 2 provides only
51limited improvements: There is a :class:`DOMImplementation` object class which
52provides access to :class:`Document` creation methods, but no way to access an
53XML reader/parser/Document builder in an implementation-independent way. There
54is also no well-defined way to access these methods without an existing
55:class:`Document` object. In Python, each DOM implementation will provide a
56function :func:`getDOMImplementation`. DOM Level 3 adds a Load/Store
57specification, which defines an interface to the reader, but this is not yet
58available in the Python standard library.
59
60Once you have a DOM document object, you can access the parts of your XML
61document through its properties and methods. These properties are defined in
62the DOM specification; this portion of the reference manual describes the
63interpretation of the specification in Python.
64
65The specification provided by the W3C defines the DOM API for Java, ECMAScript,
66and OMG IDL. The Python mapping defined here is based in large part on the IDL
67version of the specification, but strict compliance is not required (though
68implementations 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 Brandl116aa622007-08-15 14:28:22 +000080 `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
84Module Contents
85---------------
86
87The :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
113Some 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 Brandl116aa622007-08-15 14:28:22 +0000122
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 Brandl116aa622007-08-15 14:28:22 +0000128
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 Brandl116aa622007-08-15 14:28:22 +0000135
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 Brandl116aa622007-08-15 14:28:22 +0000141
142In addition, :mod:`xml.dom` contains a base :class:`Node` class and the DOM
143exception classes. The :class:`Node` class provided by this module does not
144implement any of the methods or attributes defined by the DOM specification;
145concrete DOM implementations must provide those. The :class:`Node` class
146provided as part of this module does provide the constants used for the
147:attr:`nodeType` attribute on concrete :class:`Node` objects; they are located
148within the class rather than at the module level to conform with the DOM
149specifications.
150
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000151.. Should the Node documentation go here?
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153
154.. _dom-objects:
155
156Objects in the DOM
157------------------
158
159The definitive documentation for the DOM is the DOM specification from the W3C.
160
161Note that DOM attributes may also be manipulated as nodes instead of as simple
162strings. It is fairly rare that you must do this, however, so this usage is not
163yet 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
200An additional section describes the exceptions defined for working with the DOM
201in Python.
202
203
204.. _dom-implementation-objects:
205
206DOMImplementation Objects
207^^^^^^^^^^^^^^^^^^^^^^^^^
208
209The :class:`DOMImplementation` interface provides a way for applications to
210determine the availability of particular features in the DOM they are using.
211DOM 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
240Node Objects
241^^^^^^^^^^^^
242
243All 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 Rossum0d3fb8a2007-11-26 23:23:18 +0000366 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 Brandl116aa622007-08-15 14:28:22 +0000369
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 Brandl116aa622007-08-15 14:28:22 +0000398
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
407NodeList Objects
408^^^^^^^^^^^^^^^^
409
410A :class:`NodeList` represents a sequence of nodes. These objects are used in
411two ways in the DOM Core recommendation: the :class:`Element` objects provides
412one as its list of child nodes, and the :meth:`getElementsByTagName` and
413:meth:`getElementsByTagNameNS` methods of :class:`Node` return objects with this
414interface to represent query results.
415
416The DOM Level 2 recommendation defines one method and one attribute for these
417objects:
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
431In addition, the Python DOM interface requires that some additional support is
432provided 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
436function.
437
438If 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
445DocumentType Objects
446^^^^^^^^^^^^^^^^^^^^
447
448Information about the notations and entities declared by a document (including
449the external subset if the parser uses it and can provide the information) is
450available from a :class:`DocumentType` object. The :class:`DocumentType` for a
451document is available from the :class:`Document` object's :attr:`doctype`
452attribute; if there is no ``DOCTYPE`` declaration for the document, the
453document's :attr:`doctype` attribute will be set to ``None`` instead of an
454instance of this interface.
455
456:class:`DocumentType` is a specialization of :class:`Node`, and adds the
457following 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
505Document Objects
506^^^^^^^^^^^^^^^^
507
508A :class:`Document` represents an entire XML document, including its constituent
509elements, attributes, processing instructions, comments etc. Remeber that it
510inherits 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
585Element Objects
586^^^^^^^^^^^^^^^
587
588:class:`Element` is a subclass of :class:`Node`, so inherits all the attributes
589of 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
644 Remove an attribute by name. No exception is raised if there is no matching
645 attribute.
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
689Attr 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
713NamedNodeMap 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
730There are also experimental methods that give this class more mapping behavior.
731You can use them or you can use the standardized :meth:`getAttribute\*` family
732of methods on the :class:`Element` objects.
733
734
735.. _dom-comment-objects:
736
737Comment 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
753Text and CDATASection Objects
754^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
755
756The :class:`Text` interface represents text in the XML document. If the parser
757and DOM implementation support the DOM's XML extension, portions of the text
758enclosed in CDATA marked sections are stored in :class:`CDATASection` objects.
759These two interfaces are identical, but provide different values for the
760:attr:`nodeType` attribute.
761
762These interfaces extend the :class:`Node` interface. They cannot have child
763nodes.
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
781ProcessingInstruction Objects
782^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
783
784Represents 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
802Exceptions
803^^^^^^^^^^
804
Georg Brandl116aa622007-08-15 14:28:22 +0000805The DOM Level 2 recommendation defines a single exception, :exc:`DOMException`,
806and a number of constants that allow applications to determine what sort of
807error occurred. :exc:`DOMException` instances carry a :attr:`code` attribute
808that provides the appropriate value for the specific exception.
809
810The Python DOM interface provides the constants, but also expands the set of
811exceptions so that a specific exception exists for each of the exception codes
812defined by the DOM. The implementations must raise the appropriate specific
813exception, each of which carries the appropriate value for the :attr:`code`
814attribute.
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 Heimes5b5e81c2007-12-31 16:14:33 +0000896 .. XXX a better explanation is needed!
Georg Brandl116aa622007-08-15 14:28:22 +0000897
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 Heimes5b5e81c2007-12-31 16:14:33 +0000909 .. XXX how is this different from InvalidCharacterErr?
Georg Brandl116aa622007-08-15 14:28:22 +0000910
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
918The exception codes defined in the DOM recommendation map to the exceptions
919described 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
958Conformance
959-----------
960
961This section describes the conformance requirements and relationships between
962the Python DOM API, the W3C DOM recommendations, and the OMG IDL mapping for
963Python.
964
965
966.. _dom-type-mapping:
967
968Type Mapping
969^^^^^^^^^^^^
970
971The primitive IDL types used in the DOM specification are mapped to Python types
972according 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
987Additionally, the :class:`DOMString` defined in the recommendation is mapped to
Georg Brandlf6945182008-02-01 11:56:49 +0000988a bytes or string object. Applications should be able to handle
Georg Brandl116aa622007-08-15 14:28:22 +0000989Unicode whenever a string is returned from the DOM.
990
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000991The IDL ``null`` value is mapped to ``None``, which may be accepted or
992provided by the implementation whenever ``null`` is allowed by the API.
Georg Brandl116aa622007-08-15 14:28:22 +0000993
994
995.. _dom-accessor-methods:
996
997Accessor Methods
998^^^^^^^^^^^^^^^^
999
1000The mapping from OMG IDL to Python defines accessor functions for IDL
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001001``attribute`` declarations in much the way the Java mapping does.
Georg Brandl116aa622007-08-15 14:28:22 +00001002Mapping the IDL declarations ::
1003
1004 readonly attribute string someValue;
1005 attribute string anotherValue;
1006
1007yields 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
1010particular, does not require that the IDL attributes are accessible as normal
1011Python attributes: ``object.someValue`` is *not* required to work, and may
1012raise an :exc:`AttributeError`.
1013
1014The Python DOM API, however, *does* require that normal attribute access work.
1015This means that the typical surrogates generated by Python IDL compilers are not
1016likely to work, and wrapper objects may be needed on the client if the DOM
1017objects are accessed via CORBA. While this does require some additional
1018consideration for CORBA DOM clients, the implementers with experience using DOM
1019over CORBA from Python do not consider this a problem. Attributes that are
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001020declared ``readonly`` may not restrict write access in all DOM
Georg Brandl116aa622007-08-15 14:28:22 +00001021implementations.
1022
1023In the Python DOM API, accessor functions are not required. If provided, they
1024should take the form defined by the Python IDL mapping, but these methods are
1025considered unnecessary since the attributes are accessible directly from Python.
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001026"Set" accessors should never be provided for ``readonly`` attributes.
Georg Brandl116aa622007-08-15 14:28:22 +00001027
1028The IDL definitions do not fully embody the requirements of the W3C DOM API,
1029such as the notion of certain objects, such as the return value of
1030:meth:`getElementsByTagName`, being "live". The Python DOM API does not require
1031implementations to enforce such requirements.
1032