cpython/Doc/library/xml.dom.rst

1034 lines
39 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:22 -03:00
:mod:`xml.dom` --- The Document Object Model API
================================================
.. module:: xml.dom
:synopsis: Document Object Model API for Python.
2007-08-15 11:28:22 -03:00
.. sectionauthor:: Paul Prescod <paul@prescod.net>
.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
**Source code:** :source:`Lib/xml/dom/__init__.py`
--------------
2007-08-15 11:28:22 -03:00
The Document Object Model, or "DOM," is a cross-language API from the World Wide
Web Consortium (W3C) for accessing and modifying XML documents. A DOM
implementation presents an XML document as a tree structure, or allows client
code to build such a structure from scratch. It then gives access to the
structure through a set of objects which provided well-known interfaces.
The DOM is extremely useful for random-access applications. SAX only allows you
a view of one bit of the document at a time. If you are looking at one SAX
element, you have no access to another. If you are looking at a text node, you
have no access to a containing element. When you write a SAX application, you
need to keep track of your program's position in the document somewhere in your
own code. SAX does not do it for you. Also, if you need to look ahead in the
XML document, you are just out of luck.
Some applications are simply impossible in an event driven model with no access
to a tree. Of course you could build some sort of tree yourself in SAX events,
but the DOM allows you to avoid writing that code. The DOM is a standard tree
representation for XML data.
The Document Object Model is being defined by the W3C in stages, or "levels" in
their terminology. The Python mapping of the API is substantially based on the
Merged revisions 59107-59186 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk (Note: some conflicts in the PCbuild9 directory reverted. Sorry Christian!) ........ r59120 | christian.heimes | 2007-11-22 03:21:16 -0800 (Thu, 22 Nov 2007) | 3 lines Backport of the PCbuild9 directory from the py3k branch. I've finished the last task for the PCbuild9 directory today. I don't think there is much left to do. Now you can all play around with the shiny new VS 2008 and try the PGO builds. I was able to get a speed improvement of about 10% on py3k. Have fun! :) ........ r59126 | brett.cannon | 2007-11-22 16:06:51 -0800 (Thu, 22 Nov 2007) | 2 lines Fix a bug in the test for using __loader__.get_data(). ........ r59131 | christian.heimes | 2007-11-22 23:05:03 -0800 (Thu, 22 Nov 2007) | 1 line Backport of PCbuild9 fixes from py3k r59130 ........ r59132 | christian.heimes | 2007-11-23 01:10:36 -0800 (Fri, 23 Nov 2007) | 2 lines Applied patch #1754273 and #1754271 from Thomas Glee The patches are adding deprecation warnings for back ticks and <> ........ r59133 | christian.heimes | 2007-11-23 04:12:02 -0800 (Fri, 23 Nov 2007) | 2 lines Fixed problems in the last commit. Filenames and line numbers weren't reported correctly. Backquotes still don't report the correct file. The AST nodes only contain the line number but not the file name. ........ r59134 | christian.heimes | 2007-11-23 04:16:35 -0800 (Fri, 23 Nov 2007) | 1 line How did the comment get there? ........ r59135 | christian.heimes | 2007-11-23 05:25:31 -0800 (Fri, 23 Nov 2007) | 1 line And yet another fix for the patch. Paul Moore has send me a note that I've missed a declaration. The additional code has moved the declaration in the middle of the block. ........ r59136 | andrew.kuchling | 2007-11-23 05:37:39 -0800 (Fri, 23 Nov 2007) | 1 line Add item ........ r59137 | skip.montanaro | 2007-11-23 09:08:35 -0800 (Fri, 23 Nov 2007) | 2 lines Make trace and doctest play nice together (issue 1429818). Will backport. ........ r59139 | skip.montanaro | 2007-11-23 09:12:47 -0800 (Fri, 23 Nov 2007) | 1 line issue 1429818 ........ r59144 | facundo.batista | 2007-11-23 09:59:00 -0800 (Fri, 23 Nov 2007) | 10 lines Major change in the internal structure of the Decimal number: now it does not store the mantissa as a tuple of numbers, but as a string. This avoids a lot of conversions, and achieves a speedup of 40%. The API remains intact. Thanks Mark Dickinson. ........ r59146 | facundo.batista | 2007-11-23 10:14:50 -0800 (Fri, 23 Nov 2007) | 3 lines Test cases from Cowlishaw, v2.57. All are pased cleanly. ........ r59156 | christian.heimes | 2007-11-23 17:36:02 -0800 (Fri, 23 Nov 2007) | 2 lines Added filename to compiling struct based on Martin's suggestion. I'm wonder why I was trying to add the filename to the node all the time. The compiling struct is more obvious. ........ r59158 | christian.heimes | 2007-11-23 17:53:59 -0800 (Fri, 23 Nov 2007) | 2 lines Backport of fixes from py3k branch svn merge -r59131:HEAD ../../py3k/PCbuild9/ . ........ r59159 | skip.montanaro | 2007-11-23 20:29:08 -0800 (Fri, 23 Nov 2007) | 1 line revert change that breaks test_doctest (which I forgot to run - sorry) ........ r59162 | skip.montanaro | 2007-11-23 20:31:15 -0800 (Fri, 23 Nov 2007) | 1 line revert ........ r59164 | georg.brandl | 2007-11-24 03:31:46 -0800 (Sat, 24 Nov 2007) | 3 lines #1344: document that you need to open std{in,out,err} with PIPE if you want communicate() to work as described. ........ r59165 | georg.brandl | 2007-11-24 03:39:13 -0800 (Sat, 24 Nov 2007) | 2 lines #1467: fix documentation for TestResult.add{Error,Failure}. ........ r59166 | georg.brandl | 2007-11-24 03:42:14 -0800 (Sat, 24 Nov 2007) | 2 lines #1355: remove mention of PyXML from xml.dom docs. ........ r59169 | amaury.forgeotdarc | 2007-11-24 05:20:22 -0800 (Sat, 24 Nov 2007) | 2 lines Warning "<> not supported in 3.x" should be enabled only when the -3 option is set. ........ r59170 | amaury.forgeotdarc | 2007-11-24 05:44:17 -0800 (Sat, 24 Nov 2007) | 3 lines Issue #1445: Fix a SystemError when accessing the ``cell_contents`` attribute of an empty cell object. Now a ValueError is raised. ........ r59172 | georg.brandl | 2007-11-24 05:56:09 -0800 (Sat, 24 Nov 2007) | 3 lines #1735632: add O_NOATIME constant to os module. Also document a few other O_ constants that were missing from documentation. ........ r59173 | skip.montanaro | 2007-11-24 06:30:47 -0800 (Sat, 24 Nov 2007) | 1 line back in these go - thanks to Titus Brown for the fix ........ r59176 | martin.v.loewis | 2007-11-24 10:33:40 -0800 (Sat, 24 Nov 2007) | 2 lines Bug #1494: Document that appendChild removes first. ........ r59186 | guido.van.rossum | 2007-11-26 14:16:49 -0800 (Mon, 26 Nov 2007) | 2 lines A thread-less variant of brownian.py, submitted by Michele Simoniato. ........
2007-11-26 19:23:18 -04:00
DOM Level 2 recommendation.
.. What if your needs are somewhere between SAX and the DOM? Perhaps
you cannot afford to load the entire tree in memory but you find the
SAX model somewhat cumbersome and low-level. There is also a module
called xml.dom.pulldom that allows you to build trees of only the
parts of a document that you need structured access to. It also has
features that allow you to find your way around the DOM.
See http://www.prescod.net/python/pulldom
2007-08-15 11:28:22 -03:00
DOM applications typically start by parsing some XML into a DOM. How this is
accomplished is not covered at all by DOM Level 1, and Level 2 provides only
limited improvements: There is a :class:`DOMImplementation` object class which
provides access to :class:`Document` creation methods, but no way to access an
XML reader/parser/Document builder in an implementation-independent way. There
is also no well-defined way to access these methods without an existing
:class:`Document` object. In Python, each DOM implementation will provide a
function :func:`getDOMImplementation`. DOM Level 3 adds a Load/Store
specification, which defines an interface to the reader, but this is not yet
available in the Python standard library.
Once you have a DOM document object, you can access the parts of your XML
document through its properties and methods. These properties are defined in
the DOM specification; this portion of the reference manual describes the
interpretation of the specification in Python.
The specification provided by the W3C defines the DOM API for Java, ECMAScript,
and OMG IDL. The Python mapping defined here is based in large part on the IDL
version of the specification, but strict compliance is not required (though
implementations are free to support the strict mapping from IDL). See section
:ref:`dom-conformance` for a detailed discussion of mapping requirements.
.. seealso::
`Document Object Model (DOM) Level 2 Specification <https://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/>`_
2007-08-15 11:28:22 -03:00
The W3C recommendation upon which the Python DOM API is based.
`Document Object Model (DOM) Level 1 Specification <https://www.w3.org/TR/REC-DOM-Level-1/>`_
2007-08-15 11:28:22 -03:00
The W3C recommendation for the DOM supported by :mod:`xml.dom.minidom`.
2022-02-14 17:49:11 -04:00
`Python Language Mapping Specification <https://www.omg.org/spec/PYTH/1.2/PDF>`_
2007-08-15 11:28:22 -03:00
This specifies the mapping from OMG IDL to Python.
Module Contents
---------------
The :mod:`xml.dom` contains the following functions:
.. function:: registerDOMImplementation(name, factory)
Register the *factory* function with the name *name*. The factory function
should return an object which implements the :class:`DOMImplementation`
interface. The factory function can return the same object every time, or a new
one for each call, as appropriate for the specific implementation (e.g. if that
implementation supports some customization).
.. function:: getDOMImplementation(name=None, features=())
2007-08-15 11:28:22 -03:00
Return a suitable DOM implementation. The *name* is either well-known, the
module name of a DOM implementation, or ``None``. If it is not ``None``, imports
the corresponding module and returns a :class:`DOMImplementation` object if the
import succeeds. If no name is given, and if the environment variable
:envvar:`PYTHON_DOM` is set, this variable is used to find the implementation.
If name is not given, this examines the available implementations to find one
with the required feature set. If no implementation can be found, raise an
:exc:`ImportError`. The features list must be a sequence of ``(feature,
version)`` pairs which are passed to the :meth:`hasFeature` method on available
:class:`DOMImplementation` objects.
Some convenience constants are also provided:
.. data:: EMPTY_NAMESPACE
The value used to indicate that no namespace is associated with a node in the
DOM. This is typically found as the :attr:`namespaceURI` of a node, or used as
the *namespaceURI* parameter to a namespaces-specific method.
.. data:: XML_NAMESPACE
The namespace URI associated with the reserved prefix ``xml``, as defined by
`Namespaces in XML <https://www.w3.org/TR/REC-xml-names/>`_ (section 4).
2007-08-15 11:28:22 -03:00
.. data:: XMLNS_NAMESPACE
The namespace URI for namespace declarations, as defined by `Document Object
Model (DOM) Level 2 Core Specification
<https://www.w3.org/TR/DOM-Level-2-Core/core.html>`_ (section 1.1.8).
2007-08-15 11:28:22 -03:00
.. data:: XHTML_NAMESPACE
The URI of the XHTML namespace as defined by `XHTML 1.0: The Extensible
HyperText Markup Language <https://www.w3.org/TR/xhtml1/>`_ (section 3.1.1).
2007-08-15 11:28:22 -03:00
In addition, :mod:`xml.dom` contains a base :class:`Node` class and the DOM
exception classes. The :class:`Node` class provided by this module does not
implement any of the methods or attributes defined by the DOM specification;
concrete DOM implementations must provide those. The :class:`Node` class
provided as part of this module does provide the constants used for the
:attr:`nodeType` attribute on concrete :class:`Node` objects; they are located
within the class rather than at the module level to conform with the DOM
specifications.
.. Should the Node documentation go here?
2007-08-15 11:28:22 -03:00
.. _dom-objects:
Objects in the DOM
------------------
The definitive documentation for the DOM is the DOM specification from the W3C.
Note that DOM attributes may also be manipulated as nodes instead of as simple
strings. It is fairly rare that you must do this, however, so this usage is not
yet documented.
+--------------------------------+-----------------------------------+---------------------------------+
| Interface | Section | Purpose |
+================================+===================================+=================================+
| :class:`DOMImplementation` | :ref:`dom-implementation-objects` | Interface to the underlying |
| | | implementation. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`Node` | :ref:`dom-node-objects` | Base interface for most objects |
| | | in a document. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`NodeList` | :ref:`dom-nodelist-objects` | Interface for a sequence of |
| | | nodes. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`DocumentType` | :ref:`dom-documenttype-objects` | Information about the |
| | | declarations needed to process |
| | | a document. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`Document` | :ref:`dom-document-objects` | Object which represents an |
| | | entire document. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`Element` | :ref:`dom-element-objects` | Element nodes in the document |
| | | hierarchy. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`Attr` | :ref:`dom-attr-objects` | Attribute value nodes on |
| | | element nodes. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`Comment` | :ref:`dom-comment-objects` | Representation of comments in |
| | | the source document. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`Text` | :ref:`dom-text-objects` | Nodes containing textual |
| | | content from the document. |
+--------------------------------+-----------------------------------+---------------------------------+
| :class:`ProcessingInstruction` | :ref:`dom-pi-objects` | Processing instruction |
| | | representation. |
+--------------------------------+-----------------------------------+---------------------------------+
An additional section describes the exceptions defined for working with the DOM
in Python.
.. _dom-implementation-objects:
DOMImplementation Objects
^^^^^^^^^^^^^^^^^^^^^^^^^
The :class:`DOMImplementation` interface provides a way for applications to
determine the availability of particular features in the DOM they are using.
DOM Level 2 added the ability to create new :class:`Document` and
:class:`DocumentType` objects using the :class:`DOMImplementation` as well.
.. method:: DOMImplementation.hasFeature(feature, version)
Return ``True`` if the feature identified by the pair of strings *feature* and
2007-08-15 11:28:22 -03:00
*version* is implemented.
.. method:: DOMImplementation.createDocument(namespaceUri, qualifiedName, doctype)
Return a new :class:`Document` object (the root of the DOM), with a child
:class:`Element` object having the given *namespaceUri* and *qualifiedName*. The
*doctype* must be a :class:`DocumentType` object created by
:meth:`createDocumentType`, or ``None``. In the Python DOM API, the first two
arguments can also be ``None`` in order to indicate that no :class:`Element`
child is to be created.
.. method:: DOMImplementation.createDocumentType(qualifiedName, publicId, systemId)
Return a new :class:`DocumentType` object that encapsulates the given
*qualifiedName*, *publicId*, and *systemId* strings, representing the
information contained in an XML document type declaration.
.. _dom-node-objects:
Node Objects
^^^^^^^^^^^^
All of the components of an XML document are subclasses of :class:`Node`.
.. attribute:: Node.nodeType
An integer representing the node type. Symbolic constants for the types are on
the :class:`Node` object: :const:`ELEMENT_NODE`, :const:`ATTRIBUTE_NODE`,
:const:`TEXT_NODE`, :const:`CDATA_SECTION_NODE`, :const:`ENTITY_NODE`,
:const:`PROCESSING_INSTRUCTION_NODE`, :const:`COMMENT_NODE`,
:const:`DOCUMENT_NODE`, :const:`DOCUMENT_TYPE_NODE`, :const:`NOTATION_NODE`.
This is a read-only attribute.
.. attribute:: Node.parentNode
The parent of the current node, or ``None`` for the document node. The value is
always a :class:`Node` object or ``None``. For :class:`Element` nodes, this
will be the parent element, except for the root element, in which case it will
be the :class:`Document` object. For :class:`Attr` nodes, this is always
``None``. This is a read-only attribute.
.. attribute:: Node.attributes
A :class:`NamedNodeMap` of attribute objects. Only elements have actual values
for this; others provide ``None`` for this attribute. This is a read-only
attribute.
.. attribute:: Node.previousSibling
The node that immediately precedes this one with the same parent. For
instance the element with an end-tag that comes just before the *self*
element's start-tag. Of course, XML documents are made up of more than just
elements so the previous sibling could be text, a comment, or something else.
If this node is the first child of the parent, this attribute will be
``None``. This is a read-only attribute.
.. attribute:: Node.nextSibling
The node that immediately follows this one with the same parent. See also
:attr:`previousSibling`. If this is the last child of the parent, this
attribute will be ``None``. This is a read-only attribute.
.. attribute:: Node.childNodes
A list of nodes contained within this node. This is a read-only attribute.
.. attribute:: Node.firstChild
The first child of the node, if there are any, or ``None``. This is a read-only
attribute.
.. attribute:: Node.lastChild
The last child of the node, if there are any, or ``None``. This is a read-only
attribute.
.. attribute:: Node.localName
The part of the :attr:`tagName` following the colon if there is one, else the
entire :attr:`tagName`. The value is a string.
.. attribute:: Node.prefix
The part of the :attr:`tagName` preceding the colon if there is one, else the
empty string. The value is a string, or ``None``.
2007-08-15 11:28:22 -03:00
.. attribute:: Node.namespaceURI
The namespace associated with the element name. This will be a string or
``None``. This is a read-only attribute.
.. attribute:: Node.nodeName
This has a different meaning for each node type; see the DOM specification for
details. You can always get the information you would get here from another
property such as the :attr:`tagName` property for elements or the :attr:`name`
property for attributes. For all node types, the value of this attribute will be
either a string or ``None``. This is a read-only attribute.
.. attribute:: Node.nodeValue
This has a different meaning for each node type; see the DOM specification for
details. The situation is similar to that with :attr:`nodeName`. The value is
a string or ``None``.
.. method:: Node.hasAttributes()
Return ``True`` if the node has any attributes.
2007-08-15 11:28:22 -03:00
.. method:: Node.hasChildNodes()
Return ``True`` if the node has any child nodes.
2007-08-15 11:28:22 -03:00
.. method:: Node.isSameNode(other)
Return ``True`` if *other* refers to the same node as this node. This is especially
2007-08-15 11:28:22 -03:00
useful for DOM implementations which use any sort of proxy architecture (because
more than one object can refer to the same node).
.. note::
This is based on a proposed DOM Level 3 API which is still in the "working
draft" stage, but this particular interface appears uncontroversial. Changes
from the W3C will not necessarily affect this method in the Python DOM interface
(though any new W3C API for this would also be supported).
.. method:: Node.appendChild(newChild)
Merged revisions 59107-59186 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk (Note: some conflicts in the PCbuild9 directory reverted. Sorry Christian!) ........ r59120 | christian.heimes | 2007-11-22 03:21:16 -0800 (Thu, 22 Nov 2007) | 3 lines Backport of the PCbuild9 directory from the py3k branch. I've finished the last task for the PCbuild9 directory today. I don't think there is much left to do. Now you can all play around with the shiny new VS 2008 and try the PGO builds. I was able to get a speed improvement of about 10% on py3k. Have fun! :) ........ r59126 | brett.cannon | 2007-11-22 16:06:51 -0800 (Thu, 22 Nov 2007) | 2 lines Fix a bug in the test for using __loader__.get_data(). ........ r59131 | christian.heimes | 2007-11-22 23:05:03 -0800 (Thu, 22 Nov 2007) | 1 line Backport of PCbuild9 fixes from py3k r59130 ........ r59132 | christian.heimes | 2007-11-23 01:10:36 -0800 (Fri, 23 Nov 2007) | 2 lines Applied patch #1754273 and #1754271 from Thomas Glee The patches are adding deprecation warnings for back ticks and <> ........ r59133 | christian.heimes | 2007-11-23 04:12:02 -0800 (Fri, 23 Nov 2007) | 2 lines Fixed problems in the last commit. Filenames and line numbers weren't reported correctly. Backquotes still don't report the correct file. The AST nodes only contain the line number but not the file name. ........ r59134 | christian.heimes | 2007-11-23 04:16:35 -0800 (Fri, 23 Nov 2007) | 1 line How did the comment get there? ........ r59135 | christian.heimes | 2007-11-23 05:25:31 -0800 (Fri, 23 Nov 2007) | 1 line And yet another fix for the patch. Paul Moore has send me a note that I've missed a declaration. The additional code has moved the declaration in the middle of the block. ........ r59136 | andrew.kuchling | 2007-11-23 05:37:39 -0800 (Fri, 23 Nov 2007) | 1 line Add item ........ r59137 | skip.montanaro | 2007-11-23 09:08:35 -0800 (Fri, 23 Nov 2007) | 2 lines Make trace and doctest play nice together (issue 1429818). Will backport. ........ r59139 | skip.montanaro | 2007-11-23 09:12:47 -0800 (Fri, 23 Nov 2007) | 1 line issue 1429818 ........ r59144 | facundo.batista | 2007-11-23 09:59:00 -0800 (Fri, 23 Nov 2007) | 10 lines Major change in the internal structure of the Decimal number: now it does not store the mantissa as a tuple of numbers, but as a string. This avoids a lot of conversions, and achieves a speedup of 40%. The API remains intact. Thanks Mark Dickinson. ........ r59146 | facundo.batista | 2007-11-23 10:14:50 -0800 (Fri, 23 Nov 2007) | 3 lines Test cases from Cowlishaw, v2.57. All are pased cleanly. ........ r59156 | christian.heimes | 2007-11-23 17:36:02 -0800 (Fri, 23 Nov 2007) | 2 lines Added filename to compiling struct based on Martin's suggestion. I'm wonder why I was trying to add the filename to the node all the time. The compiling struct is more obvious. ........ r59158 | christian.heimes | 2007-11-23 17:53:59 -0800 (Fri, 23 Nov 2007) | 2 lines Backport of fixes from py3k branch svn merge -r59131:HEAD ../../py3k/PCbuild9/ . ........ r59159 | skip.montanaro | 2007-11-23 20:29:08 -0800 (Fri, 23 Nov 2007) | 1 line revert change that breaks test_doctest (which I forgot to run - sorry) ........ r59162 | skip.montanaro | 2007-11-23 20:31:15 -0800 (Fri, 23 Nov 2007) | 1 line revert ........ r59164 | georg.brandl | 2007-11-24 03:31:46 -0800 (Sat, 24 Nov 2007) | 3 lines #1344: document that you need to open std{in,out,err} with PIPE if you want communicate() to work as described. ........ r59165 | georg.brandl | 2007-11-24 03:39:13 -0800 (Sat, 24 Nov 2007) | 2 lines #1467: fix documentation for TestResult.add{Error,Failure}. ........ r59166 | georg.brandl | 2007-11-24 03:42:14 -0800 (Sat, 24 Nov 2007) | 2 lines #1355: remove mention of PyXML from xml.dom docs. ........ r59169 | amaury.forgeotdarc | 2007-11-24 05:20:22 -0800 (Sat, 24 Nov 2007) | 2 lines Warning "<> not supported in 3.x" should be enabled only when the -3 option is set. ........ r59170 | amaury.forgeotdarc | 2007-11-24 05:44:17 -0800 (Sat, 24 Nov 2007) | 3 lines Issue #1445: Fix a SystemError when accessing the ``cell_contents`` attribute of an empty cell object. Now a ValueError is raised. ........ r59172 | georg.brandl | 2007-11-24 05:56:09 -0800 (Sat, 24 Nov 2007) | 3 lines #1735632: add O_NOATIME constant to os module. Also document a few other O_ constants that were missing from documentation. ........ r59173 | skip.montanaro | 2007-11-24 06:30:47 -0800 (Sat, 24 Nov 2007) | 1 line back in these go - thanks to Titus Brown for the fix ........ r59176 | martin.v.loewis | 2007-11-24 10:33:40 -0800 (Sat, 24 Nov 2007) | 2 lines Bug #1494: Document that appendChild removes first. ........ r59186 | guido.van.rossum | 2007-11-26 14:16:49 -0800 (Mon, 26 Nov 2007) | 2 lines A thread-less variant of brownian.py, submitted by Michele Simoniato. ........
2007-11-26 19:23:18 -04:00
Add a new child node to this node at the end of the list of
children, returning *newChild*. If the node was already in
the tree, it is removed first.
2007-08-15 11:28:22 -03:00
.. method:: Node.insertBefore(newChild, refChild)
Insert a new child node before an existing child. It must be the case that
*refChild* is a child of this node; if not, :exc:`ValueError` is raised.
*newChild* is returned. If *refChild* is ``None``, it inserts *newChild* at the
end of the children's list.
.. method:: Node.removeChild(oldChild)
Remove a child node. *oldChild* must be a child of this node; if not,
:exc:`ValueError` is raised. *oldChild* is returned on success. If *oldChild*
will not be used further, its :meth:`unlink` method should be called.
.. method:: Node.replaceChild(newChild, oldChild)
Replace an existing node with a new node. It must be the case that *oldChild*
is a child of this node; if not, :exc:`ValueError` is raised.
.. method:: Node.normalize()
Join adjacent text nodes so that all stretches of text are stored as single
:class:`Text` instances. This simplifies processing text from a DOM tree for
many applications.
.. method:: Node.cloneNode(deep)
Clone this node. Setting *deep* means to clone all child nodes as well. This
returns the clone.
.. _dom-nodelist-objects:
NodeList Objects
^^^^^^^^^^^^^^^^
A :class:`NodeList` represents a sequence of nodes. These objects are used in
two ways in the DOM Core recommendation: an :class:`Element` object provides
2007-08-15 11:28:22 -03:00
one as its list of child nodes, and the :meth:`getElementsByTagName` and
:meth:`getElementsByTagNameNS` methods of :class:`Node` return objects with this
interface to represent query results.
The DOM Level 2 recommendation defines one method and one attribute for these
objects:
.. method:: NodeList.item(i)
Return the *i*'th item from the sequence, if there is one, or ``None``. The
index *i* is not allowed to be less than zero or greater than or equal to the
2007-08-15 11:28:22 -03:00
length of the sequence.
.. attribute:: NodeList.length
The number of nodes in the sequence.
In addition, the Python DOM interface requires that some additional support is
provided to allow :class:`NodeList` objects to be used as Python sequences. All
:class:`NodeList` implementations must include support for
:meth:`~object.__len__` and
:meth:`~object.__getitem__`; this allows iteration over the :class:`NodeList` in
2007-08-15 11:28:22 -03:00
:keyword:`for` statements and proper support for the :func:`len` built-in
function.
If a DOM implementation supports modification of the document, the
:class:`NodeList` implementation must also support the
:meth:`~object.__setitem__` and :meth:`~object.__delitem__` methods.
2007-08-15 11:28:22 -03:00
.. _dom-documenttype-objects:
DocumentType Objects
^^^^^^^^^^^^^^^^^^^^
Information about the notations and entities declared by a document (including
the external subset if the parser uses it and can provide the information) is
available from a :class:`DocumentType` object. The :class:`DocumentType` for a
document is available from the :class:`Document` object's :attr:`doctype`
attribute; if there is no ``DOCTYPE`` declaration for the document, the
document's :attr:`doctype` attribute will be set to ``None`` instead of an
instance of this interface.
:class:`DocumentType` is a specialization of :class:`Node`, and adds the
following attributes:
.. attribute:: DocumentType.publicId
The public identifier for the external subset of the document type definition.
This will be a string or ``None``.
.. attribute:: DocumentType.systemId
The system identifier for the external subset of the document type definition.
This will be a URI as a string, or ``None``.
.. attribute:: DocumentType.internalSubset
A string giving the complete internal subset from the document. This does not
include the brackets which enclose the subset. If the document has no internal
subset, this should be ``None``.
.. attribute:: DocumentType.name
The name of the root element as given in the ``DOCTYPE`` declaration, if
present.
.. attribute:: DocumentType.entities
This is a :class:`NamedNodeMap` giving the definitions of external entities.
For entity names defined more than once, only the first definition is provided
(others are ignored as required by the XML recommendation). This may be
``None`` if the information is not provided by the parser, or if no entities are
defined.
.. attribute:: DocumentType.notations
This is a :class:`NamedNodeMap` giving the definitions of notations. For
notation names defined more than once, only the first definition is provided
(others are ignored as required by the XML recommendation). This may be
``None`` if the information is not provided by the parser, or if no notations
are defined.
.. _dom-document-objects:
Document Objects
^^^^^^^^^^^^^^^^
A :class:`Document` represents an entire XML document, including its constituent
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r61239 | andrew.kuchling | 2008-03-05 01:44:41 +0100 (Wed, 05 Mar 2008) | 1 line Add more items; add fragmentary notes ........ r61240 | amaury.forgeotdarc | 2008-03-05 02:50:33 +0100 (Wed, 05 Mar 2008) | 13 lines Issue#2238: some syntax errors from *args or **kwargs expressions would give bogus error messages, because of untested exceptions:: >>> f(**g(1=2)) XXX undetected error Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable instead of the expected SyntaxError: keyword can't be an expression Will backport. ........ r61241 | neal.norwitz | 2008-03-05 06:10:48 +0100 (Wed, 05 Mar 2008) | 3 lines Remove the files/dirs after closing the DB so the tests work on Windows. Patch from Trent Nelson. Also simplified removing a file by using test_support. ........ r61242 | neal.norwitz | 2008-03-05 06:14:18 +0100 (Wed, 05 Mar 2008) | 3 lines Get this test to pass even when there is no sound card in the system. Patch from Trent Nelson. (I can't test this.) ........ r61243 | neal.norwitz | 2008-03-05 06:20:44 +0100 (Wed, 05 Mar 2008) | 3 lines Catch OSError when trying to remove a file in case removal fails. This should prevent a failure in tearDown masking any real test failure. ........ r61244 | neal.norwitz | 2008-03-05 06:38:06 +0100 (Wed, 05 Mar 2008) | 5 lines Make the timeout longer to give slow machines a chance to pass the test before timing out. This doesn't change the duration of the test under normal circumstances. This is targetted at fixing the spurious failures on the FreeBSD buildbot primarily. ........ r61245 | neal.norwitz | 2008-03-05 06:49:03 +0100 (Wed, 05 Mar 2008) | 1 line Tabs -> spaces ........ r61246 | neal.norwitz | 2008-03-05 06:50:20 +0100 (Wed, 05 Mar 2008) | 1 line Use -u urlfetch to run more tests ........ r61247 | neal.norwitz | 2008-03-05 06:51:20 +0100 (Wed, 05 Mar 2008) | 1 line test_smtplib sometimes reports leaks too, suppress it ........ r61248 | jeffrey.yasskin | 2008-03-05 07:19:56 +0100 (Wed, 05 Mar 2008) | 5 lines Fix test_socketserver on Windows after r61099 added several signal.alarm() calls (which don't exist on non-Unix platforms). Thanks to Trent Nelson for the report and patch. ........ r61249 | georg.brandl | 2008-03-05 08:10:35 +0100 (Wed, 05 Mar 2008) | 2 lines Fix some rst. ........ r61252 | thomas.heller | 2008-03-05 15:53:39 +0100 (Wed, 05 Mar 2008) | 2 lines News entry for yesterdays commit. ........ r61253 | thomas.heller | 2008-03-05 16:34:29 +0100 (Wed, 05 Mar 2008) | 3 lines Issue 1872: Changed the struct module typecode from 't' to '?', for compatibility with PEP3118. ........ r61254 | skip.montanaro | 2008-03-05 17:41:09 +0100 (Wed, 05 Mar 2008) | 4 lines Elaborate on the role of the altinstall target when installing multiple versions. ........ r61255 | georg.brandl | 2008-03-05 20:31:44 +0100 (Wed, 05 Mar 2008) | 2 lines #2239: PYTHONPATH delimiter is os.pathsep. ........ r61256 | raymond.hettinger | 2008-03-05 21:59:58 +0100 (Wed, 05 Mar 2008) | 1 line C implementation of itertools.permutations(). ........ r61257 | raymond.hettinger | 2008-03-05 22:04:32 +0100 (Wed, 05 Mar 2008) | 1 line Small code cleanup. ........ r61260 | martin.v.loewis | 2008-03-05 23:24:31 +0100 (Wed, 05 Mar 2008) | 2 lines cd PCbuild only after deleting all pyc files. ........ r61261 | raymond.hettinger | 2008-03-06 02:15:52 +0100 (Thu, 06 Mar 2008) | 1 line Add examples. ........ r61262 | andrew.kuchling | 2008-03-06 02:36:27 +0100 (Thu, 06 Mar 2008) | 1 line Add two items ........ r61263 | georg.brandl | 2008-03-06 07:47:18 +0100 (Thu, 06 Mar 2008) | 2 lines #1725737: ignore other VC directories other than CVS and SVN's too. ........ r61264 | martin.v.loewis | 2008-03-06 07:55:22 +0100 (Thu, 06 Mar 2008) | 4 lines Patch #2232: os.tmpfile might fail on Windows if the user has no permission to create files in the root directory. Will backport to 2.5. ........ r61269 | georg.brandl | 2008-03-06 08:19:15 +0100 (Thu, 06 Mar 2008) | 2 lines Expand on re.split behavior with captured expressions. ........ r61270 | georg.brandl | 2008-03-06 08:22:09 +0100 (Thu, 06 Mar 2008) | 2 lines Little clarification of assignments. ........ r61271 | georg.brandl | 2008-03-06 08:31:34 +0100 (Thu, 06 Mar 2008) | 2 lines Add isinstance/issubclass to tutorial. ........ r61272 | georg.brandl | 2008-03-06 08:34:52 +0100 (Thu, 06 Mar 2008) | 2 lines Add missing NEWS entry for r61263. ........ r61273 | georg.brandl | 2008-03-06 08:41:16 +0100 (Thu, 06 Mar 2008) | 2 lines #2225: return nonzero status code from py_compile if not all files could be compiled. ........ r61274 | georg.brandl | 2008-03-06 08:43:02 +0100 (Thu, 06 Mar 2008) | 2 lines #2220: handle matching failure more gracefully. ........ r61275 | georg.brandl | 2008-03-06 08:45:52 +0100 (Thu, 06 Mar 2008) | 2 lines Bug #2220: handle rlcompleter attribute match failure more gracefully. ........ r61278 | martin.v.loewis | 2008-03-06 14:49:47 +0100 (Thu, 06 Mar 2008) | 1 line Rely on x64 platform configuration when building _bsddb on AMD64. ........ r61279 | martin.v.loewis | 2008-03-06 14:50:28 +0100 (Thu, 06 Mar 2008) | 1 line Update db-4.4.20 build procedure. ........ r61285 | raymond.hettinger | 2008-03-06 21:52:01 +0100 (Thu, 06 Mar 2008) | 1 line More tests. ........ r61286 | raymond.hettinger | 2008-03-06 23:51:36 +0100 (Thu, 06 Mar 2008) | 1 line Issue 2246: itertools grouper object did not participate in GC (should be backported). ........ r61288 | raymond.hettinger | 2008-03-07 02:33:20 +0100 (Fri, 07 Mar 2008) | 1 line Tweak recipes and tests ........ r61289 | jeffrey.yasskin | 2008-03-07 07:22:15 +0100 (Fri, 07 Mar 2008) | 5 lines Progress on issue #1193577 by adding a polling .shutdown() method to SocketServers. The core of the patch was written by Pedro Werneck, but any bugs are mine. I've also rearranged the code for timeouts in order to avoid interfering with the shutdown poll. ........ r61290 | nick.coghlan | 2008-03-07 15:13:28 +0100 (Fri, 07 Mar 2008) | 1 line Speed up with statements by storing the __exit__ method on the stack instead of in a temp variable (bumps the magic number for pyc files) ........ r61298 | andrew.kuchling | 2008-03-07 22:09:23 +0100 (Fri, 07 Mar 2008) | 1 line Grammar fix ........ r61303 | georg.brandl | 2008-03-08 10:54:06 +0100 (Sat, 08 Mar 2008) | 2 lines #2253: fix continue vs. finally docs. ........ r61304 | marc-andre.lemburg | 2008-03-08 11:01:43 +0100 (Sat, 08 Mar 2008) | 3 lines Add new name for Mandrake: Mandriva. ........ r61305 | georg.brandl | 2008-03-08 11:05:24 +0100 (Sat, 08 Mar 2008) | 2 lines #1533486: fix types in refcount intro. ........ r61312 | facundo.batista | 2008-03-08 17:50:27 +0100 (Sat, 08 Mar 2008) | 5 lines Issue 1106316. post_mortem()'s parameter, traceback, is now optional: it defaults to the traceback of the exception that is currently being handled. ........ r61313 | jeffrey.yasskin | 2008-03-08 19:26:54 +0100 (Sat, 08 Mar 2008) | 2 lines Add tests for with and finally performance to pybench. ........ r61314 | jeffrey.yasskin | 2008-03-08 21:08:21 +0100 (Sat, 08 Mar 2008) | 2 lines Fix pybench for pythons < 2.6, tested back to 2.3. ........ r61317 | jeffrey.yasskin | 2008-03-08 22:35:15 +0100 (Sat, 08 Mar 2008) | 3 lines Well that was dumb. platform.python_implementation returns a function, not a string. ........ r61329 | georg.brandl | 2008-03-09 16:11:39 +0100 (Sun, 09 Mar 2008) | 2 lines #2249: document assertTrue and assertFalse. ........ r61332 | neal.norwitz | 2008-03-09 20:03:42 +0100 (Sun, 09 Mar 2008) | 4 lines Introduce a lock to fix a race condition which caused an exception in the test. Some buildbots were consistently failing (e.g., amd64). Also remove a couple of semi-colons. ........ r61344 | raymond.hettinger | 2008-03-11 01:19:07 +0100 (Tue, 11 Mar 2008) | 1 line Add recipe to docs. ........ r61350 | guido.van.rossum | 2008-03-11 22:18:06 +0100 (Tue, 11 Mar 2008) | 3 lines Fix the overflows in expandtabs(). "This time for sure!" (Exploit at request.) ........ r61351 | raymond.hettinger | 2008-03-11 22:37:46 +0100 (Tue, 11 Mar 2008) | 1 line Improve docs for itemgetter(). Show that it works with slices. ........ r61363 | georg.brandl | 2008-03-13 08:15:56 +0100 (Thu, 13 Mar 2008) | 2 lines #2265: fix example. ........ r61364 | georg.brandl | 2008-03-13 08:17:14 +0100 (Thu, 13 Mar 2008) | 2 lines #2270: fix typo. ........ r61365 | georg.brandl | 2008-03-13 08:21:41 +0100 (Thu, 13 Mar 2008) | 2 lines #1720705: add docs about import/threading interaction, wording by Nick. ........ r61366 | andrew.kuchling | 2008-03-13 12:07:35 +0100 (Thu, 13 Mar 2008) | 1 line Add class decorators ........ r61367 | raymond.hettinger | 2008-03-13 17:43:17 +0100 (Thu, 13 Mar 2008) | 1 line Add 2-to-3 support for the itertools moved to builtins or renamed. ........ r61368 | raymond.hettinger | 2008-03-13 17:43:59 +0100 (Thu, 13 Mar 2008) | 1 line Consistent tense. ........ r61369 | raymond.hettinger | 2008-03-13 20:03:51 +0100 (Thu, 13 Mar 2008) | 1 line Issue 2274: Add heapq.heappushpop(). ........ r61370 | raymond.hettinger | 2008-03-13 20:33:34 +0100 (Thu, 13 Mar 2008) | 1 line Simplify the nlargest() code using heappushpop(). ........ r61371 | brett.cannon | 2008-03-13 21:27:00 +0100 (Thu, 13 Mar 2008) | 4 lines Move test_thread over to unittest. Commits GHOP 237. Thanks Benjamin Peterson for the patch. ........ r61372 | brett.cannon | 2008-03-13 21:33:10 +0100 (Thu, 13 Mar 2008) | 4 lines Move test_tokenize to doctest. Done as GHOP 238 by Josip Dzolonga. ........ r61373 | brett.cannon | 2008-03-13 21:47:41 +0100 (Thu, 13 Mar 2008) | 4 lines Convert test_contains, test_crypt, and test_select to unittest. Patch from GHOP 294 by David Marek. ........ r61374 | brett.cannon | 2008-03-13 22:02:16 +0100 (Thu, 13 Mar 2008) | 4 lines Move test_gdbm to use unittest. Closes issue #1960. Thanks Giampaolo Rodola. ........ r61375 | brett.cannon | 2008-03-13 22:09:28 +0100 (Thu, 13 Mar 2008) | 4 lines Convert test_fcntl to unittest. Closes issue #2055. Thanks Giampaolo Rodola. ........ r61376 | raymond.hettinger | 2008-03-14 06:03:44 +0100 (Fri, 14 Mar 2008) | 1 line Leave heapreplace() unchanged. ........ r61378 | martin.v.loewis | 2008-03-14 14:56:09 +0100 (Fri, 14 Mar 2008) | 2 lines Patch #2284: add -x64 option to rt.bat. ........ r61379 | martin.v.loewis | 2008-03-14 14:57:59 +0100 (Fri, 14 Mar 2008) | 2 lines Use -x64 flag. ........ r61382 | brett.cannon | 2008-03-14 15:03:10 +0100 (Fri, 14 Mar 2008) | 2 lines Remove a bad test. ........ r61383 | mark.dickinson | 2008-03-14 15:23:37 +0100 (Fri, 14 Mar 2008) | 9 lines Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently across platforms: it should now raise OverflowError on all platforms. (Previously it raised OverflowError only on non IEEE 754 platforms.) Also fix the (already existing) test for this behaviour so that it actually raises TestFailed instead of just referencing it. ........ r61387 | thomas.heller | 2008-03-14 22:06:21 +0100 (Fri, 14 Mar 2008) | 1 line Remove unneeded initializer. ........ r61388 | martin.v.loewis | 2008-03-14 22:19:28 +0100 (Fri, 14 Mar 2008) | 2 lines Run debug version, cd to PCbuild. ........ r61392 | georg.brandl | 2008-03-15 00:10:34 +0100 (Sat, 15 Mar 2008) | 2 lines Remove obsolete paragraph. #2288. ........ r61395 | georg.brandl | 2008-03-15 01:20:19 +0100 (Sat, 15 Mar 2008) | 2 lines Fix lots of broken links in the docs, found by Sphinx' external link checker. ........ r61396 | skip.montanaro | 2008-03-15 03:32:49 +0100 (Sat, 15 Mar 2008) | 1 line note that fork and forkpty raise OSError on failure ........ r61402 | skip.montanaro | 2008-03-15 17:04:45 +0100 (Sat, 15 Mar 2008) | 1 line add %f format to datetime - issue 1158 ........ r61403 | skip.montanaro | 2008-03-15 17:07:11 +0100 (Sat, 15 Mar 2008) | 2 lines . ........
2008-03-15 21:07:10 -03:00
elements, attributes, processing instructions, comments etc. Remember that it
2007-08-15 11:28:22 -03:00
inherits properties from :class:`Node`.
.. attribute:: Document.documentElement
The one and only root element of the document.
.. method:: Document.createElement(tagName)
Create and return a new element node. The element is not inserted into the
document when it is created. You need to explicitly insert it with one of the
other methods such as :meth:`insertBefore` or :meth:`appendChild`.
.. method:: Document.createElementNS(namespaceURI, tagName)
Create and return a new element with a namespace. The *tagName* may have a
prefix. The element is not inserted into the document when it is created. You
need to explicitly insert it with one of the other methods such as
:meth:`insertBefore` or :meth:`appendChild`.
.. method:: Document.createTextNode(data)
Create and return a text node containing the data passed as a parameter. As
with the other creation methods, this one does not insert the node into the
tree.
.. method:: Document.createComment(data)
Create and return a comment node containing the data passed as a parameter. As
with the other creation methods, this one does not insert the node into the
tree.
.. method:: Document.createProcessingInstruction(target, data)
Create and return a processing instruction node containing the *target* and
*data* passed as parameters. As with the other creation methods, this one does
not insert the node into the tree.
.. method:: Document.createAttribute(name)
Create and return an attribute node. This method does not associate the
attribute node with any particular element. You must use
:meth:`setAttributeNode` on the appropriate :class:`Element` object to use the
newly created attribute instance.
.. method:: Document.createAttributeNS(namespaceURI, qualifiedName)
Create and return an attribute node with a namespace. The *tagName* may have a
prefix. This method does not associate the attribute node with any particular
element. You must use :meth:`setAttributeNode` on the appropriate
:class:`Element` object to use the newly created attribute instance.
.. method:: Document.getElementsByTagName(tagName)
Search for all descendants (direct children, children's children, etc.) with a
particular element type name.
.. method:: Document.getElementsByTagNameNS(namespaceURI, localName)
Search for all descendants (direct children, children's children, etc.) with a
particular namespace URI and localname. The localname is the part of the
namespace after the prefix.
.. _dom-element-objects:
Element Objects
^^^^^^^^^^^^^^^
:class:`Element` is a subclass of :class:`Node`, so inherits all the attributes
of that class.
.. attribute:: Element.tagName
The element type name. In a namespace-using document it may have colons in it.
The value is a string.
.. method:: Element.getElementsByTagName(tagName)
Same as equivalent method in the :class:`Document` class.
Merged revisions 70342,70385-70387,70389-70390,70392-70393,70395,70400,70405-70406,70418,70438,70464,70468 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r70342 | georg.brandl | 2009-03-13 14:03:58 -0500 (Fri, 13 Mar 2009) | 1 line #5486: typos. ........ r70385 | benjamin.peterson | 2009-03-15 09:38:55 -0500 (Sun, 15 Mar 2009) | 1 line fix tuple.index() error message #5495 ........ r70386 | georg.brandl | 2009-03-15 16:32:06 -0500 (Sun, 15 Mar 2009) | 1 line #5496: fix docstring of lookup(). ........ r70387 | georg.brandl | 2009-03-15 16:37:16 -0500 (Sun, 15 Mar 2009) | 1 line #5493: clarify __nonzero__ docs. ........ r70389 | georg.brandl | 2009-03-15 16:43:38 -0500 (Sun, 15 Mar 2009) | 1 line Fix a small nit in the error message if bool() falls back on __len__ and it returns the wrong type: it would tell the user that __nonzero__ should return bool or int. ........ r70390 | georg.brandl | 2009-03-15 16:44:43 -0500 (Sun, 15 Mar 2009) | 1 line #5491: clarify nested() semantics. ........ r70392 | georg.brandl | 2009-03-15 16:46:00 -0500 (Sun, 15 Mar 2009) | 1 line #5488: add missing struct member. ........ r70393 | georg.brandl | 2009-03-15 16:47:42 -0500 (Sun, 15 Mar 2009) | 1 line #5478: fix copy-paste oversight in function signature. ........ r70395 | georg.brandl | 2009-03-15 16:51:48 -0500 (Sun, 15 Mar 2009) | 1 line #5276: document IDLESTARTUP and .Idle.py. ........ r70400 | georg.brandl | 2009-03-15 16:59:37 -0500 (Sun, 15 Mar 2009) | 3 lines Fix markup in re docs and give a mail address in regex howto, so that the recommendation to send suggestions to the author can be followed. ........ r70405 | georg.brandl | 2009-03-15 17:11:07 -0500 (Sun, 15 Mar 2009) | 7 lines Move the previously local import of threading to module level. This is cleaner and avoids lockups in obscure cases where a Queue is instantiated while the import lock is already held by another thread. OKed by Tim Peters. ........ r70406 | hirokazu.yamamoto | 2009-03-15 17:43:14 -0500 (Sun, 15 Mar 2009) | 1 line Added skip for old MSVC. ........ r70418 | georg.brandl | 2009-03-16 14:42:03 -0500 (Mon, 16 Mar 2009) | 1 line Add token markup. ........ r70438 | benjamin.peterson | 2009-03-17 15:29:51 -0500 (Tue, 17 Mar 2009) | 1 line I thought this was begging for an example ........ r70464 | benjamin.peterson | 2009-03-18 15:58:09 -0500 (Wed, 18 Mar 2009) | 1 line a much better example ........ r70468 | benjamin.peterson | 2009-03-18 22:04:31 -0500 (Wed, 18 Mar 2009) | 1 line close files after comparing them ........
2009-03-21 14:31:58 -03:00
.. method:: Element.getElementsByTagNameNS(namespaceURI, localName)
2007-08-15 11:28:22 -03:00
Same as equivalent method in the :class:`Document` class.
.. method:: Element.hasAttribute(name)
Return ``True`` if the element has an attribute named by *name*.
2007-08-15 11:28:22 -03:00
.. method:: Element.hasAttributeNS(namespaceURI, localName)
Return ``True`` if the element has an attribute named by *namespaceURI* and
2007-08-15 11:28:22 -03:00
*localName*.
.. method:: Element.getAttribute(name)
Return the value of the attribute named by *name* as a string. If no such
attribute exists, an empty string is returned, as if the attribute had no value.
.. method:: Element.getAttributeNode(attrname)
Return the :class:`Attr` node for the attribute named by *attrname*.
.. method:: Element.getAttributeNS(namespaceURI, localName)
Return the value of the attribute named by *namespaceURI* and *localName* as a
string. If no such attribute exists, an empty string is returned, as if the
attribute had no value.
.. method:: Element.getAttributeNodeNS(namespaceURI, localName)
Return an attribute value as a node, given a *namespaceURI* and *localName*.
.. method:: Element.removeAttribute(name)
Merged revisions 61003-61033 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r61004 | georg.brandl | 2008-02-23 19:47:04 +0100 (Sat, 23 Feb 2008) | 2 lines Documentation coverage builder, part 1. ........ r61006 | andrew.kuchling | 2008-02-23 20:02:33 +0100 (Sat, 23 Feb 2008) | 1 line #1389051: IMAP module tries to read entire message in one chunk. Patch by Fredrik Lundh. ........ r61008 | andrew.kuchling | 2008-02-23 20:28:58 +0100 (Sat, 23 Feb 2008) | 1 line #1389051, #1092502: fix excessively large allocations when using read() on a socket ........ r61011 | jeffrey.yasskin | 2008-02-23 20:40:54 +0100 (Sat, 23 Feb 2008) | 13 lines Prevent classes like: class RunSelfFunction(object): def __init__(self): self.thread = threading.Thread(target=self._run) self.thread.start() def _run(self): pass from creating a permanent cycle between the object and the thread by having the Thread delete its references to the object when it completes. As an example of the effect of this bug, paramiko.Transport inherits from Thread to avoid it. ........ r61013 | jeffrey.yasskin | 2008-02-23 21:40:35 +0100 (Sat, 23 Feb 2008) | 3 lines Followup to r61011: Also avoid the reference cycle when the Thread's target raises an exception. ........ r61017 | georg.brandl | 2008-02-23 22:59:11 +0100 (Sat, 23 Feb 2008) | 2 lines #2101: fix removeAttribute docs. ........ r61018 | georg.brandl | 2008-02-23 23:05:38 +0100 (Sat, 23 Feb 2008) | 2 lines Add examples to modulefinder docs. Written for GHOP by Josip Dzolonga. ........ r61019 | georg.brandl | 2008-02-23 23:09:24 +0100 (Sat, 23 Feb 2008) | 2 lines Use os.closerange() in popen2. ........ r61020 | georg.brandl | 2008-02-23 23:14:02 +0100 (Sat, 23 Feb 2008) | 2 lines Use os.closerange(). ........ r61021 | georg.brandl | 2008-02-23 23:35:33 +0100 (Sat, 23 Feb 2008) | 3 lines In test_heapq and test_bisect, test both the Python and the C implementation. Originally written for GHOP by Josip Dzolonga, heavily patched by me. ........ r61024 | facundo.batista | 2008-02-23 23:54:12 +0100 (Sat, 23 Feb 2008) | 3 lines Added simple test case. Thanks Benjamin Peterson. ........ r61025 | georg.brandl | 2008-02-23 23:55:18 +0100 (Sat, 23 Feb 2008) | 2 lines #1825: correctly document msilib.add_data. ........ r61027 | georg.brandl | 2008-02-24 00:02:23 +0100 (Sun, 24 Feb 2008) | 2 lines #1826: allow dotted attribute paths in operator.attrgetter. ........ r61028 | georg.brandl | 2008-02-24 00:04:35 +0100 (Sun, 24 Feb 2008) | 2 lines #1506171: added operator.methodcaller(). ........ r61029 | georg.brandl | 2008-02-24 00:25:26 +0100 (Sun, 24 Feb 2008) | 2 lines Document import ./. threading issues. #1720705. ........ r61032 | georg.brandl | 2008-02-24 00:43:01 +0100 (Sun, 24 Feb 2008) | 2 lines Specify what kind of warning -3 emits. ........ r61033 | christian.heimes | 2008-02-24 00:59:45 +0100 (Sun, 24 Feb 2008) | 1 line MS Windows doesn't have mode_t but stat.st_mode is defined as unsigned short. ........
2008-02-23 20:38:49 -04:00
Remove an attribute by name. If there is no matching attribute, a
:exc:`NotFoundErr` is raised.
2007-08-15 11:28:22 -03:00
.. method:: Element.removeAttributeNode(oldAttr)
Remove and return *oldAttr* from the attribute list, if present. If *oldAttr* is
not present, :exc:`NotFoundErr` is raised.
.. method:: Element.removeAttributeNS(namespaceURI, localName)
Remove an attribute by name. Note that it uses a localName, not a qname. No
exception is raised if there is no matching attribute.
.. method:: Element.setAttribute(name, value)
Set an attribute value from a string.
.. method:: Element.setAttributeNode(newAttr)
Add a new attribute node to the element, replacing an existing attribute if
necessary if the :attr:`name` attribute matches. If a replacement occurs, the
old attribute node will be returned. If *newAttr* is already in use,
:exc:`InuseAttributeErr` will be raised.
.. method:: Element.setAttributeNodeNS(newAttr)
Add a new attribute node to the element, replacing an existing attribute if
necessary if the :attr:`namespaceURI` and :attr:`localName` attributes match.
If a replacement occurs, the old attribute node will be returned. If *newAttr*
is already in use, :exc:`InuseAttributeErr` will be raised.
.. method:: Element.setAttributeNS(namespaceURI, qname, value)
Set an attribute value from a string, given a *namespaceURI* and a *qname*.
Note that a qname is the whole attribute name. This is different than above.
.. _dom-attr-objects:
Attr Objects
^^^^^^^^^^^^
:class:`Attr` inherits from :class:`Node`, so inherits all its attributes.
.. attribute:: Attr.name
The attribute name.
In a namespace-using document it may include a colon.
2007-08-15 11:28:22 -03:00
.. attribute:: Attr.localName
The part of the name following the colon if there is one, else the
entire name.
2007-08-15 11:28:22 -03:00
This is a read-only attribute.
.. attribute:: Attr.prefix
The part of the name preceding the colon if there is one, else the
empty string.
.. attribute:: Attr.value
The text value of the attribute. This is a synonym for the
:attr:`nodeValue` attribute.
2007-08-15 11:28:22 -03:00
.. _dom-attributelist-objects:
NamedNodeMap Objects
^^^^^^^^^^^^^^^^^^^^
:class:`NamedNodeMap` does *not* inherit from :class:`Node`.
.. attribute:: NamedNodeMap.length
The length of the attribute list.
.. method:: NamedNodeMap.item(index)
Return an attribute with a particular index. The order you get the attributes
in is arbitrary but will be consistent for the life of a DOM. Each item is an
attribute node. Get its value with the :attr:`value` attribute.
There are also experimental methods that give this class more mapping behavior.
You can use them or you can use the standardized :meth:`!getAttribute\*` family
2007-08-15 11:28:22 -03:00
of methods on the :class:`Element` objects.
.. _dom-comment-objects:
Comment Objects
^^^^^^^^^^^^^^^
:class:`Comment` represents a comment in the XML document. It is a subclass of
:class:`Node`, but cannot have child nodes.
.. attribute:: Comment.data
The content of the comment as a string. The attribute contains all characters
between the leading ``<!-``\ ``-`` and trailing ``-``\ ``->``, but does not
include them.
.. _dom-text-objects:
Text and CDATASection Objects
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The :class:`Text` interface represents text in the XML document. If the parser
and DOM implementation support the DOM's XML extension, portions of the text
enclosed in CDATA marked sections are stored in :class:`CDATASection` objects.
These two interfaces are identical, but provide different values for the
:attr:`nodeType` attribute.
These interfaces extend the :class:`Node` interface. They cannot have child
nodes.
.. attribute:: Text.data
The content of the text node as a string.
.. note::
The use of a :class:`CDATASection` node does not indicate that the node
represents a complete CDATA marked section, only that the content of the node
was part of a CDATA section. A single CDATA section may be represented by more
than one node in the document tree. There is no way to determine whether two
adjacent :class:`CDATASection` nodes represent different CDATA marked sections.
.. _dom-pi-objects:
ProcessingInstruction Objects
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Represents a processing instruction in the XML document; this inherits from the
:class:`Node` interface and cannot have child nodes.
.. attribute:: ProcessingInstruction.target
The content of the processing instruction up to the first whitespace character.
This is a read-only attribute.
.. attribute:: ProcessingInstruction.data
The content of the processing instruction following the first whitespace
character.
.. _dom-exceptions:
Exceptions
^^^^^^^^^^
The DOM Level 2 recommendation defines a single exception, :exc:`DOMException`,
and a number of constants that allow applications to determine what sort of
error occurred. :exc:`DOMException` instances carry a :attr:`code` attribute
that provides the appropriate value for the specific exception.
The Python DOM interface provides the constants, but also expands the set of
exceptions so that a specific exception exists for each of the exception codes
defined by the DOM. The implementations must raise the appropriate specific
exception, each of which carries the appropriate value for the :attr:`code`
attribute.
.. exception:: DOMException
Base exception class used for all specific DOM exceptions. This exception class
cannot be directly instantiated.
.. exception:: DomstringSizeErr
Raised when a specified range of text does not fit into a string. This is not
known to be used in the Python DOM implementations, but may be received from DOM
implementations not written in Python.
.. exception:: HierarchyRequestErr
Raised when an attempt is made to insert a node where the node type is not
allowed.
.. exception:: IndexSizeErr
Raised when an index or size parameter to a method is negative or exceeds the
allowed values.
.. exception:: InuseAttributeErr
Raised when an attempt is made to insert an :class:`Attr` node that is already
present elsewhere in the document.
.. exception:: InvalidAccessErr
Raised if a parameter or an operation is not supported on the underlying object.
.. exception:: InvalidCharacterErr
This exception is raised when a string parameter contains a character that is
not permitted in the context it's being used in by the XML 1.0 recommendation.
For example, attempting to create an :class:`Element` node with a space in the
element type name will cause this error to be raised.
.. exception:: InvalidModificationErr
Raised when an attempt is made to modify the type of a node.
.. exception:: InvalidStateErr
Raised when an attempt is made to use an object that is not defined or is no
longer usable.
.. exception:: NamespaceErr
If an attempt is made to change any object in a way that is not permitted with
regard to the `Namespaces in XML <https://www.w3.org/TR/REC-xml-names/>`_
2007-08-15 11:28:22 -03:00
recommendation, this exception is raised.
.. exception:: NotFoundErr
Exception when a node does not exist in the referenced context. For example,
:meth:`NamedNodeMap.removeNamedItem` will raise this if the node passed in does
not exist in the map.
.. exception:: NotSupportedErr
Raised when the implementation does not support the requested type of object or
operation.
.. exception:: NoDataAllowedErr
This is raised if data is specified for a node which does not support data.
.. XXX a better explanation is needed!
2007-08-15 11:28:22 -03:00
.. exception:: NoModificationAllowedErr
Raised on attempts to modify an object where modifications are not allowed (such
as for read-only nodes).
.. exception:: SyntaxErr
Raised when an invalid or illegal string is specified.
.. XXX how is this different from InvalidCharacterErr?
2007-08-15 11:28:22 -03:00
.. exception:: WrongDocumentErr
Raised when a node is inserted in a different document than it currently belongs
to, and the implementation does not support migrating the node from one document
to the other.
The exception codes defined in the DOM recommendation map to the exceptions
described above according to this table:
+--------------------------------------+---------------------------------+
| Constant | Exception |
+======================================+=================================+
| :const:`DOMSTRING_SIZE_ERR` | :exc:`DomstringSizeErr` |
+--------------------------------------+---------------------------------+
| :const:`HIERARCHY_REQUEST_ERR` | :exc:`HierarchyRequestErr` |
+--------------------------------------+---------------------------------+
| :const:`INDEX_SIZE_ERR` | :exc:`IndexSizeErr` |
+--------------------------------------+---------------------------------+
| :const:`INUSE_ATTRIBUTE_ERR` | :exc:`InuseAttributeErr` |
+--------------------------------------+---------------------------------+
| :const:`INVALID_ACCESS_ERR` | :exc:`InvalidAccessErr` |
+--------------------------------------+---------------------------------+
| :const:`INVALID_CHARACTER_ERR` | :exc:`InvalidCharacterErr` |
+--------------------------------------+---------------------------------+
| :const:`INVALID_MODIFICATION_ERR` | :exc:`InvalidModificationErr` |
+--------------------------------------+---------------------------------+
| :const:`INVALID_STATE_ERR` | :exc:`InvalidStateErr` |
+--------------------------------------+---------------------------------+
| :const:`NAMESPACE_ERR` | :exc:`NamespaceErr` |
+--------------------------------------+---------------------------------+
| :const:`NOT_FOUND_ERR` | :exc:`NotFoundErr` |
+--------------------------------------+---------------------------------+
| :const:`NOT_SUPPORTED_ERR` | :exc:`NotSupportedErr` |
+--------------------------------------+---------------------------------+
| :const:`NO_DATA_ALLOWED_ERR` | :exc:`NoDataAllowedErr` |
+--------------------------------------+---------------------------------+
| :const:`NO_MODIFICATION_ALLOWED_ERR` | :exc:`NoModificationAllowedErr` |
+--------------------------------------+---------------------------------+
| :const:`SYNTAX_ERR` | :exc:`SyntaxErr` |
+--------------------------------------+---------------------------------+
| :const:`WRONG_DOCUMENT_ERR` | :exc:`WrongDocumentErr` |
+--------------------------------------+---------------------------------+
.. _dom-conformance:
Conformance
-----------
This section describes the conformance requirements and relationships between
the Python DOM API, the W3C DOM recommendations, and the OMG IDL mapping for
Python.
.. _dom-type-mapping:
Type Mapping
^^^^^^^^^^^^
The IDL types used in the DOM specification are mapped to Python types
2007-08-15 11:28:22 -03:00
according to the following table.
+------------------+-------------------------------------------+
| IDL Type | Python Type |
+==================+===========================================+
| ``boolean`` | ``bool`` or ``int`` |
2007-08-15 11:28:22 -03:00
+------------------+-------------------------------------------+
| ``int`` | ``int`` |
2007-08-15 11:28:22 -03:00
+------------------+-------------------------------------------+
| ``long int`` | ``int`` |
2007-08-15 11:28:22 -03:00
+------------------+-------------------------------------------+
| ``unsigned int`` | ``int`` |
+------------------+-------------------------------------------+
| ``DOMString`` | ``str`` or ``bytes`` |
+------------------+-------------------------------------------+
| ``null`` | ``None`` |
2007-08-15 11:28:22 -03:00
+------------------+-------------------------------------------+
.. _dom-accessor-methods:
Accessor Methods
^^^^^^^^^^^^^^^^
The mapping from OMG IDL to Python defines accessor functions for IDL
``attribute`` declarations in much the way the Java mapping does.
2007-08-15 11:28:22 -03:00
Mapping the IDL declarations ::
readonly attribute string someValue;
attribute string anotherValue;
yields three accessor functions: a "get" method for :attr:`someValue`
(:meth:`_get_someValue`), and "get" and "set" methods for :attr:`anotherValue`
(:meth:`_get_anotherValue` and :meth:`_set_anotherValue`). The mapping, in
particular, does not require that the IDL attributes are accessible as normal
Python attributes: ``object.someValue`` is *not* required to work, and may
raise an :exc:`AttributeError`.
The Python DOM API, however, *does* require that normal attribute access work.
This means that the typical surrogates generated by Python IDL compilers are not
likely to work, and wrapper objects may be needed on the client if the DOM
objects are accessed via CORBA. While this does require some additional
consideration for CORBA DOM clients, the implementers with experience using DOM
over CORBA from Python do not consider this a problem. Attributes that are
declared ``readonly`` may not restrict write access in all DOM
2007-08-15 11:28:22 -03:00
implementations.
In the Python DOM API, accessor functions are not required. If provided, they
should take the form defined by the Python IDL mapping, but these methods are
considered unnecessary since the attributes are accessible directly from Python.
"Set" accessors should never be provided for ``readonly`` attributes.
2007-08-15 11:28:22 -03:00
The IDL definitions do not fully embody the requirements of the W3C DOM API,
such as the notion of certain objects, such as the return value of
:meth:`getElementsByTagName`, being "live". The Python DOM API does not require
implementations to enforce such requirements.