/Doc/library/xml.dom.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 1045 lines · 653 code · 392 blank · 0 comment · 0 complexity · 987410ef4ac78607c58d11122d32aa81 MD5 · raw file

  1. :mod:`xml.dom` --- The Document Object Model API
  2. ================================================
  3. .. module:: xml.dom
  4. :synopsis: Document Object Model API for Python.
  5. .. sectionauthor:: Paul Prescod <paul@prescod.net>
  6. .. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
  7. .. versionadded:: 2.0
  8. The Document Object Model, or "DOM," is a cross-language API from the World Wide
  9. Web Consortium (W3C) for accessing and modifying XML documents. A DOM
  10. implementation presents an XML document as a tree structure, or allows client
  11. code to build such a structure from scratch. It then gives access to the
  12. structure through a set of objects which provided well-known interfaces.
  13. The DOM is extremely useful for random-access applications. SAX only allows you
  14. a view of one bit of the document at a time. If you are looking at one SAX
  15. element, you have no access to another. If you are looking at a text node, you
  16. have no access to a containing element. When you write a SAX application, you
  17. need to keep track of your program's position in the document somewhere in your
  18. own code. SAX does not do it for you. Also, if you need to look ahead in the
  19. XML document, you are just out of luck.
  20. Some applications are simply impossible in an event driven model with no access
  21. to a tree. Of course you could build some sort of tree yourself in SAX events,
  22. but the DOM allows you to avoid writing that code. The DOM is a standard tree
  23. representation for XML data.
  24. The Document Object Model is being defined by the W3C in stages, or "levels" in
  25. their terminology. The Python mapping of the API is substantially based on the
  26. DOM Level 2 recommendation.
  27. .. XXX PyXML is dead...
  28. .. The mapping of the Level 3 specification, currently
  29. only available in draft form, is being developed by the `Python XML Special
  30. Interest Group <http://www.python.org/sigs/xml-sig/>`_ as part of the `PyXML
  31. package <http://pyxml.sourceforge.net/>`_. Refer to the documentation bundled
  32. with that package for information on the current state of DOM Level 3 support.
  33. .. What if your needs are somewhere between SAX and the DOM? Perhaps
  34. you cannot afford to load the entire tree in memory but you find the
  35. SAX model somewhat cumbersome and low-level. There is also a module
  36. called xml.dom.pulldom that allows you to build trees of only the
  37. parts of a document that you need structured access to. It also has
  38. features that allow you to find your way around the DOM.
  39. See http://www.prescod.net/python/pulldom
  40. DOM applications typically start by parsing some XML into a DOM. How this is
  41. accomplished is not covered at all by DOM Level 1, and Level 2 provides only
  42. limited improvements: There is a :class:`DOMImplementation` object class which
  43. provides access to :class:`Document` creation methods, but no way to access an
  44. XML reader/parser/Document builder in an implementation-independent way. There
  45. is also no well-defined way to access these methods without an existing
  46. :class:`Document` object. In Python, each DOM implementation will provide a
  47. function :func:`getDOMImplementation`. DOM Level 3 adds a Load/Store
  48. specification, which defines an interface to the reader, but this is not yet
  49. available in the Python standard library.
  50. Once you have a DOM document object, you can access the parts of your XML
  51. document through its properties and methods. These properties are defined in
  52. the DOM specification; this portion of the reference manual describes the
  53. interpretation of the specification in Python.
  54. The specification provided by the W3C defines the DOM API for Java, ECMAScript,
  55. and OMG IDL. The Python mapping defined here is based in large part on the IDL
  56. version of the specification, but strict compliance is not required (though
  57. implementations are free to support the strict mapping from IDL). See section
  58. :ref:`dom-conformance` for a detailed discussion of mapping requirements.
  59. .. seealso::
  60. `Document Object Model (DOM) Level 2 Specification <http://www.w3.org/TR/DOM-Level-2-Core/>`_
  61. The W3C recommendation upon which the Python DOM API is based.
  62. `Document Object Model (DOM) Level 1 Specification <http://www.w3.org/TR/REC-DOM-Level-1/>`_
  63. The W3C recommendation for the DOM supported by :mod:`xml.dom.minidom`.
  64. `Python Language Mapping Specification <http://www.omg.org/docs/formal/02-11-05.pdf>`_
  65. This specifies the mapping from OMG IDL to Python.
  66. Module Contents
  67. ---------------
  68. The :mod:`xml.dom` contains the following functions:
  69. .. function:: registerDOMImplementation(name, factory)
  70. Register the *factory* function with the name *name*. The factory function
  71. should return an object which implements the :class:`DOMImplementation`
  72. interface. The factory function can return the same object every time, or a new
  73. one for each call, as appropriate for the specific implementation (e.g. if that
  74. implementation supports some customization).
  75. .. function:: getDOMImplementation([name[, features]])
  76. Return a suitable DOM implementation. The *name* is either well-known, the
  77. module name of a DOM implementation, or ``None``. If it is not ``None``, imports
  78. the corresponding module and returns a :class:`DOMImplementation` object if the
  79. import succeeds. If no name is given, and if the environment variable
  80. :envvar:`PYTHON_DOM` is set, this variable is used to find the implementation.
  81. If name is not given, this examines the available implementations to find one
  82. with the required feature set. If no implementation can be found, raise an
  83. :exc:`ImportError`. The features list must be a sequence of ``(feature,
  84. version)`` pairs which are passed to the :meth:`hasFeature` method on available
  85. :class:`DOMImplementation` objects.
  86. Some convenience constants are also provided:
  87. .. data:: EMPTY_NAMESPACE
  88. The value used to indicate that no namespace is associated with a node in the
  89. DOM. This is typically found as the :attr:`namespaceURI` of a node, or used as
  90. the *namespaceURI* parameter to a namespaces-specific method.
  91. .. versionadded:: 2.2
  92. .. data:: XML_NAMESPACE
  93. The namespace URI associated with the reserved prefix ``xml``, as defined by
  94. `Namespaces in XML <http://www.w3.org/TR/REC-xml-names/>`_ (section 4).
  95. .. versionadded:: 2.2
  96. .. data:: XMLNS_NAMESPACE
  97. The namespace URI for namespace declarations, as defined by `Document Object
  98. Model (DOM) Level 2 Core Specification
  99. <http://www.w3.org/TR/DOM-Level-2-Core/core.html>`_ (section 1.1.8).
  100. .. versionadded:: 2.2
  101. .. data:: XHTML_NAMESPACE
  102. The URI of the XHTML namespace as defined by `XHTML 1.0: The Extensible
  103. HyperText Markup Language <http://www.w3.org/TR/xhtml1/>`_ (section 3.1.1).
  104. .. versionadded:: 2.2
  105. In addition, :mod:`xml.dom` contains a base :class:`Node` class and the DOM
  106. exception classes. The :class:`Node` class provided by this module does not
  107. implement any of the methods or attributes defined by the DOM specification;
  108. concrete DOM implementations must provide those. The :class:`Node` class
  109. provided as part of this module does provide the constants used for the
  110. :attr:`nodeType` attribute on concrete :class:`Node` objects; they are located
  111. within the class rather than at the module level to conform with the DOM
  112. specifications.
  113. .. Should the Node documentation go here?
  114. .. _dom-objects:
  115. Objects in the DOM
  116. ------------------
  117. The definitive documentation for the DOM is the DOM specification from the W3C.
  118. Note that DOM attributes may also be manipulated as nodes instead of as simple
  119. strings. It is fairly rare that you must do this, however, so this usage is not
  120. yet documented.
  121. +--------------------------------+-----------------------------------+---------------------------------+
  122. | Interface | Section | Purpose |
  123. +================================+===================================+=================================+
  124. | :class:`DOMImplementation` | :ref:`dom-implementation-objects` | Interface to the underlying |
  125. | | | implementation. |
  126. +--------------------------------+-----------------------------------+---------------------------------+
  127. | :class:`Node` | :ref:`dom-node-objects` | Base interface for most objects |
  128. | | | in a document. |
  129. +--------------------------------+-----------------------------------+---------------------------------+
  130. | :class:`NodeList` | :ref:`dom-nodelist-objects` | Interface for a sequence of |
  131. | | | nodes. |
  132. +--------------------------------+-----------------------------------+---------------------------------+
  133. | :class:`DocumentType` | :ref:`dom-documenttype-objects` | Information about the |
  134. | | | declarations needed to process |
  135. | | | a document. |
  136. +--------------------------------+-----------------------------------+---------------------------------+
  137. | :class:`Document` | :ref:`dom-document-objects` | Object which represents an |
  138. | | | entire document. |
  139. +--------------------------------+-----------------------------------+---------------------------------+
  140. | :class:`Element` | :ref:`dom-element-objects` | Element nodes in the document |
  141. | | | hierarchy. |
  142. +--------------------------------+-----------------------------------+---------------------------------+
  143. | :class:`Attr` | :ref:`dom-attr-objects` | Attribute value nodes on |
  144. | | | element nodes. |
  145. +--------------------------------+-----------------------------------+---------------------------------+
  146. | :class:`Comment` | :ref:`dom-comment-objects` | Representation of comments in |
  147. | | | the source document. |
  148. +--------------------------------+-----------------------------------+---------------------------------+
  149. | :class:`Text` | :ref:`dom-text-objects` | Nodes containing textual |
  150. | | | content from the document. |
  151. +--------------------------------+-----------------------------------+---------------------------------+
  152. | :class:`ProcessingInstruction` | :ref:`dom-pi-objects` | Processing instruction |
  153. | | | representation. |
  154. +--------------------------------+-----------------------------------+---------------------------------+
  155. An additional section describes the exceptions defined for working with the DOM
  156. in Python.
  157. .. _dom-implementation-objects:
  158. DOMImplementation Objects
  159. ^^^^^^^^^^^^^^^^^^^^^^^^^
  160. The :class:`DOMImplementation` interface provides a way for applications to
  161. determine the availability of particular features in the DOM they are using.
  162. DOM Level 2 added the ability to create new :class:`Document` and
  163. :class:`DocumentType` objects using the :class:`DOMImplementation` as well.
  164. .. method:: DOMImplementation.hasFeature(feature, version)
  165. Return true if the feature identified by the pair of strings *feature* and
  166. *version* is implemented.
  167. .. method:: DOMImplementation.createDocument(namespaceUri, qualifiedName, doctype)
  168. Return a new :class:`Document` object (the root of the DOM), with a child
  169. :class:`Element` object having the given *namespaceUri* and *qualifiedName*. The
  170. *doctype* must be a :class:`DocumentType` object created by
  171. :meth:`createDocumentType`, or ``None``. In the Python DOM API, the first two
  172. arguments can also be ``None`` in order to indicate that no :class:`Element`
  173. child is to be created.
  174. .. method:: DOMImplementation.createDocumentType(qualifiedName, publicId, systemId)
  175. Return a new :class:`DocumentType` object that encapsulates the given
  176. *qualifiedName*, *publicId*, and *systemId* strings, representing the
  177. information contained in an XML document type declaration.
  178. .. _dom-node-objects:
  179. Node Objects
  180. ^^^^^^^^^^^^
  181. All of the components of an XML document are subclasses of :class:`Node`.
  182. .. attribute:: Node.nodeType
  183. An integer representing the node type. Symbolic constants for the types are on
  184. the :class:`Node` object: :const:`ELEMENT_NODE`, :const:`ATTRIBUTE_NODE`,
  185. :const:`TEXT_NODE`, :const:`CDATA_SECTION_NODE`, :const:`ENTITY_NODE`,
  186. :const:`PROCESSING_INSTRUCTION_NODE`, :const:`COMMENT_NODE`,
  187. :const:`DOCUMENT_NODE`, :const:`DOCUMENT_TYPE_NODE`, :const:`NOTATION_NODE`.
  188. This is a read-only attribute.
  189. .. attribute:: Node.parentNode
  190. The parent of the current node, or ``None`` for the document node. The value is
  191. always a :class:`Node` object or ``None``. For :class:`Element` nodes, this
  192. will be the parent element, except for the root element, in which case it will
  193. be the :class:`Document` object. For :class:`Attr` nodes, this is always
  194. ``None``. This is a read-only attribute.
  195. .. attribute:: Node.attributes
  196. A :class:`NamedNodeMap` of attribute objects. Only elements have actual values
  197. for this; others provide ``None`` for this attribute. This is a read-only
  198. attribute.
  199. .. attribute:: Node.previousSibling
  200. The node that immediately precedes this one with the same parent. For
  201. instance the element with an end-tag that comes just before the *self*
  202. element's start-tag. Of course, XML documents are made up of more than just
  203. elements so the previous sibling could be text, a comment, or something else.
  204. If this node is the first child of the parent, this attribute will be
  205. ``None``. This is a read-only attribute.
  206. .. attribute:: Node.nextSibling
  207. The node that immediately follows this one with the same parent. See also
  208. :attr:`previousSibling`. If this is the last child of the parent, this
  209. attribute will be ``None``. This is a read-only attribute.
  210. .. attribute:: Node.childNodes
  211. A list of nodes contained within this node. This is a read-only attribute.
  212. .. attribute:: Node.firstChild
  213. The first child of the node, if there are any, or ``None``. This is a read-only
  214. attribute.
  215. .. attribute:: Node.lastChild
  216. The last child of the node, if there are any, or ``None``. This is a read-only
  217. attribute.
  218. .. attribute:: Node.localName
  219. The part of the :attr:`tagName` following the colon if there is one, else the
  220. entire :attr:`tagName`. The value is a string.
  221. .. attribute:: Node.prefix
  222. The part of the :attr:`tagName` preceding the colon if there is one, else the
  223. empty string. The value is a string, or ``None``
  224. .. attribute:: Node.namespaceURI
  225. The namespace associated with the element name. This will be a string or
  226. ``None``. This is a read-only attribute.
  227. .. attribute:: Node.nodeName
  228. This has a different meaning for each node type; see the DOM specification for
  229. details. You can always get the information you would get here from another
  230. property such as the :attr:`tagName` property for elements or the :attr:`name`
  231. property for attributes. For all node types, the value of this attribute will be
  232. either a string or ``None``. This is a read-only attribute.
  233. .. attribute:: Node.nodeValue
  234. This has a different meaning for each node type; see the DOM specification for
  235. details. The situation is similar to that with :attr:`nodeName`. The value is
  236. a string or ``None``.
  237. .. method:: Node.hasAttributes()
  238. Returns true if the node has any attributes.
  239. .. method:: Node.hasChildNodes()
  240. Returns true if the node has any child nodes.
  241. .. method:: Node.isSameNode(other)
  242. Returns true if *other* refers to the same node as this node. This is especially
  243. useful for DOM implementations which use any sort of proxy architecture (because
  244. more than one object can refer to the same node).
  245. .. note::
  246. This is based on a proposed DOM Level 3 API which is still in the "working
  247. draft" stage, but this particular interface appears uncontroversial. Changes
  248. from the W3C will not necessarily affect this method in the Python DOM interface
  249. (though any new W3C API for this would also be supported).
  250. .. method:: Node.appendChild(newChild)
  251. Add a new child node to this node at the end of the list of
  252. children, returning *newChild*. If the node was already in
  253. in the tree, it is removed first.
  254. .. method:: Node.insertBefore(newChild, refChild)
  255. Insert a new child node before an existing child. It must be the case that
  256. *refChild* is a child of this node; if not, :exc:`ValueError` is raised.
  257. *newChild* is returned. If *refChild* is ``None``, it inserts *newChild* at the
  258. end of the children's list.
  259. .. method:: Node.removeChild(oldChild)
  260. Remove a child node. *oldChild* must be a child of this node; if not,
  261. :exc:`ValueError` is raised. *oldChild* is returned on success. If *oldChild*
  262. will not be used further, its :meth:`unlink` method should be called.
  263. .. method:: Node.replaceChild(newChild, oldChild)
  264. Replace an existing node with a new node. It must be the case that *oldChild*
  265. is a child of this node; if not, :exc:`ValueError` is raised.
  266. .. method:: Node.normalize()
  267. Join adjacent text nodes so that all stretches of text are stored as single
  268. :class:`Text` instances. This simplifies processing text from a DOM tree for
  269. many applications.
  270. .. versionadded:: 2.1
  271. .. method:: Node.cloneNode(deep)
  272. Clone this node. Setting *deep* means to clone all child nodes as well. This
  273. returns the clone.
  274. .. _dom-nodelist-objects:
  275. NodeList Objects
  276. ^^^^^^^^^^^^^^^^
  277. A :class:`NodeList` represents a sequence of nodes. These objects are used in
  278. two ways in the DOM Core recommendation: the :class:`Element` objects provides
  279. one as its list of child nodes, and the :meth:`getElementsByTagName` and
  280. :meth:`getElementsByTagNameNS` methods of :class:`Node` return objects with this
  281. interface to represent query results.
  282. The DOM Level 2 recommendation defines one method and one attribute for these
  283. objects:
  284. .. method:: NodeList.item(i)
  285. Return the *i*'th item from the sequence, if there is one, or ``None``. The
  286. index *i* is not allowed to be less then zero or greater than or equal to the
  287. length of the sequence.
  288. .. attribute:: NodeList.length
  289. The number of nodes in the sequence.
  290. In addition, the Python DOM interface requires that some additional support is
  291. provided to allow :class:`NodeList` objects to be used as Python sequences. All
  292. :class:`NodeList` implementations must include support for :meth:`__len__` and
  293. :meth:`__getitem__`; this allows iteration over the :class:`NodeList` in
  294. :keyword:`for` statements and proper support for the :func:`len` built-in
  295. function.
  296. If a DOM implementation supports modification of the document, the
  297. :class:`NodeList` implementation must also support the :meth:`__setitem__` and
  298. :meth:`__delitem__` methods.
  299. .. _dom-documenttype-objects:
  300. DocumentType Objects
  301. ^^^^^^^^^^^^^^^^^^^^
  302. Information about the notations and entities declared by a document (including
  303. the external subset if the parser uses it and can provide the information) is
  304. available from a :class:`DocumentType` object. The :class:`DocumentType` for a
  305. document is available from the :class:`Document` object's :attr:`doctype`
  306. attribute; if there is no ``DOCTYPE`` declaration for the document, the
  307. document's :attr:`doctype` attribute will be set to ``None`` instead of an
  308. instance of this interface.
  309. :class:`DocumentType` is a specialization of :class:`Node`, and adds the
  310. following attributes:
  311. .. attribute:: DocumentType.publicId
  312. The public identifier for the external subset of the document type definition.
  313. This will be a string or ``None``.
  314. .. attribute:: DocumentType.systemId
  315. The system identifier for the external subset of the document type definition.
  316. This will be a URI as a string, or ``None``.
  317. .. attribute:: DocumentType.internalSubset
  318. A string giving the complete internal subset from the document. This does not
  319. include the brackets which enclose the subset. If the document has no internal
  320. subset, this should be ``None``.
  321. .. attribute:: DocumentType.name
  322. The name of the root element as given in the ``DOCTYPE`` declaration, if
  323. present.
  324. .. attribute:: DocumentType.entities
  325. This is a :class:`NamedNodeMap` giving the definitions of external entities.
  326. For entity names defined more than once, only the first definition is provided
  327. (others are ignored as required by the XML recommendation). This may be
  328. ``None`` if the information is not provided by the parser, or if no entities are
  329. defined.
  330. .. attribute:: DocumentType.notations
  331. This is a :class:`NamedNodeMap` giving the definitions of notations. For
  332. notation names defined more than once, only the first definition is provided
  333. (others are ignored as required by the XML recommendation). This may be
  334. ``None`` if the information is not provided by the parser, or if no notations
  335. are defined.
  336. .. _dom-document-objects:
  337. Document Objects
  338. ^^^^^^^^^^^^^^^^
  339. A :class:`Document` represents an entire XML document, including its constituent
  340. elements, attributes, processing instructions, comments etc. Remember that it
  341. inherits properties from :class:`Node`.
  342. .. attribute:: Document.documentElement
  343. The one and only root element of the document.
  344. .. method:: Document.createElement(tagName)
  345. Create and return a new element node. The element is not inserted into the
  346. document when it is created. You need to explicitly insert it with one of the
  347. other methods such as :meth:`insertBefore` or :meth:`appendChild`.
  348. .. method:: Document.createElementNS(namespaceURI, tagName)
  349. Create and return a new element with a namespace. The *tagName* may have a
  350. prefix. The element is not inserted into the document when it is created. You
  351. need to explicitly insert it with one of the other methods such as
  352. :meth:`insertBefore` or :meth:`appendChild`.
  353. .. method:: Document.createTextNode(data)
  354. Create and return a text node containing the data passed as a parameter. As
  355. with the other creation methods, this one does not insert the node into the
  356. tree.
  357. .. method:: Document.createComment(data)
  358. Create and return a comment node containing the data passed as a parameter. As
  359. with the other creation methods, this one does not insert the node into the
  360. tree.
  361. .. method:: Document.createProcessingInstruction(target, data)
  362. Create and return a processing instruction node containing the *target* and
  363. *data* passed as parameters. As with the other creation methods, this one does
  364. not insert the node into the tree.
  365. .. method:: Document.createAttribute(name)
  366. Create and return an attribute node. This method does not associate the
  367. attribute node with any particular element. You must use
  368. :meth:`setAttributeNode` on the appropriate :class:`Element` object to use the
  369. newly created attribute instance.
  370. .. method:: Document.createAttributeNS(namespaceURI, qualifiedName)
  371. Create and return an attribute node with a namespace. The *tagName* may have a
  372. prefix. This method does not associate the attribute node with any particular
  373. element. You must use :meth:`setAttributeNode` on the appropriate
  374. :class:`Element` object to use the newly created attribute instance.
  375. .. method:: Document.getElementsByTagName(tagName)
  376. Search for all descendants (direct children, children's children, etc.) with a
  377. particular element type name.
  378. .. method:: Document.getElementsByTagNameNS(namespaceURI, localName)
  379. Search for all descendants (direct children, children's children, etc.) with a
  380. particular namespace URI and localname. The localname is the part of the
  381. namespace after the prefix.
  382. .. _dom-element-objects:
  383. Element Objects
  384. ^^^^^^^^^^^^^^^
  385. :class:`Element` is a subclass of :class:`Node`, so inherits all the attributes
  386. of that class.
  387. .. attribute:: Element.tagName
  388. The element type name. In a namespace-using document it may have colons in it.
  389. The value is a string.
  390. .. method:: Element.getElementsByTagName(tagName)
  391. Same as equivalent method in the :class:`Document` class.
  392. .. method:: Element.getElementsByTagNameNS(namespaceURI, localName)
  393. Same as equivalent method in the :class:`Document` class.
  394. .. method:: Element.hasAttribute(name)
  395. Returns true if the element has an attribute named by *name*.
  396. .. method:: Element.hasAttributeNS(namespaceURI, localName)
  397. Returns true if the element has an attribute named by *namespaceURI* and
  398. *localName*.
  399. .. method:: Element.getAttribute(name)
  400. Return the value of the attribute named by *name* as a string. If no such
  401. attribute exists, an empty string is returned, as if the attribute had no value.
  402. .. method:: Element.getAttributeNode(attrname)
  403. Return the :class:`Attr` node for the attribute named by *attrname*.
  404. .. method:: Element.getAttributeNS(namespaceURI, localName)
  405. Return the value of the attribute named by *namespaceURI* and *localName* as a
  406. string. If no such attribute exists, an empty string is returned, as if the
  407. attribute had no value.
  408. .. method:: Element.getAttributeNodeNS(namespaceURI, localName)
  409. Return an attribute value as a node, given a *namespaceURI* and *localName*.
  410. .. method:: Element.removeAttribute(name)
  411. Remove an attribute by name. If there is no matching attribute, a
  412. :exc:`NotFoundErr` is raised.
  413. .. method:: Element.removeAttributeNode(oldAttr)
  414. Remove and return *oldAttr* from the attribute list, if present. If *oldAttr* is
  415. not present, :exc:`NotFoundErr` is raised.
  416. .. method:: Element.removeAttributeNS(namespaceURI, localName)
  417. Remove an attribute by name. Note that it uses a localName, not a qname. No
  418. exception is raised if there is no matching attribute.
  419. .. method:: Element.setAttribute(name, value)
  420. Set an attribute value from a string.
  421. .. method:: Element.setAttributeNode(newAttr)
  422. Add a new attribute node to the element, replacing an existing attribute if
  423. necessary if the :attr:`name` attribute matches. If a replacement occurs, the
  424. old attribute node will be returned. If *newAttr* is already in use,
  425. :exc:`InuseAttributeErr` will be raised.
  426. .. method:: Element.setAttributeNodeNS(newAttr)
  427. Add a new attribute node to the element, replacing an existing attribute if
  428. necessary if the :attr:`namespaceURI` and :attr:`localName` attributes match.
  429. If a replacement occurs, the old attribute node will be returned. If *newAttr*
  430. is already in use, :exc:`InuseAttributeErr` will be raised.
  431. .. method:: Element.setAttributeNS(namespaceURI, qname, value)
  432. Set an attribute value from a string, given a *namespaceURI* and a *qname*.
  433. Note that a qname is the whole attribute name. This is different than above.
  434. .. _dom-attr-objects:
  435. Attr Objects
  436. ^^^^^^^^^^^^
  437. :class:`Attr` inherits from :class:`Node`, so inherits all its attributes.
  438. .. attribute:: Attr.name
  439. The attribute name. In a namespace-using document it may have colons in it.
  440. .. attribute:: Attr.localName
  441. The part of the name following the colon if there is one, else the entire name.
  442. This is a read-only attribute.
  443. .. attribute:: Attr.prefix
  444. The part of the name preceding the colon if there is one, else the empty string.
  445. .. _dom-attributelist-objects:
  446. NamedNodeMap Objects
  447. ^^^^^^^^^^^^^^^^^^^^
  448. :class:`NamedNodeMap` does *not* inherit from :class:`Node`.
  449. .. attribute:: NamedNodeMap.length
  450. The length of the attribute list.
  451. .. method:: NamedNodeMap.item(index)
  452. Return an attribute with a particular index. The order you get the attributes
  453. in is arbitrary but will be consistent for the life of a DOM. Each item is an
  454. attribute node. Get its value with the :attr:`value` attribute.
  455. There are also experimental methods that give this class more mapping behavior.
  456. You can use them or you can use the standardized :meth:`getAttribute\*` family
  457. of methods on the :class:`Element` objects.
  458. .. _dom-comment-objects:
  459. Comment Objects
  460. ^^^^^^^^^^^^^^^
  461. :class:`Comment` represents a comment in the XML document. It is a subclass of
  462. :class:`Node`, but cannot have child nodes.
  463. .. attribute:: Comment.data
  464. The content of the comment as a string. The attribute contains all characters
  465. between the leading ``<!-``\ ``-`` and trailing ``-``\ ``->``, but does not
  466. include them.
  467. .. _dom-text-objects:
  468. Text and CDATASection Objects
  469. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  470. The :class:`Text` interface represents text in the XML document. If the parser
  471. and DOM implementation support the DOM's XML extension, portions of the text
  472. enclosed in CDATA marked sections are stored in :class:`CDATASection` objects.
  473. These two interfaces are identical, but provide different values for the
  474. :attr:`nodeType` attribute.
  475. These interfaces extend the :class:`Node` interface. They cannot have child
  476. nodes.
  477. .. attribute:: Text.data
  478. The content of the text node as a string.
  479. .. note::
  480. The use of a :class:`CDATASection` node does not indicate that the node
  481. represents a complete CDATA marked section, only that the content of the node
  482. was part of a CDATA section. A single CDATA section may be represented by more
  483. than one node in the document tree. There is no way to determine whether two
  484. adjacent :class:`CDATASection` nodes represent different CDATA marked sections.
  485. .. _dom-pi-objects:
  486. ProcessingInstruction Objects
  487. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  488. Represents a processing instruction in the XML document; this inherits from the
  489. :class:`Node` interface and cannot have child nodes.
  490. .. attribute:: ProcessingInstruction.target
  491. The content of the processing instruction up to the first whitespace character.
  492. This is a read-only attribute.
  493. .. attribute:: ProcessingInstruction.data
  494. The content of the processing instruction following the first whitespace
  495. character.
  496. .. _dom-exceptions:
  497. Exceptions
  498. ^^^^^^^^^^
  499. .. versionadded:: 2.1
  500. The DOM Level 2 recommendation defines a single exception, :exc:`DOMException`,
  501. and a number of constants that allow applications to determine what sort of
  502. error occurred. :exc:`DOMException` instances carry a :attr:`code` attribute
  503. that provides the appropriate value for the specific exception.
  504. The Python DOM interface provides the constants, but also expands the set of
  505. exceptions so that a specific exception exists for each of the exception codes
  506. defined by the DOM. The implementations must raise the appropriate specific
  507. exception, each of which carries the appropriate value for the :attr:`code`
  508. attribute.
  509. .. exception:: DOMException
  510. Base exception class used for all specific DOM exceptions. This exception class
  511. cannot be directly instantiated.
  512. .. exception:: DomstringSizeErr
  513. Raised when a specified range of text does not fit into a string. This is not
  514. known to be used in the Python DOM implementations, but may be received from DOM
  515. implementations not written in Python.
  516. .. exception:: HierarchyRequestErr
  517. Raised when an attempt is made to insert a node where the node type is not
  518. allowed.
  519. .. exception:: IndexSizeErr
  520. Raised when an index or size parameter to a method is negative or exceeds the
  521. allowed values.
  522. .. exception:: InuseAttributeErr
  523. Raised when an attempt is made to insert an :class:`Attr` node that is already
  524. present elsewhere in the document.
  525. .. exception:: InvalidAccessErr
  526. Raised if a parameter or an operation is not supported on the underlying object.
  527. .. exception:: InvalidCharacterErr
  528. This exception is raised when a string parameter contains a character that is
  529. not permitted in the context it's being used in by the XML 1.0 recommendation.
  530. For example, attempting to create an :class:`Element` node with a space in the
  531. element type name will cause this error to be raised.
  532. .. exception:: InvalidModificationErr
  533. Raised when an attempt is made to modify the type of a node.
  534. .. exception:: InvalidStateErr
  535. Raised when an attempt is made to use an object that is not defined or is no
  536. longer usable.
  537. .. exception:: NamespaceErr
  538. If an attempt is made to change any object in a way that is not permitted with
  539. regard to the `Namespaces in XML <http://www.w3.org/TR/REC-xml-names/>`_
  540. recommendation, this exception is raised.
  541. .. exception:: NotFoundErr
  542. Exception when a node does not exist in the referenced context. For example,
  543. :meth:`NamedNodeMap.removeNamedItem` will raise this if the node passed in does
  544. not exist in the map.
  545. .. exception:: NotSupportedErr
  546. Raised when the implementation does not support the requested type of object or
  547. operation.
  548. .. exception:: NoDataAllowedErr
  549. This is raised if data is specified for a node which does not support data.
  550. .. XXX a better explanation is needed!
  551. .. exception:: NoModificationAllowedErr
  552. Raised on attempts to modify an object where modifications are not allowed (such
  553. as for read-only nodes).
  554. .. exception:: SyntaxErr
  555. Raised when an invalid or illegal string is specified.
  556. .. XXX how is this different from InvalidCharacterErr?
  557. .. exception:: WrongDocumentErr
  558. Raised when a node is inserted in a different document than it currently belongs
  559. to, and the implementation does not support migrating the node from one document
  560. to the other.
  561. The exception codes defined in the DOM recommendation map to the exceptions
  562. described above according to this table:
  563. +--------------------------------------+---------------------------------+
  564. | Constant | Exception |
  565. +======================================+=================================+
  566. | :const:`DOMSTRING_SIZE_ERR` | :exc:`DomstringSizeErr` |
  567. +--------------------------------------+---------------------------------+
  568. | :const:`HIERARCHY_REQUEST_ERR` | :exc:`HierarchyRequestErr` |
  569. +--------------------------------------+---------------------------------+
  570. | :const:`INDEX_SIZE_ERR` | :exc:`IndexSizeErr` |
  571. +--------------------------------------+---------------------------------+
  572. | :const:`INUSE_ATTRIBUTE_ERR` | :exc:`InuseAttributeErr` |
  573. +--------------------------------------+---------------------------------+
  574. | :const:`INVALID_ACCESS_ERR` | :exc:`InvalidAccessErr` |
  575. +--------------------------------------+---------------------------------+
  576. | :const:`INVALID_CHARACTER_ERR` | :exc:`InvalidCharacterErr` |
  577. +--------------------------------------+---------------------------------+
  578. | :const:`INVALID_MODIFICATION_ERR` | :exc:`InvalidModificationErr` |
  579. +--------------------------------------+---------------------------------+
  580. | :const:`INVALID_STATE_ERR` | :exc:`InvalidStateErr` |
  581. +--------------------------------------+---------------------------------+
  582. | :const:`NAMESPACE_ERR` | :exc:`NamespaceErr` |
  583. +--------------------------------------+---------------------------------+
  584. | :const:`NOT_FOUND_ERR` | :exc:`NotFoundErr` |
  585. +--------------------------------------+---------------------------------+
  586. | :const:`NOT_SUPPORTED_ERR` | :exc:`NotSupportedErr` |
  587. +--------------------------------------+---------------------------------+
  588. | :const:`NO_DATA_ALLOWED_ERR` | :exc:`NoDataAllowedErr` |
  589. +--------------------------------------+---------------------------------+
  590. | :const:`NO_MODIFICATION_ALLOWED_ERR` | :exc:`NoModificationAllowedErr` |
  591. +--------------------------------------+---------------------------------+
  592. | :const:`SYNTAX_ERR` | :exc:`SyntaxErr` |
  593. +--------------------------------------+---------------------------------+
  594. | :const:`WRONG_DOCUMENT_ERR` | :exc:`WrongDocumentErr` |
  595. +--------------------------------------+---------------------------------+
  596. .. _dom-conformance:
  597. Conformance
  598. -----------
  599. This section describes the conformance requirements and relationships between
  600. the Python DOM API, the W3C DOM recommendations, and the OMG IDL mapping for
  601. Python.
  602. .. _dom-type-mapping:
  603. Type Mapping
  604. ^^^^^^^^^^^^
  605. The primitive IDL types used in the DOM specification are mapped to Python types
  606. according to the following table.
  607. +------------------+-------------------------------------------+
  608. | IDL Type | Python Type |
  609. +==================+===========================================+
  610. | ``boolean`` | ``IntegerType`` (with a value of ``0`` or |
  611. | | ``1``) |
  612. +------------------+-------------------------------------------+
  613. | ``int`` | ``IntegerType`` |
  614. +------------------+-------------------------------------------+
  615. | ``long int`` | ``IntegerType`` |
  616. +------------------+-------------------------------------------+
  617. | ``unsigned int`` | ``IntegerType`` |
  618. +------------------+-------------------------------------------+
  619. Additionally, the :class:`DOMString` defined in the recommendation is mapped to
  620. a Python string or Unicode string. Applications should be able to handle
  621. Unicode whenever a string is returned from the DOM.
  622. The IDL ``null`` value is mapped to ``None``, which may be accepted or
  623. provided by the implementation whenever ``null`` is allowed by the API.
  624. .. _dom-accessor-methods:
  625. Accessor Methods
  626. ^^^^^^^^^^^^^^^^
  627. The mapping from OMG IDL to Python defines accessor functions for IDL
  628. ``attribute`` declarations in much the way the Java mapping does.
  629. Mapping the IDL declarations ::
  630. readonly attribute string someValue;
  631. attribute string anotherValue;
  632. yields three accessor functions: a "get" method for :attr:`someValue`
  633. (:meth:`_get_someValue`), and "get" and "set" methods for :attr:`anotherValue`
  634. (:meth:`_get_anotherValue` and :meth:`_set_anotherValue`). The mapping, in
  635. particular, does not require that the IDL attributes are accessible as normal
  636. Python attributes: ``object.someValue`` is *not* required to work, and may
  637. raise an :exc:`AttributeError`.
  638. The Python DOM API, however, *does* require that normal attribute access work.
  639. This means that the typical surrogates generated by Python IDL compilers are not
  640. likely to work, and wrapper objects may be needed on the client if the DOM
  641. objects are accessed via CORBA. While this does require some additional
  642. consideration for CORBA DOM clients, the implementers with experience using DOM
  643. over CORBA from Python do not consider this a problem. Attributes that are
  644. declared ``readonly`` may not restrict write access in all DOM
  645. implementations.
  646. In the Python DOM API, accessor functions are not required. If provided, they
  647. should take the form defined by the Python IDL mapping, but these methods are
  648. considered unnecessary since the attributes are accessible directly from Python.
  649. "Set" accessors should never be provided for ``readonly`` attributes.
  650. The IDL definitions do not fully embody the requirements of the W3C DOM API,
  651. such as the notion of certain objects, such as the return value of
  652. :meth:`getElementsByTagName`, being "live". The Python DOM API does not require
  653. implementations to enforce such requirements.