mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 06:55:36 +08:00
3 lines
92 KiB
HTML
3 lines
92 KiB
HTML
<div class="body" role="main"><div class="section" id="module-xml.dom"><h1><span class="yiyi-st" id="yiyi-10">20.6. <a class="reference internal" href="#module-xml.dom" title="xml.dom: Document Object Model API for Python."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom</span></code></a> — The Document Object Model API</span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/xml/dom/__init__.py">Lib/xml/dom/__init__.py</a></span></p><p><span class="yiyi-st" id="yiyi-12">The Document Object Model, or “DOM,” is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents. </span><span class="yiyi-st" id="yiyi-13">A DOM implementation presents an XML document as a tree structure, or allows client code to build such a structure from scratch. </span><span class="yiyi-st" id="yiyi-14">It then gives access to the structure through a set of objects which provided well-known interfaces.</span></p><p><span class="yiyi-st" id="yiyi-15">The DOM is extremely useful for random-access applications. </span><span class="yiyi-st" id="yiyi-16">SAX only allows you a view of one bit of the document at a time. </span><span class="yiyi-st" id="yiyi-17">If you are looking at one SAX element, you have no access to another. </span><span class="yiyi-st" id="yiyi-18">If you are looking at a text node, you have no access to a containing element. </span><span class="yiyi-st" id="yiyi-19">When you write a SAX application, you need to keep track of your program’s position in the document somewhere in your own code. </span><span class="yiyi-st" id="yiyi-20">SAX does not do it for you. </span><span class="yiyi-st" id="yiyi-21">Also, if you need to look ahead in the XML document, you are just out of luck.</span></p><p><span class="yiyi-st" id="yiyi-22">Some applications are simply impossible in an event driven model with no access to a tree. </span><span class="yiyi-st" id="yiyi-23">Of course you could build some sort of tree yourself in SAX events, but the DOM allows you to avoid writing that code. </span><span class="yiyi-st" id="yiyi-24">The DOM is a standard tree representation for XML data.</span></p><p><span class="yiyi-st" id="yiyi-25">The Document Object Model is being defined by the W3C in stages, or “levels” in their terminology. </span><span class="yiyi-st" id="yiyi-26">The Python mapping of the API is substantially based on the DOM Level 2 recommendation.</span></p><p><span class="yiyi-st" id="yiyi-27">DOM applications typically start by parsing some XML into a DOM. </span><span class="yiyi-st" id="yiyi-28">How this is accomplished is not covered at all by DOM Level 1, and Level 2 provides only limited improvements: There is a <code class="xref py py-class docutils literal"><span class="pre">DOMImplementation</span></code> object class which provides access to <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> creation methods, but no way to access an XML reader/parser/Document builder in an implementation-independent way. </span><span class="yiyi-st" id="yiyi-29">There is also no well-defined way to access these methods without an existing <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> object. </span><span class="yiyi-st" id="yiyi-30">In Python, each DOM implementation will provide a function <a class="reference internal" href="#xml.dom.getDOMImplementation" title="xml.dom.getDOMImplementation"><code class="xref py py-func docutils literal"><span class="pre">getDOMImplementation()</span></code></a>. </span><span class="yiyi-st" id="yiyi-31">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.</span></p><p><span class="yiyi-st" id="yiyi-32">Once you have a DOM document object, you can access the parts of your XML document through its properties and methods. </span><span class="yiyi-st" id="yiyi-33">These properties are defined in the DOM specification; this portion of the reference manual describes the interpretation of the specification in Python.</span></p><p><span class="yiyi-st" id="yiyi-34">The specification provided by the W3C defines the DOM API for Java, ECMAScript, and OMG IDL. </span><span class="yiyi-st" id="yiyi-35">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). </span><span class="yiyi-st" id="yiyi-36">See section <a class="reference internal" href="#dom-conformance"><span>Conformance</span></a> for a detailed discussion of mapping requirements.</span></p><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-37">See also</span></p><dl class="last docutils"><dt><span class="yiyi-st" id="yiyi-38"><a class="reference external" href="https://www.w3.org/TR/DOM-Level-2-Core/">Document Object Model (DOM) Level 2 Specification</a></span></dt><dd><span class="yiyi-st" id="yiyi-39">The W3C recommendation upon which the Python DOM API is based.</span></dd><dt><span class="yiyi-st" id="yiyi-40"><a class="reference external" href="https://www.w3.org/TR/REC-DOM-Level-1/">Document Object Model (DOM) Level 1 Specification</a></span></dt><dd><span class="yiyi-st" id="yiyi-41">The W3C recommendation for the DOM supported by <a class="reference internal" href="xml.dom.minidom.html#module-xml.dom.minidom" title="xml.dom.minidom: Minimal Document Object Model (DOM) implementation."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom.minidom</span></code></a>.</span></dd><dt><span class="yiyi-st" id="yiyi-42"><a class="reference external" href="http://www.omg.org/spec/PYTH/1.2/PDF">Python Language Mapping Specification</a></span></dt><dd><span class="yiyi-st" id="yiyi-43">This specifies the mapping from OMG IDL to Python.</span></dd></dl></div><div class="section" id="module-contents"><h2><span class="yiyi-st" id="yiyi-44">20.6.1. </span><span class="yiyi-st" id="yiyi-45">Module Contents</span></h2><p><span class="yiyi-st" id="yiyi-46">The <a class="reference internal" href="#module-xml.dom" title="xml.dom: Document Object Model API for Python."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom</span></code></a> contains the following functions:</span></p><dl class="function"><dt id="xml.dom.registerDOMImplementation"><span class="yiyi-st" id="yiyi-47"> <code class="descclassname">xml.dom.</code><code class="descname">registerDOMImplementation</code><span class="sig-paren">(</span><em>name</em>, <em>factory</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-48">Register the <em>factory</em> function with the name <em>name</em>. </span><span class="yiyi-st" id="yiyi-49">The factory function should return an object which implements the <code class="xref py py-class docutils literal"><span class="pre">DOMImplementation</span></code> interface. </span><span class="yiyi-st" id="yiyi-50">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. </span><span class="yiyi-st" id="yiyi-51">if that implementation supports some customization).</span></p></dd></dl><dl class="function"><dt id="xml.dom.getDOMImplementation"><span class="yiyi-st" id="yiyi-52"> <code class="descclassname">xml.dom.</code><code class="descname">getDOMImplementation</code><span class="sig-paren">(</span><em>name=None</em>, <em>features=()</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-53">Return a suitable DOM implementation. </span><span class="yiyi-st" id="yiyi-54">The <em>name</em> is either well-known, the module name of a DOM implementation, or <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-55">If it is not <code class="docutils literal"><span class="pre">None</span></code>, imports the corresponding module and returns a <code class="xref py py-class docutils literal"><span class="pre">DOMImplementation</span></code> object if the import succeeds. </span><span class="yiyi-st" id="yiyi-56">If no name is given, and if the environment variable <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal"><span class="pre">PYTHON_DOM</span></code> is set, this variable is used to find the implementation.</span></p><p><span class="yiyi-st" id="yiyi-57">If name is not given, this examines the available implementations to find one with the required feature set. </span><span class="yiyi-st" id="yiyi-58">If no implementation can be found, raise an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a>. </span><span class="yiyi-st" id="yiyi-59">The features list must be a sequence of <code class="docutils literal"><span class="pre">(feature,</span> <span class="pre">version)</span></code> pairs which are passed to the <code class="xref py py-meth docutils literal"><span class="pre">hasFeature()</span></code> method on available <code class="xref py py-class docutils literal"><span class="pre">DOMImplementation</span></code> objects.</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-60">Some convenience constants are also provided:</span></p><dl class="data"><dt id="xml.dom.EMPTY_NAMESPACE"><span class="yiyi-st" id="yiyi-61"> <code class="descclassname">xml.dom.</code><code class="descname">EMPTY_NAMESPACE</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-62">The value used to indicate that no namespace is associated with a node in the DOM. </span><span class="yiyi-st" id="yiyi-63">This is typically found as the <code class="xref py py-attr docutils literal"><span class="pre">namespaceURI</span></code> of a node, or used as the <em>namespaceURI</em> parameter to a namespaces-specific method.</span></p></dd></dl><dl class="data"><dt id="xml.dom.XML_NAMESPACE"><span class="yiyi-st" id="yiyi-64"> <code class="descclassname">xml.dom.</code><code class="descname">XML_NAMESPACE</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-65">The namespace URI associated with the reserved prefix <code class="docutils literal"><span class="pre">xml</span></code>, as defined by <a class="reference external" href="https://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a> (section 4).</span></p></dd></dl><dl class="data"><dt id="xml.dom.XMLNS_NAMESPACE"><span class="yiyi-st" id="yiyi-66"> <code class="descclassname">xml.dom.</code><code class="descname">XMLNS_NAMESPACE</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-67">The namespace URI for namespace declarations, as defined by <a class="reference external" href="https://www.w3.org/TR/DOM-Level-2-Core/core.html">Document Object Model (DOM) Level 2 Core Specification</a> (section 1.1.8).</span></p></dd></dl><dl class="data"><dt id="xml.dom.XHTML_NAMESPACE"><span class="yiyi-st" id="yiyi-68"> <code class="descclassname">xml.dom.</code><code class="descname">XHTML_NAMESPACE</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-69">The URI of the XHTML namespace as defined by <a class="reference external" href="https://www.w3.org/TR/xhtml1/">XHTML 1.0: The Extensible HyperText Markup Language</a> (section 3.1.1).</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-70">In addition, <a class="reference internal" href="#module-xml.dom" title="xml.dom: Document Object Model API for Python."><code class="xref py py-mod docutils literal"><span class="pre">xml.dom</span></code></a> contains a base <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> class and the DOM exception classes. </span><span class="yiyi-st" id="yiyi-71">The <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> 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. </span><span class="yiyi-st" id="yiyi-72">The <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> class provided as part of this module does provide the constants used for the <code class="xref py py-attr docutils literal"><span class="pre">nodeType</span></code> attribute on concrete <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> objects; they are located within the class rather than at the module level to conform with the DOM specifications.</span></p></div><div class="section" id="objects-in-the-dom"><h2><span class="yiyi-st" id="yiyi-73">20.6.2. </span><span class="yiyi-st" id="yiyi-74">Objects in the DOM</span></h2><p><span class="yiyi-st" id="yiyi-75">The definitive documentation for the DOM is the DOM specification from the W3C.</span></p><p><span class="yiyi-st" id="yiyi-76">Note that DOM attributes may also be manipulated as nodes instead of as simple strings. </span><span class="yiyi-st" id="yiyi-77">It is fairly rare that you must do this, however, so this usage is not yet documented.</span></p><table border="1" class="docutils"><thead valign="bottom"><tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-78">Interface</span></th><th class="head"><span class="yiyi-st" id="yiyi-79">Section</span></th><th class="head"><span class="yiyi-st" id="yiyi-80">Purpose</span></th></tr></thead><tbody valign="top"><tr class="row-even"><td><span class="yiyi-st" id="yiyi-81"><code class="xref py py-class docutils literal"><span class="pre">DOMImplementation</span></code></span></td><td><span class="yiyi-st" id="yiyi-82"><a class="reference internal" href="#dom-implementation-objects"><span>DOMImplementation Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-83">Interface to the underlying implementation.</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-84"><code class="xref py py-class docutils literal"><span class="pre">Node</span></code></span></td><td><span class="yiyi-st" id="yiyi-85"><a class="reference internal" href="#dom-node-objects"><span>Node Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-86">Base interface for most objects in a document.</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-87"><code class="xref py py-class docutils literal"><span class="pre">NodeList</span></code></span></td><td><span class="yiyi-st" id="yiyi-88"><a class="reference internal" href="#dom-nodelist-objects"><span>NodeList Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-89">Interface for a sequence of nodes.</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-90"><code class="xref py py-class docutils literal"><span class="pre">DocumentType</span></code></span></td><td><span class="yiyi-st" id="yiyi-91"><a class="reference internal" href="#dom-documenttype-objects"><span>DocumentType Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-92">Information about the declarations needed to process a document.</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-93"><code class="xref py py-class docutils literal"><span class="pre">Document</span></code></span></td><td><span class="yiyi-st" id="yiyi-94"><a class="reference internal" href="#dom-document-objects"><span>Document Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-95">Object which represents an entire document.</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-96"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></span></td><td><span class="yiyi-st" id="yiyi-97"><a class="reference internal" href="#dom-element-objects"><span>Element Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-98">Element nodes in the document hierarchy.</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-99"><code class="xref py py-class docutils literal"><span class="pre">Attr</span></code></span></td><td><span class="yiyi-st" id="yiyi-100"><a class="reference internal" href="#dom-attr-objects"><span>Attr Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-101">Attribute value nodes on element nodes.</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-102"><code class="xref py py-class docutils literal"><span class="pre">Comment</span></code></span></td><td><span class="yiyi-st" id="yiyi-103"><a class="reference internal" href="#dom-comment-objects"><span>Comment Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-104">Representation of comments in the source document.</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-105"><code class="xref py py-class docutils literal"><span class="pre">Text</span></code></span></td><td><span class="yiyi-st" id="yiyi-106"><a class="reference internal" href="#dom-text-objects"><span>Text and CDATASection Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-107">Nodes containing textual content from the document.</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-108"><code class="xref py py-class docutils literal"><span class="pre">ProcessingInstruction</span></code></span></td><td><span class="yiyi-st" id="yiyi-109"><a class="reference internal" href="#dom-pi-objects"><span>ProcessingInstruction Objects</span></a></span></td><td><span class="yiyi-st" id="yiyi-110">Processing instruction representation.</span></td></tr></tbody></table><p><span class="yiyi-st" id="yiyi-111">An additional section describes the exceptions defined for working with the DOM in Python.</span></p><div class="section" id="domimplementation-objects"><h3><span class="yiyi-st" id="yiyi-112">20.6.2.1. </span><span class="yiyi-st" id="yiyi-113">DOMImplementation Objects</span></h3><p><span class="yiyi-st" id="yiyi-114">The <code class="xref py py-class docutils literal"><span class="pre">DOMImplementation</span></code> interface provides a way for applications to determine the availability of particular features in the DOM they are using. </span><span class="yiyi-st" id="yiyi-115">DOM Level 2 added the ability to create new <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> and <code class="xref py py-class docutils literal"><span class="pre">DocumentType</span></code> objects using the <code class="xref py py-class docutils literal"><span class="pre">DOMImplementation</span></code> as well.</span></p><dl class="method"><dt id="xml.dom.DOMImplementation.hasFeature"><span class="yiyi-st" id="yiyi-116"> <code class="descclassname">DOMImplementation.</code><code class="descname">hasFeature</code><span class="sig-paren">(</span><em>feature</em>, <em>version</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-117">Return true if the feature identified by the pair of strings <em>feature</em> and <em>version</em> is implemented.</span></p></dd></dl><dl class="method"><dt id="xml.dom.DOMImplementation.createDocument"><span class="yiyi-st" id="yiyi-118"> <code class="descclassname">DOMImplementation.</code><code class="descname">createDocument</code><span class="sig-paren">(</span><em>namespaceUri</em>, <em>qualifiedName</em>, <em>doctype</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-119">Return a new <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> object (the root of the DOM), with a child <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> object having the given <em>namespaceUri</em> and <em>qualifiedName</em>. </span><span class="yiyi-st" id="yiyi-120">The <em>doctype</em> must be a <code class="xref py py-class docutils literal"><span class="pre">DocumentType</span></code> object created by <a class="reference internal" href="#xml.dom.DOMImplementation.createDocumentType" title="xml.dom.DOMImplementation.createDocumentType"><code class="xref py py-meth docutils literal"><span class="pre">createDocumentType()</span></code></a>, or <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-121">In the Python DOM API, the first two arguments can also be <code class="docutils literal"><span class="pre">None</span></code> in order to indicate that no <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> child is to be created.</span></p></dd></dl><dl class="method"><dt id="xml.dom.DOMImplementation.createDocumentType"><span class="yiyi-st" id="yiyi-122"> <code class="descclassname">DOMImplementation.</code><code class="descname">createDocumentType</code><span class="sig-paren">(</span><em>qualifiedName</em>, <em>publicId</em>, <em>systemId</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-123">Return a new <code class="xref py py-class docutils literal"><span class="pre">DocumentType</span></code> object that encapsulates the given <em>qualifiedName</em>, <em>publicId</em>, and <em>systemId</em> strings, representing the information contained in an XML document type declaration.</span></p></dd></dl></div><div class="section" id="node-objects"><h3><span class="yiyi-st" id="yiyi-124">20.6.2.2. </span><span class="yiyi-st" id="yiyi-125">Node Objects</span></h3><p><span class="yiyi-st" id="yiyi-126">All of the components of an XML document are subclasses of <code class="xref py py-class docutils literal"><span class="pre">Node</span></code>.</span></p><dl class="attribute"><dt id="xml.dom.Node.nodeType"><span class="yiyi-st" id="yiyi-127"> <code class="descclassname">Node.</code><code class="descname">nodeType</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-128">An integer representing the node type. </span><span class="yiyi-st" id="yiyi-129">Symbolic constants for the types are on the <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> object: <code class="xref py py-const docutils literal"><span class="pre">ELEMENT_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">ATTRIBUTE_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">TEXT_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">CDATA_SECTION_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">ENTITY_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">PROCESSING_INSTRUCTION_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">COMMENT_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">DOCUMENT_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">DOCUMENT_TYPE_NODE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">NOTATION_NODE</span></code>. </span><span class="yiyi-st" id="yiyi-130">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.parentNode"><span class="yiyi-st" id="yiyi-131"> <code class="descclassname">Node.</code><code class="descname">parentNode</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-132">The parent of the current node, or <code class="docutils literal"><span class="pre">None</span></code> for the document node. </span><span class="yiyi-st" id="yiyi-133">The value is always a <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> object or <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-134">For <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> nodes, this will be the parent element, except for the root element, in which case it will be the <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> object. </span><span class="yiyi-st" id="yiyi-135">For <code class="xref py py-class docutils literal"><span class="pre">Attr</span></code> nodes, this is always <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-136">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.attributes"><span class="yiyi-st" id="yiyi-137"> <code class="descclassname">Node.</code><code class="descname">attributes</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-138">A <code class="xref py py-class docutils literal"><span class="pre">NamedNodeMap</span></code> of attribute objects. </span><span class="yiyi-st" id="yiyi-139">Only elements have actual values for this; others provide <code class="docutils literal"><span class="pre">None</span></code> for this attribute. </span><span class="yiyi-st" id="yiyi-140">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.previousSibling"><span class="yiyi-st" id="yiyi-141"> <code class="descclassname">Node.</code><code class="descname">previousSibling</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-142">The node that immediately precedes this one with the same parent. </span><span class="yiyi-st" id="yiyi-143">For instance the element with an end-tag that comes just before the <em>self</em> element’s start-tag. </span><span class="yiyi-st" id="yiyi-144">Of course, XML documents are made up of more than just elements so the previous sibling could be text, a comment, or something else. </span><span class="yiyi-st" id="yiyi-145">If this node is the first child of the parent, this attribute will be <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-146">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.nextSibling"><span class="yiyi-st" id="yiyi-147"> <code class="descclassname">Node.</code><code class="descname">nextSibling</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-148">The node that immediately follows this one with the same parent. </span><span class="yiyi-st" id="yiyi-149">See also <a class="reference internal" href="#xml.dom.Node.previousSibling" title="xml.dom.Node.previousSibling"><code class="xref py py-attr docutils literal"><span class="pre">previousSibling</span></code></a>. </span><span class="yiyi-st" id="yiyi-150">If this is the last child of the parent, this attribute will be <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-151">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.childNodes"><span class="yiyi-st" id="yiyi-152"> <code class="descclassname">Node.</code><code class="descname">childNodes</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-153">A list of nodes contained within this node. </span><span class="yiyi-st" id="yiyi-154">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.firstChild"><span class="yiyi-st" id="yiyi-155"> <code class="descclassname">Node.</code><code class="descname">firstChild</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-156">The first child of the node, if there are any, or <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-157">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.lastChild"><span class="yiyi-st" id="yiyi-158"> <code class="descclassname">Node.</code><code class="descname">lastChild</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-159">The last child of the node, if there are any, or <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-160">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.localName"><span class="yiyi-st" id="yiyi-161"> <code class="descclassname">Node.</code><code class="descname">localName</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-162">The part of the <code class="xref py py-attr docutils literal"><span class="pre">tagName</span></code> following the colon if there is one, else the entire <code class="xref py py-attr docutils literal"><span class="pre">tagName</span></code>. </span><span class="yiyi-st" id="yiyi-163">The value is a string.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.prefix"><span class="yiyi-st" id="yiyi-164"> <code class="descclassname">Node.</code><code class="descname">prefix</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-165">The part of the <code class="xref py py-attr docutils literal"><span class="pre">tagName</span></code> preceding the colon if there is one, else the empty string. </span><span class="yiyi-st" id="yiyi-166">The value is a string, or <code class="docutils literal"><span class="pre">None</span></code>.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.namespaceURI"><span class="yiyi-st" id="yiyi-167"> <code class="descclassname">Node.</code><code class="descname">namespaceURI</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-168">The namespace associated with the element name. </span><span class="yiyi-st" id="yiyi-169">This will be a string or <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-170">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.nodeName"><span class="yiyi-st" id="yiyi-171"> <code class="descclassname">Node.</code><code class="descname">nodeName</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-172">This has a different meaning for each node type; see the DOM specification for details. </span><span class="yiyi-st" id="yiyi-173">You can always get the information you would get here from another property such as the <code class="xref py py-attr docutils literal"><span class="pre">tagName</span></code> property for elements or the <code class="xref py py-attr docutils literal"><span class="pre">name</span></code> property for attributes. </span><span class="yiyi-st" id="yiyi-174">For all node types, the value of this attribute will be either a string or <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-175">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Node.nodeValue"><span class="yiyi-st" id="yiyi-176"> <code class="descclassname">Node.</code><code class="descname">nodeValue</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-177">This has a different meaning for each node type; see the DOM specification for details. </span><span class="yiyi-st" id="yiyi-178">The situation is similar to that with <a class="reference internal" href="#xml.dom.Node.nodeName" title="xml.dom.Node.nodeName"><code class="xref py py-attr docutils literal"><span class="pre">nodeName</span></code></a>. </span><span class="yiyi-st" id="yiyi-179">The value is a string or <code class="docutils literal"><span class="pre">None</span></code>.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Node.hasAttributes"><span class="yiyi-st" id="yiyi-180"> <code class="descclassname">Node.</code><code class="descname">hasAttributes</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-181">Returns true if the node has any attributes.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Node.hasChildNodes"><span class="yiyi-st" id="yiyi-182"> <code class="descclassname">Node.</code><code class="descname">hasChildNodes</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-183">Returns true if the node has any child nodes.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Node.isSameNode"><span class="yiyi-st" id="yiyi-184"> <code class="descclassname">Node.</code><code class="descname">isSameNode</code><span class="sig-paren">(</span><em>other</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-185">Returns true if <em>other</em> refers to the same node as this node. </span><span class="yiyi-st" id="yiyi-186">This is especially useful for DOM implementations which use any sort of proxy architecture (because more than one object can refer to the same node).</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-187">Note</span></p><p class="last"><span class="yiyi-st" id="yiyi-188">This is based on a proposed DOM Level 3 API which is still in the “working draft” stage, but this particular interface appears uncontroversial. </span><span class="yiyi-st" id="yiyi-189">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).</span></p></div></dd></dl><dl class="method"><dt id="xml.dom.Node.appendChild"><span class="yiyi-st" id="yiyi-190"> <code class="descclassname">Node.</code><code class="descname">appendChild</code><span class="sig-paren">(</span><em>newChild</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-191">Add a new child node to this node at the end of the list of children, returning <em>newChild</em>. </span><span class="yiyi-st" id="yiyi-192">If the node was already in the tree, it is removed first.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Node.insertBefore"><span class="yiyi-st" id="yiyi-193"> <code class="descclassname">Node.</code><code class="descname">insertBefore</code><span class="sig-paren">(</span><em>newChild</em>, <em>refChild</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-194">Insert a new child node before an existing child. </span><span class="yiyi-st" id="yiyi-195">It must be the case that <em>refChild</em> is a child of this node; if not, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised. </span><span class="yiyi-st" id="yiyi-196"><em>newChild</em> is returned. </span><span class="yiyi-st" id="yiyi-197">If <em>refChild</em> is <code class="docutils literal"><span class="pre">None</span></code>, it inserts <em>newChild</em> at the end of the children’s list.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Node.removeChild"><span class="yiyi-st" id="yiyi-198"> <code class="descclassname">Node.</code><code class="descname">removeChild</code><span class="sig-paren">(</span><em>oldChild</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-199">Remove a child node. </span><span class="yiyi-st" id="yiyi-200"><em>oldChild</em> must be a child of this node; if not, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised. </span><span class="yiyi-st" id="yiyi-201"><em>oldChild</em> is returned on success. </span><span class="yiyi-st" id="yiyi-202">If <em>oldChild</em> will not be used further, its <code class="xref py py-meth docutils literal"><span class="pre">unlink()</span></code> method should be called.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Node.replaceChild"><span class="yiyi-st" id="yiyi-203"> <code class="descclassname">Node.</code><code class="descname">replaceChild</code><span class="sig-paren">(</span><em>newChild</em>, <em>oldChild</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-204">Replace an existing node with a new node. </span><span class="yiyi-st" id="yiyi-205">It must be the case that <em>oldChild</em> is a child of this node; if not, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Node.normalize"><span class="yiyi-st" id="yiyi-206"> <code class="descclassname">Node.</code><code class="descname">normalize</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-207">Join adjacent text nodes so that all stretches of text are stored as single <code class="xref py py-class docutils literal"><span class="pre">Text</span></code> instances. </span><span class="yiyi-st" id="yiyi-208">This simplifies processing text from a DOM tree for many applications.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Node.cloneNode"><span class="yiyi-st" id="yiyi-209"> <code class="descclassname">Node.</code><code class="descname">cloneNode</code><span class="sig-paren">(</span><em>deep</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-210">Clone this node. </span><span class="yiyi-st" id="yiyi-211">Setting <em>deep</em> means to clone all child nodes as well. </span><span class="yiyi-st" id="yiyi-212">This returns the clone.</span></p></dd></dl></div><div class="section" id="nodelist-objects"><h3><span class="yiyi-st" id="yiyi-213">20.6.2.3. </span><span class="yiyi-st" id="yiyi-214">NodeList Objects</span></h3><p><span class="yiyi-st" id="yiyi-215">A <code class="xref py py-class docutils literal"><span class="pre">NodeList</span></code> represents a sequence of nodes. </span><span class="yiyi-st" id="yiyi-216">These objects are used in two ways in the DOM Core recommendation: the <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> objects provides one as its list of child nodes, and the <code class="xref py py-meth docutils literal"><span class="pre">getElementsByTagName()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">getElementsByTagNameNS()</span></code> methods of <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> return objects with this interface to represent query results.</span></p><p><span class="yiyi-st" id="yiyi-217">The DOM Level 2 recommendation defines one method and one attribute for these objects:</span></p><dl class="method"><dt id="xml.dom.NodeList.item"><span class="yiyi-st" id="yiyi-218"> <code class="descclassname">NodeList.</code><code class="descname">item</code><span class="sig-paren">(</span><em>i</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-219">Return the <em>i</em>‘th item from the sequence, if there is one, or <code class="docutils literal"><span class="pre">None</span></code>. </span><span class="yiyi-st" id="yiyi-220">The index <em>i</em> is not allowed to be less than zero or greater than or equal to the length of the sequence.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.NodeList.length"><span class="yiyi-st" id="yiyi-221"> <code class="descclassname">NodeList.</code><code class="descname">length</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-222">The number of nodes in the sequence.</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-223">In addition, the Python DOM interface requires that some additional support is provided to allow <code class="xref py py-class docutils literal"><span class="pre">NodeList</span></code> objects to be used as Python sequences. </span><span class="yiyi-st" id="yiyi-224">All <code class="xref py py-class docutils literal"><span class="pre">NodeList</span></code> implementations must include support for <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal"><span class="pre">__len__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a>; this allows iteration over the <code class="xref py py-class docutils literal"><span class="pre">NodeList</span></code> in <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal"><span class="pre">for</span></code></a> statements and proper support for the <a class="reference internal" href="functions.html#len" title="len"><code class="xref py py-func docutils literal"><span class="pre">len()</span></code></a> built-in function.</span></p><p><span class="yiyi-st" id="yiyi-225">If a DOM implementation supports modification of the document, the <code class="xref py py-class docutils literal"><span class="pre">NodeList</span></code> implementation must also support the <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal"><span class="pre">__delitem__()</span></code></a> methods.</span></p></div><div class="section" id="documenttype-objects"><h3><span class="yiyi-st" id="yiyi-226">20.6.2.4. </span><span class="yiyi-st" id="yiyi-227">DocumentType Objects</span></h3><p><span class="yiyi-st" id="yiyi-228">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 <code class="xref py py-class docutils literal"><span class="pre">DocumentType</span></code> object. </span><span class="yiyi-st" id="yiyi-229">The <code class="xref py py-class docutils literal"><span class="pre">DocumentType</span></code> for a document is available from the <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> object’s <code class="xref py py-attr docutils literal"><span class="pre">doctype</span></code> attribute; if there is no <code class="docutils literal"><span class="pre">DOCTYPE</span></code> declaration for the document, the document’s <code class="xref py py-attr docutils literal"><span class="pre">doctype</span></code> attribute will be set to <code class="docutils literal"><span class="pre">None</span></code> instead of an instance of this interface.</span></p><p><span class="yiyi-st" id="yiyi-230"><code class="xref py py-class docutils literal"><span class="pre">DocumentType</span></code> is a specialization of <code class="xref py py-class docutils literal"><span class="pre">Node</span></code>, and adds the following attributes:</span></p><dl class="attribute"><dt id="xml.dom.DocumentType.publicId"><span class="yiyi-st" id="yiyi-231"> <code class="descclassname">DocumentType.</code><code class="descname">publicId</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-232">The public identifier for the external subset of the document type definition. </span><span class="yiyi-st" id="yiyi-233">This will be a string or <code class="docutils literal"><span class="pre">None</span></code>.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.DocumentType.systemId"><span class="yiyi-st" id="yiyi-234"> <code class="descclassname">DocumentType.</code><code class="descname">systemId</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-235">The system identifier for the external subset of the document type definition. </span><span class="yiyi-st" id="yiyi-236">This will be a URI as a string, or <code class="docutils literal"><span class="pre">None</span></code>.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.DocumentType.internalSubset"><span class="yiyi-st" id="yiyi-237"> <code class="descclassname">DocumentType.</code><code class="descname">internalSubset</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-238">A string giving the complete internal subset from the document. </span><span class="yiyi-st" id="yiyi-239">This does not include the brackets which enclose the subset. </span><span class="yiyi-st" id="yiyi-240">If the document has no internal subset, this should be <code class="docutils literal"><span class="pre">None</span></code>.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.DocumentType.name"><span class="yiyi-st" id="yiyi-241"> <code class="descclassname">DocumentType.</code><code class="descname">name</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-242">The name of the root element as given in the <code class="docutils literal"><span class="pre">DOCTYPE</span></code> declaration, if present.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.DocumentType.entities"><span class="yiyi-st" id="yiyi-243"> <code class="descclassname">DocumentType.</code><code class="descname">entities</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-244">This is a <code class="xref py py-class docutils literal"><span class="pre">NamedNodeMap</span></code> giving the definitions of external entities. </span><span class="yiyi-st" id="yiyi-245">For entity names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). </span><span class="yiyi-st" id="yiyi-246">This may be <code class="docutils literal"><span class="pre">None</span></code> if the information is not provided by the parser, or if no entities are defined.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.DocumentType.notations"><span class="yiyi-st" id="yiyi-247"> <code class="descclassname">DocumentType.</code><code class="descname">notations</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-248">This is a <code class="xref py py-class docutils literal"><span class="pre">NamedNodeMap</span></code> giving the definitions of notations. </span><span class="yiyi-st" id="yiyi-249">For notation names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). </span><span class="yiyi-st" id="yiyi-250">This may be <code class="docutils literal"><span class="pre">None</span></code> if the information is not provided by the parser, or if no notations are defined.</span></p></dd></dl></div><div class="section" id="document-objects"><h3><span class="yiyi-st" id="yiyi-251">20.6.2.5. </span><span class="yiyi-st" id="yiyi-252">Document Objects</span></h3><p><span class="yiyi-st" id="yiyi-253">A <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> represents an entire XML document, including its constituent elements, attributes, processing instructions, comments etc. </span><span class="yiyi-st" id="yiyi-254">Remember that it inherits properties from <code class="xref py py-class docutils literal"><span class="pre">Node</span></code>.</span></p><dl class="attribute"><dt id="xml.dom.Document.documentElement"><span class="yiyi-st" id="yiyi-255"> <code class="descclassname">Document.</code><code class="descname">documentElement</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-256">The one and only root element of the document.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.createElement"><span class="yiyi-st" id="yiyi-257"> <code class="descclassname">Document.</code><code class="descname">createElement</code><span class="sig-paren">(</span><em>tagName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-258">Create and return a new element node. </span><span class="yiyi-st" id="yiyi-259">The element is not inserted into the document when it is created. </span><span class="yiyi-st" id="yiyi-260">You need to explicitly insert it with one of the other methods such as <code class="xref py py-meth docutils literal"><span class="pre">insertBefore()</span></code> or <code class="xref py py-meth docutils literal"><span class="pre">appendChild()</span></code>.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.createElementNS"><span class="yiyi-st" id="yiyi-261"> <code class="descclassname">Document.</code><code class="descname">createElementNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>tagName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-262">Create and return a new element with a namespace. </span><span class="yiyi-st" id="yiyi-263">The <em>tagName</em> may have a prefix. </span><span class="yiyi-st" id="yiyi-264">The element is not inserted into the document when it is created. </span><span class="yiyi-st" id="yiyi-265">You need to explicitly insert it with one of the other methods such as <code class="xref py py-meth docutils literal"><span class="pre">insertBefore()</span></code> or <code class="xref py py-meth docutils literal"><span class="pre">appendChild()</span></code>.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.createTextNode"><span class="yiyi-st" id="yiyi-266"> <code class="descclassname">Document.</code><code class="descname">createTextNode</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-267">Create and return a text node containing the data passed as a parameter. </span><span class="yiyi-st" id="yiyi-268">As with the other creation methods, this one does not insert the node into the tree.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.createComment"><span class="yiyi-st" id="yiyi-269"> <code class="descclassname">Document.</code><code class="descname">createComment</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-270">Create and return a comment node containing the data passed as a parameter. </span><span class="yiyi-st" id="yiyi-271">As with the other creation methods, this one does not insert the node into the tree.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.createProcessingInstruction"><span class="yiyi-st" id="yiyi-272"> <code class="descclassname">Document.</code><code class="descname">createProcessingInstruction</code><span class="sig-paren">(</span><em>target</em>, <em>data</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-273">Create and return a processing instruction node containing the <em>target</em> and <em>data</em> passed as parameters. </span><span class="yiyi-st" id="yiyi-274">As with the other creation methods, this one does not insert the node into the tree.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.createAttribute"><span class="yiyi-st" id="yiyi-275"> <code class="descclassname">Document.</code><code class="descname">createAttribute</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-276">Create and return an attribute node. </span><span class="yiyi-st" id="yiyi-277">This method does not associate the attribute node with any particular element. </span><span class="yiyi-st" id="yiyi-278">You must use <code class="xref py py-meth docutils literal"><span class="pre">setAttributeNode()</span></code> on the appropriate <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> object to use the newly created attribute instance.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.createAttributeNS"><span class="yiyi-st" id="yiyi-279"> <code class="descclassname">Document.</code><code class="descname">createAttributeNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>qualifiedName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-280">Create and return an attribute node with a namespace. </span><span class="yiyi-st" id="yiyi-281">The <em>tagName</em> may have a prefix. </span><span class="yiyi-st" id="yiyi-282">This method does not associate the attribute node with any particular element. </span><span class="yiyi-st" id="yiyi-283">You must use <code class="xref py py-meth docutils literal"><span class="pre">setAttributeNode()</span></code> on the appropriate <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> object to use the newly created attribute instance.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.getElementsByTagName"><span class="yiyi-st" id="yiyi-284"> <code class="descclassname">Document.</code><code class="descname">getElementsByTagName</code><span class="sig-paren">(</span><em>tagName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-285">Search for all descendants (direct children, children’s children, etc.) </span><span class="yiyi-st" id="yiyi-286">with a particular element type name.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Document.getElementsByTagNameNS"><span class="yiyi-st" id="yiyi-287"> <code class="descclassname">Document.</code><code class="descname">getElementsByTagNameNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>localName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-288">Search for all descendants (direct children, children’s children, etc.) </span><span class="yiyi-st" id="yiyi-289">with a particular namespace URI and localname. </span><span class="yiyi-st" id="yiyi-290">The localname is the part of the namespace after the prefix.</span></p></dd></dl></div><div class="section" id="element-objects"><h3><span class="yiyi-st" id="yiyi-291">20.6.2.6. </span><span class="yiyi-st" id="yiyi-292">Element Objects</span></h3><p><span class="yiyi-st" id="yiyi-293"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code> is a subclass of <code class="xref py py-class docutils literal"><span class="pre">Node</span></code>, so inherits all the attributes of that class.</span></p><dl class="attribute"><dt id="xml.dom.Element.tagName"><span class="yiyi-st" id="yiyi-294"> <code class="descclassname">Element.</code><code class="descname">tagName</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-295">The element type name. </span><span class="yiyi-st" id="yiyi-296">In a namespace-using document it may have colons in it. </span><span class="yiyi-st" id="yiyi-297">The value is a string.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.getElementsByTagName"><span class="yiyi-st" id="yiyi-298"> <code class="descclassname">Element.</code><code class="descname">getElementsByTagName</code><span class="sig-paren">(</span><em>tagName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-299">Same as equivalent method in the <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> class.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.getElementsByTagNameNS"><span class="yiyi-st" id="yiyi-300"> <code class="descclassname">Element.</code><code class="descname">getElementsByTagNameNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>localName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-301">Same as equivalent method in the <code class="xref py py-class docutils literal"><span class="pre">Document</span></code> class.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.hasAttribute"><span class="yiyi-st" id="yiyi-302"> <code class="descclassname">Element.</code><code class="descname">hasAttribute</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-303">Returns true if the element has an attribute named by <em>name</em>.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.hasAttributeNS"><span class="yiyi-st" id="yiyi-304"> <code class="descclassname">Element.</code><code class="descname">hasAttributeNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>localName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-305">Returns true if the element has an attribute named by <em>namespaceURI</em> and <em>localName</em>.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.getAttribute"><span class="yiyi-st" id="yiyi-306"> <code class="descclassname">Element.</code><code class="descname">getAttribute</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-307">Return the value of the attribute named by <em>name</em> as a string. </span><span class="yiyi-st" id="yiyi-308">If no such attribute exists, an empty string is returned, as if the attribute had no value.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.getAttributeNode"><span class="yiyi-st" id="yiyi-309"> <code class="descclassname">Element.</code><code class="descname">getAttributeNode</code><span class="sig-paren">(</span><em>attrname</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-310">Return the <code class="xref py py-class docutils literal"><span class="pre">Attr</span></code> node for the attribute named by <em>attrname</em>.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.getAttributeNS"><span class="yiyi-st" id="yiyi-311"> <code class="descclassname">Element.</code><code class="descname">getAttributeNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>localName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-312">Return the value of the attribute named by <em>namespaceURI</em> and <em>localName</em> as a string. </span><span class="yiyi-st" id="yiyi-313">If no such attribute exists, an empty string is returned, as if the attribute had no value.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.getAttributeNodeNS"><span class="yiyi-st" id="yiyi-314"> <code class="descclassname">Element.</code><code class="descname">getAttributeNodeNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>localName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-315">Return an attribute value as a node, given a <em>namespaceURI</em> and <em>localName</em>.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.removeAttribute"><span class="yiyi-st" id="yiyi-316"> <code class="descclassname">Element.</code><code class="descname">removeAttribute</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-317">Remove an attribute by name. </span><span class="yiyi-st" id="yiyi-318">If there is no matching attribute, a <a class="reference internal" href="#xml.dom.NotFoundErr" title="xml.dom.NotFoundErr"><code class="xref py py-exc docutils literal"><span class="pre">NotFoundErr</span></code></a> is raised.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.removeAttributeNode"><span class="yiyi-st" id="yiyi-319"> <code class="descclassname">Element.</code><code class="descname">removeAttributeNode</code><span class="sig-paren">(</span><em>oldAttr</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-320">Remove and return <em>oldAttr</em> from the attribute list, if present. </span><span class="yiyi-st" id="yiyi-321">If <em>oldAttr</em> is not present, <a class="reference internal" href="#xml.dom.NotFoundErr" title="xml.dom.NotFoundErr"><code class="xref py py-exc docutils literal"><span class="pre">NotFoundErr</span></code></a> is raised.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.removeAttributeNS"><span class="yiyi-st" id="yiyi-322"> <code class="descclassname">Element.</code><code class="descname">removeAttributeNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>localName</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-323">Remove an attribute by name. </span><span class="yiyi-st" id="yiyi-324">Note that it uses a localName, not a qname. </span><span class="yiyi-st" id="yiyi-325">No exception is raised if there is no matching attribute.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.setAttribute"><span class="yiyi-st" id="yiyi-326"> <code class="descclassname">Element.</code><code class="descname">setAttribute</code><span class="sig-paren">(</span><em>name</em>, <em>value</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-327">Set an attribute value from a string.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.setAttributeNode"><span class="yiyi-st" id="yiyi-328"> <code class="descclassname">Element.</code><code class="descname">setAttributeNode</code><span class="sig-paren">(</span><em>newAttr</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-329">Add a new attribute node to the element, replacing an existing attribute if necessary if the <code class="xref py py-attr docutils literal"><span class="pre">name</span></code> attribute matches. </span><span class="yiyi-st" id="yiyi-330">If a replacement occurs, the old attribute node will be returned. </span><span class="yiyi-st" id="yiyi-331">If <em>newAttr</em> is already in use, <a class="reference internal" href="#xml.dom.InuseAttributeErr" title="xml.dom.InuseAttributeErr"><code class="xref py py-exc docutils literal"><span class="pre">InuseAttributeErr</span></code></a> will be raised.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.setAttributeNodeNS"><span class="yiyi-st" id="yiyi-332"> <code class="descclassname">Element.</code><code class="descname">setAttributeNodeNS</code><span class="sig-paren">(</span><em>newAttr</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-333">Add a new attribute node to the element, replacing an existing attribute if necessary if the <code class="xref py py-attr docutils literal"><span class="pre">namespaceURI</span></code> and <code class="xref py py-attr docutils literal"><span class="pre">localName</span></code> attributes match. </span><span class="yiyi-st" id="yiyi-334">If a replacement occurs, the old attribute node will be returned. </span><span class="yiyi-st" id="yiyi-335">If <em>newAttr</em> is already in use, <a class="reference internal" href="#xml.dom.InuseAttributeErr" title="xml.dom.InuseAttributeErr"><code class="xref py py-exc docutils literal"><span class="pre">InuseAttributeErr</span></code></a> will be raised.</span></p></dd></dl><dl class="method"><dt id="xml.dom.Element.setAttributeNS"><span class="yiyi-st" id="yiyi-336"> <code class="descclassname">Element.</code><code class="descname">setAttributeNS</code><span class="sig-paren">(</span><em>namespaceURI</em>, <em>qname</em>, <em>value</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-337">Set an attribute value from a string, given a <em>namespaceURI</em> and a <em>qname</em>. </span><span class="yiyi-st" id="yiyi-338">Note that a qname is the whole attribute name. </span><span class="yiyi-st" id="yiyi-339">This is different than above.</span></p></dd></dl></div><div class="section" id="attr-objects"><h3><span class="yiyi-st" id="yiyi-340">20.6.2.7. </span><span class="yiyi-st" id="yiyi-341">Attr Objects</span></h3><p><span class="yiyi-st" id="yiyi-342"><code class="xref py py-class docutils literal"><span class="pre">Attr</span></code> inherits from <code class="xref py py-class docutils literal"><span class="pre">Node</span></code>, so inherits all its attributes.</span></p><dl class="attribute"><dt id="xml.dom.Attr.name"><span class="yiyi-st" id="yiyi-343"> <code class="descclassname">Attr.</code><code class="descname">name</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-344">The attribute name. </span><span class="yiyi-st" id="yiyi-345">In a namespace-using document it may include a colon.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Attr.localName"><span class="yiyi-st" id="yiyi-346"> <code class="descclassname">Attr.</code><code class="descname">localName</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-347">The part of the name following the colon if there is one, else the entire name. </span><span class="yiyi-st" id="yiyi-348">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Attr.prefix"><span class="yiyi-st" id="yiyi-349"> <code class="descclassname">Attr.</code><code class="descname">prefix</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-350">The part of the name preceding the colon if there is one, else the empty string.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.Attr.value"><span class="yiyi-st" id="yiyi-351"> <code class="descclassname">Attr.</code><code class="descname">value</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-352">The text value of the attribute. </span><span class="yiyi-st" id="yiyi-353">This is a synonym for the <code class="xref py py-attr docutils literal"><span class="pre">nodeValue</span></code> attribute.</span></p></dd></dl></div><div class="section" id="namednodemap-objects"><h3><span class="yiyi-st" id="yiyi-354">20.6.2.8. </span><span class="yiyi-st" id="yiyi-355">NamedNodeMap Objects</span></h3><p><span class="yiyi-st" id="yiyi-356"><code class="xref py py-class docutils literal"><span class="pre">NamedNodeMap</span></code> does <em>not</em> inherit from <code class="xref py py-class docutils literal"><span class="pre">Node</span></code>.</span></p><dl class="attribute"><dt id="xml.dom.NamedNodeMap.length"><span class="yiyi-st" id="yiyi-357"> <code class="descclassname">NamedNodeMap.</code><code class="descname">length</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-358">The length of the attribute list.</span></p></dd></dl><dl class="method"><dt id="xml.dom.NamedNodeMap.item"><span class="yiyi-st" id="yiyi-359"> <code class="descclassname">NamedNodeMap.</code><code class="descname">item</code><span class="sig-paren">(</span><em>index</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-360">Return an attribute with a particular index. </span><span class="yiyi-st" id="yiyi-361">The order you get the attributes in is arbitrary but will be consistent for the life of a DOM. </span><span class="yiyi-st" id="yiyi-362">Each item is an attribute node. </span><span class="yiyi-st" id="yiyi-363">Get its value with the <code class="xref py py-attr docutils literal"><span class="pre">value</span></code> attribute.</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-364">There are also experimental methods that give this class more mapping behavior. </span><span class="yiyi-st" id="yiyi-365">You can use them or you can use the standardized <code class="xref py py-meth docutils literal"><span class="pre">getAttribute*()</span></code> family of methods on the <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> objects.</span></p></div><div class="section" id="comment-objects"><h3><span class="yiyi-st" id="yiyi-366">20.6.2.9. </span><span class="yiyi-st" id="yiyi-367">Comment Objects</span></h3><p><span class="yiyi-st" id="yiyi-368"><code class="xref py py-class docutils literal"><span class="pre">Comment</span></code> represents a comment in the XML document. </span><span class="yiyi-st" id="yiyi-369">It is a subclass of <code class="xref py py-class docutils literal"><span class="pre">Node</span></code>, but cannot have child nodes.</span></p><dl class="attribute"><dt id="xml.dom.Comment.data"><span class="yiyi-st" id="yiyi-370"> <code class="descclassname">Comment.</code><code class="descname">data</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-371">The content of the comment as a string. </span><span class="yiyi-st" id="yiyi-372">The attribute contains all characters between the leading <code class="docutils literal"><span class="pre"><!-</span></code><code class="docutils literal"><span class="pre">-</span></code> and trailing <code class="docutils literal"><span class="pre">-</span></code><code class="docutils literal"><span class="pre">-></span></code>, but does not include them.</span></p></dd></dl></div><div class="section" id="text-and-cdatasection-objects"><h3><span class="yiyi-st" id="yiyi-373">20.6.2.10. </span><span class="yiyi-st" id="yiyi-374">Text and CDATASection Objects</span></h3><p><span class="yiyi-st" id="yiyi-375">The <code class="xref py py-class docutils literal"><span class="pre">Text</span></code> interface represents text in the XML document. </span><span class="yiyi-st" id="yiyi-376">If the parser and DOM implementation support the DOM’s XML extension, portions of the text enclosed in CDATA marked sections are stored in <code class="xref py py-class docutils literal"><span class="pre">CDATASection</span></code> objects. </span><span class="yiyi-st" id="yiyi-377">These two interfaces are identical, but provide different values for the <code class="xref py py-attr docutils literal"><span class="pre">nodeType</span></code> attribute.</span></p><p><span class="yiyi-st" id="yiyi-378">These interfaces extend the <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> interface. </span><span class="yiyi-st" id="yiyi-379">They cannot have child nodes.</span></p><dl class="attribute"><dt id="xml.dom.Text.data"><span class="yiyi-st" id="yiyi-380"> <code class="descclassname">Text.</code><code class="descname">data</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-381">The content of the text node as a string.</span></p></dd></dl><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-382">Note</span></p><p class="last"><span class="yiyi-st" id="yiyi-383">The use of a <code class="xref py py-class docutils literal"><span class="pre">CDATASection</span></code> 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. </span><span class="yiyi-st" id="yiyi-384">A single CDATA section may be represented by more than one node in the document tree. </span><span class="yiyi-st" id="yiyi-385">There is no way to determine whether two adjacent <code class="xref py py-class docutils literal"><span class="pre">CDATASection</span></code> nodes represent different CDATA marked sections.</span></p></div></div><div class="section" id="processinginstruction-objects"><h3><span class="yiyi-st" id="yiyi-386">20.6.2.11. </span><span class="yiyi-st" id="yiyi-387">ProcessingInstruction Objects</span></h3><p><span class="yiyi-st" id="yiyi-388">Represents a processing instruction in the XML document; this inherits from the <code class="xref py py-class docutils literal"><span class="pre">Node</span></code> interface and cannot have child nodes.</span></p><dl class="attribute"><dt id="xml.dom.ProcessingInstruction.target"><span class="yiyi-st" id="yiyi-389"> <code class="descclassname">ProcessingInstruction.</code><code class="descname">target</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-390">The content of the processing instruction up to the first whitespace character. </span><span class="yiyi-st" id="yiyi-391">This is a read-only attribute.</span></p></dd></dl><dl class="attribute"><dt id="xml.dom.ProcessingInstruction.data"><span class="yiyi-st" id="yiyi-392"> <code class="descclassname">ProcessingInstruction.</code><code class="descname">data</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-393">The content of the processing instruction following the first whitespace character.</span></p></dd></dl></div><div class="section" id="exceptions"><h3><span class="yiyi-st" id="yiyi-394">20.6.2.12. </span><span class="yiyi-st" id="yiyi-395">Exceptions</span></h3><p><span class="yiyi-st" id="yiyi-396">The DOM Level 2 recommendation defines a single exception, <a class="reference internal" href="#xml.dom.DOMException" title="xml.dom.DOMException"><code class="xref py py-exc docutils literal"><span class="pre">DOMException</span></code></a>, and a number of constants that allow applications to determine what sort of error occurred. </span><span class="yiyi-st" id="yiyi-397"><a class="reference internal" href="#xml.dom.DOMException" title="xml.dom.DOMException"><code class="xref py py-exc docutils literal"><span class="pre">DOMException</span></code></a> instances carry a <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-attr docutils literal"><span class="pre">code</span></code></a> attribute that provides the appropriate value for the specific exception.</span></p><p><span class="yiyi-st" id="yiyi-398">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. </span><span class="yiyi-st" id="yiyi-399">The implementations must raise the appropriate specific exception, each of which carries the appropriate value for the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-attr docutils literal"><span class="pre">code</span></code></a> attribute.</span></p><dl class="exception"><dt id="xml.dom.DOMException"><span class="yiyi-st" id="yiyi-400"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">DOMException</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-401">Base exception class used for all specific DOM exceptions. </span><span class="yiyi-st" id="yiyi-402">This exception class cannot be directly instantiated.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.DomstringSizeErr"><span class="yiyi-st" id="yiyi-403"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">DomstringSizeErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-404">Raised when a specified range of text does not fit into a string. </span><span class="yiyi-st" id="yiyi-405">This is not known to be used in the Python DOM implementations, but may be received from DOM implementations not written in Python.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.HierarchyRequestErr"><span class="yiyi-st" id="yiyi-406"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">HierarchyRequestErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-407">Raised when an attempt is made to insert a node where the node type is not allowed.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.IndexSizeErr"><span class="yiyi-st" id="yiyi-408"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">IndexSizeErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-409">Raised when an index or size parameter to a method is negative or exceeds the allowed values.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.InuseAttributeErr"><span class="yiyi-st" id="yiyi-410"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">InuseAttributeErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-411">Raised when an attempt is made to insert an <code class="xref py py-class docutils literal"><span class="pre">Attr</span></code> node that is already present elsewhere in the document.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.InvalidAccessErr"><span class="yiyi-st" id="yiyi-412"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">InvalidAccessErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-413">Raised if a parameter or an operation is not supported on the underlying object.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.InvalidCharacterErr"><span class="yiyi-st" id="yiyi-414"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">InvalidCharacterErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-415">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. </span><span class="yiyi-st" id="yiyi-416">For example, attempting to create an <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> node with a space in the element type name will cause this error to be raised.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.InvalidModificationErr"><span class="yiyi-st" id="yiyi-417"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">InvalidModificationErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-418">Raised when an attempt is made to modify the type of a node.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.InvalidStateErr"><span class="yiyi-st" id="yiyi-419"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">InvalidStateErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-420">Raised when an attempt is made to use an object that is not defined or is no longer usable.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.NamespaceErr"><span class="yiyi-st" id="yiyi-421"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">NamespaceErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-422">If an attempt is made to change any object in a way that is not permitted with regard to the <a class="reference external" href="https://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a> recommendation, this exception is raised.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.NotFoundErr"><span class="yiyi-st" id="yiyi-423"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">NotFoundErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-424">Exception when a node does not exist in the referenced context. </span><span class="yiyi-st" id="yiyi-425">For example, <code class="xref py py-meth docutils literal"><span class="pre">NamedNodeMap.removeNamedItem()</span></code> will raise this if the node passed in does not exist in the map.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.NotSupportedErr"><span class="yiyi-st" id="yiyi-426"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">NotSupportedErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-427">Raised when the implementation does not support the requested type of object or operation.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.NoDataAllowedErr"><span class="yiyi-st" id="yiyi-428"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">NoDataAllowedErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-429">This is raised if data is specified for a node which does not support data.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.NoModificationAllowedErr"><span class="yiyi-st" id="yiyi-430"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">NoModificationAllowedErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-431">Raised on attempts to modify an object where modifications are not allowed (such as for read-only nodes).</span></p></dd></dl><dl class="exception"><dt id="xml.dom.SyntaxErr"><span class="yiyi-st" id="yiyi-432"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">SyntaxErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-433">Raised when an invalid or illegal string is specified.</span></p></dd></dl><dl class="exception"><dt id="xml.dom.WrongDocumentErr"><span class="yiyi-st" id="yiyi-434"> <em class="property">exception </em><code class="descclassname">xml.dom.</code><code class="descname">WrongDocumentErr</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-435">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.</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-436">The exception codes defined in the DOM recommendation map to the exceptions described above according to this table:</span></p><table border="1" class="docutils"><thead valign="bottom"><tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-437">Constant</span></th><th class="head"><span class="yiyi-st" id="yiyi-438">Exception</span></th></tr></thead><tbody valign="top"><tr class="row-even"><td><span class="yiyi-st" id="yiyi-439"><code class="xref py py-const docutils literal"><span class="pre">DOMSTRING_SIZE_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-440"><a class="reference internal" href="#xml.dom.DomstringSizeErr" title="xml.dom.DomstringSizeErr"><code class="xref py py-exc docutils literal"><span class="pre">DomstringSizeErr</span></code></a></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-441"><code class="xref py py-const docutils literal"><span class="pre">HIERARCHY_REQUEST_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-442"><a class="reference internal" href="#xml.dom.HierarchyRequestErr" title="xml.dom.HierarchyRequestErr"><code class="xref py py-exc docutils literal"><span class="pre">HierarchyRequestErr</span></code></a></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-443"><code class="xref py py-const docutils literal"><span class="pre">INDEX_SIZE_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-444"><a class="reference internal" href="#xml.dom.IndexSizeErr" title="xml.dom.IndexSizeErr"><code class="xref py py-exc docutils literal"><span class="pre">IndexSizeErr</span></code></a></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-445"><code class="xref py py-const docutils literal"><span class="pre">INUSE_ATTRIBUTE_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-446"><a class="reference internal" href="#xml.dom.InuseAttributeErr" title="xml.dom.InuseAttributeErr"><code class="xref py py-exc docutils literal"><span class="pre">InuseAttributeErr</span></code></a></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-447"><code class="xref py py-const docutils literal"><span class="pre">INVALID_ACCESS_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-448"><a class="reference internal" href="#xml.dom.InvalidAccessErr" title="xml.dom.InvalidAccessErr"><code class="xref py py-exc docutils literal"><span class="pre">InvalidAccessErr</span></code></a></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-449"><code class="xref py py-const docutils literal"><span class="pre">INVALID_CHARACTER_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-450"><a class="reference internal" href="#xml.dom.InvalidCharacterErr" title="xml.dom.InvalidCharacterErr"><code class="xref py py-exc docutils literal"><span class="pre">InvalidCharacterErr</span></code></a></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-451"><code class="xref py py-const docutils literal"><span class="pre">INVALID_MODIFICATION_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-452"><a class="reference internal" href="#xml.dom.InvalidModificationErr" title="xml.dom.InvalidModificationErr"><code class="xref py py-exc docutils literal"><span class="pre">InvalidModificationErr</span></code></a></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-453"><code class="xref py py-const docutils literal"><span class="pre">INVALID_STATE_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-454"><a class="reference internal" href="#xml.dom.InvalidStateErr" title="xml.dom.InvalidStateErr"><code class="xref py py-exc docutils literal"><span class="pre">InvalidStateErr</span></code></a></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-455"><code class="xref py py-const docutils literal"><span class="pre">NAMESPACE_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-456"><a class="reference internal" href="#xml.dom.NamespaceErr" title="xml.dom.NamespaceErr"><code class="xref py py-exc docutils literal"><span class="pre">NamespaceErr</span></code></a></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-457"><code class="xref py py-const docutils literal"><span class="pre">NOT_FOUND_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-458"><a class="reference internal" href="#xml.dom.NotFoundErr" title="xml.dom.NotFoundErr"><code class="xref py py-exc docutils literal"><span class="pre">NotFoundErr</span></code></a></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-459"><code class="xref py py-const docutils literal"><span class="pre">NOT_SUPPORTED_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-460"><a class="reference internal" href="#xml.dom.NotSupportedErr" title="xml.dom.NotSupportedErr"><code class="xref py py-exc docutils literal"><span class="pre">NotSupportedErr</span></code></a></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-461"><code class="xref py py-const docutils literal"><span class="pre">NO_DATA_ALLOWED_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-462"><a class="reference internal" href="#xml.dom.NoDataAllowedErr" title="xml.dom.NoDataAllowedErr"><code class="xref py py-exc docutils literal"><span class="pre">NoDataAllowedErr</span></code></a></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-463"><code class="xref py py-const docutils literal"><span class="pre">NO_MODIFICATION_ALLOWED_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-464"><a class="reference internal" href="#xml.dom.NoModificationAllowedErr" title="xml.dom.NoModificationAllowedErr"><code class="xref py py-exc docutils literal"><span class="pre">NoModificationAllowedErr</span></code></a></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-465"><code class="xref py py-const docutils literal"><span class="pre">SYNTAX_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-466"><a class="reference internal" href="#xml.dom.SyntaxErr" title="xml.dom.SyntaxErr"><code class="xref py py-exc docutils literal"><span class="pre">SyntaxErr</span></code></a></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-467"><code class="xref py py-const docutils literal"><span class="pre">WRONG_DOCUMENT_ERR</span></code></span></td><td><span class="yiyi-st" id="yiyi-468"><a class="reference internal" href="#xml.dom.WrongDocumentErr" title="xml.dom.WrongDocumentErr"><code class="xref py py-exc docutils literal"><span class="pre">WrongDocumentErr</span></code></a></span></td></tr></tbody></table></div></div><div class="section" id="conformance"><h2><span class="yiyi-st" id="yiyi-469">20.6.3. </span><span class="yiyi-st" id="yiyi-470">Conformance</span></h2><p><span class="yiyi-st" id="yiyi-471">This section describes the conformance requirements and relationships between the Python DOM API, the W3C DOM recommendations, and the OMG IDL mapping for Python.</span></p><div class="section" id="type-mapping"><h3><span class="yiyi-st" id="yiyi-472">20.6.3.1. </span><span class="yiyi-st" id="yiyi-473">Type Mapping</span></h3><p><span class="yiyi-st" id="yiyi-474">The IDL types used in the DOM specification are mapped to Python types according to the following table.</span></p><table border="1" class="docutils"><thead valign="bottom"><tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-475">IDL Type</span></th><th class="head"><span class="yiyi-st" id="yiyi-476">Python Type</span></th></tr></thead><tbody valign="top"><tr class="row-even"><td><span class="yiyi-st" id="yiyi-477"><code class="docutils literal"><span class="pre">boolean</span></code></span></td><td><span class="yiyi-st" id="yiyi-478"><code class="docutils literal"><span class="pre">bool</span></code> or <code class="docutils literal"><span class="pre">int</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-479"><code class="docutils literal"><span class="pre">int</span></code></span></td><td><span class="yiyi-st" id="yiyi-480"><code class="docutils literal"><span class="pre">int</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-481"><code class="docutils literal"><span class="pre">long</span> <span class="pre">int</span></code></span></td><td><span class="yiyi-st" id="yiyi-482"><code class="docutils literal"><span class="pre">int</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-483"><code class="docutils literal"><span class="pre">unsigned</span> <span class="pre">int</span></code></span></td><td><span class="yiyi-st" id="yiyi-484"><code class="docutils literal"><span class="pre">int</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-485"><code class="docutils literal"><span class="pre">DOMString</span></code></span></td><td><span class="yiyi-st" id="yiyi-486"><code class="docutils literal"><span class="pre">str</span></code> or <code class="docutils literal"><span class="pre">bytes</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-487"><code class="docutils literal"><span class="pre">null</span></code></span></td><td><span class="yiyi-st" id="yiyi-488"><code class="docutils literal"><span class="pre">None</span></code></span></td></tr></tbody></table></div><div class="section" id="accessor-methods"><h3><span class="yiyi-st" id="yiyi-489">20.6.3.2. </span><span class="yiyi-st" id="yiyi-490">Accessor Methods</span></h3><p><span class="yiyi-st" id="yiyi-491">The mapping from OMG IDL to Python defines accessor functions for IDL <code class="docutils literal"><span class="pre">attribute</span></code> declarations in much the way the Java mapping does. </span><span class="yiyi-st" id="yiyi-492">Mapping the IDL declarations</span></p><pre><code class="language-python"><span></span><span class="n">readonly</span> <span class="n">attribute</span> <span class="n">string</span> <span class="n">someValue</span><span class="p">;</span>
|
||
<span class="n">attribute</span> <span class="n">string</span> <span class="n">anotherValue</span><span class="p">;</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-493">yields three accessor functions: a “get” method for <code class="xref py py-attr docutils literal"><span class="pre">someValue</span></code> (<code class="xref py py-meth docutils literal"><span class="pre">_get_someValue()</span></code>), and “get” and “set” methods for <code class="xref py py-attr docutils literal"><span class="pre">anotherValue</span></code> (<code class="xref py py-meth docutils literal"><span class="pre">_get_anotherValue()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">_set_anotherValue()</span></code>). </span><span class="yiyi-st" id="yiyi-494">The mapping, in particular, does not require that the IDL attributes are accessible as normal Python attributes: <code class="docutils literal"><span class="pre">object.someValue</span></code> is <em>not</em> required to work, and may raise an <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></code></a>.</span></p><p><span class="yiyi-st" id="yiyi-495">The Python DOM API, however, <em>does</em> require that normal attribute access work. </span><span class="yiyi-st" id="yiyi-496">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. </span><span class="yiyi-st" id="yiyi-497">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. </span><span class="yiyi-st" id="yiyi-498">Attributes that are declared <code class="docutils literal"><span class="pre">readonly</span></code> may not restrict write access in all DOM implementations.</span></p><p><span class="yiyi-st" id="yiyi-499">In the Python DOM API, accessor functions are not required. </span><span class="yiyi-st" id="yiyi-500">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. </span><span class="yiyi-st" id="yiyi-501">“Set” accessors should never be provided for <code class="docutils literal"><span class="pre">readonly</span></code> attributes.</span></p><p><span class="yiyi-st" id="yiyi-502">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 <code class="xref py py-meth docutils literal"><span class="pre">getElementsByTagName()</span></code>, being “live”. </span><span class="yiyi-st" id="yiyi-503">The Python DOM API does not require implementations to enforce such requirements.</span></p></div></div></div></div> |