\section{\module{xml.dom} --- The Document Object Model API} \declaremodule{standard}{xml.dom}\modulesynopsis{Document Object Model API for Python.}\sectionauthor{Paul Prescod}{paul@prescod.net}\sectionauthor{Martin v. L\"owis}{martin@v.loewis.de} \versionadded{2.0} The Document Object Model, or ``DOM,'' is a cross-language API fromthe World Wide Web Consortium (W3C) for accessing and modifying XMLdocuments. A DOM implementation presents an XML document as a treestructure, or allows client code to build such a structure fromscratch. It then gives access to the structure through a set ofobjects which provided well-known interfaces. The DOM is extremely useful for random-access applications. SAX onlyallows you a view of one bit of the document at a time. If you arelooking at one SAX element, you have no access to another. If you arelooking at a text node, you have no access to a containing element.When you write a SAX application, you need to keep track of yourprogram's position in the document somewhere in your own code. SAXdoes not do it for you. Also, if you need to look ahead in the XMLdocument, you are just out of luck. Some applications are simply impossible in an event driven model withno access to a tree. Of course you could build some sort of treeyourself in SAX events, but the DOM allows you to avoid writing thatcode. The DOM is a standard tree representation for XML data. %What if your needs are somewhere between SAX and the DOM? Perhaps%you cannot afford to load the entire tree in memory but you find the%SAX model somewhat cumbersome and low-level. There is also a module%called xml.dom.pulldom that allows you to build trees of only the%parts of a document that you need structured access to. It also has%features that allow you to find your way around the DOM.% See http://www.prescod.net/python/pulldom The Document Object Model is being defined by the W3C in stages, or``levels'' in their terminology. The Python mapping of the API issubstantially based on the DOM Level~2 recommendation. The mapping ofthe Level~3 specification, currently only available in draft form, isbeing developed by the \ulink{Python XML Special InterestGroup}{http://www.python.org/sigs/xml-sig/} as part of the\ulink{PyXML package}{http://pyxml.sourceforge.net/}. Refer to thedocumentation bundled with that package for information on the currentstate of DOM Level~3 support. DOM applications typically start by parsing some XML into a DOM. Howthis is accomplished is not covered at all by DOM Level~1, and Level~2provides only limited improvements: There is a\class{DOMImplementation} object class which provides access to\class{Document} creation methods, but no way to access an XMLreader/parser/Document builder in an implementation-independent way.There is also no well-defined way to access these methods without anexisting \class{Document} object. In Python, each DOM implementationwill provide a function \function{getDOMImplementation()}. DOM Level~3adds a Load/Store specification, which defines an interface to thereader, but this is not yet available in the Python standard library. Once you have a DOM document object, you can access the parts of yourXML document through its properties and methods. These properties aredefined in the DOM specification; this portion of the reference manualdescribes the interpretation of the specification in Python. The specification provided by the W3C defines the DOM API for Java,ECMAScript, and OMG IDL. The Python mapping defined here is based inlarge part on the IDL version of the specification, but strictcompliance is not required (though implementations are free to supportthe strict mapping from IDL). See section \ref{dom-conformance},``Conformance,'' for a detailed discussion of mapping requirements. \begin{seealso} \seetitle[http://www.w3.org/TR/DOM-Level-2-Core/]{Document Object Model (DOM) Level~2 Specification} {The W3C recommendation upon which the Python DOM API is based.} \seetitle[http://www.w3.org/TR/REC-DOM-Level-1/]{Document Object Model (DOM) Level~1 Specification} {The W3C recommendation for the DOM supported by \module{xml.dom.minidom}.} \seetitle[http://pyxml.sourceforge.net]{PyXML}{Users that require a full-featured implementation of DOM should use the PyXML package.} \seetitle[http://www.omg.org/docs/formal/02-11-05.pdf]{Python Language Mapping Specification} {This specifies the mapping from OMG IDL to Python.}\end{seealso} \subsection{Module Contents} The \module{xml.dom} contains the following functions: \begin{funcdesc}{registerDOMImplementation}{name, factory}Register the \var{factory} function with the name \var{name}. Thefactory function should return an object which implements the\class{DOMImplementation} interface. The factory function can returnthe same object every time, or a new one for each call, as appropriatefor the specific implementation (e.g. if that implementation supportssome customization).\end{funcdesc} \begin{funcdesc}{getDOMImplementation}{\optional{name\optional{, features}}}Return a suitable DOM implementation. The \var{name} is eitherwell-known, the module name of a DOM implementation, or\code{None}. If it is not \code{None}, imports the correspondingmodule and returns a \class{DOMImplementation} object if the importsucceeds. If no name is given, and if the environment variable\envvar{PYTHON_DOM} is set, this variable is used to find theimplementation. If name is not given, this examines the available implementations tofind one with the required feature set. If no implementation can befound, raise an \exception{ImportError}. The features list must be asequence of \code{(\var{feature}, \var{version})} pairs which arepassed to the \method{hasFeature()} method on available\class{DOMImplementation} objects.\end{funcdesc} Some convenience constants are also provided: \begin{datadesc}{EMPTY_NAMESPACE} The value used to indicate that no namespace is associated with a node in the DOM. This is typically found as the \member{namespaceURI} of a node, or used as the \var{namespaceURI} parameter to a namespaces-specific method. \versionadded{2.2}\end{datadesc} \begin{datadesc}{XML_NAMESPACE} The namespace URI associated with the reserved prefix \code{xml}, as defined by \citetitle[http://www.w3.org/TR/REC-xml-names/]{Namespaces in XML} (section~4). \versionadded{2.2}\end{datadesc} \begin{datadesc}{XMLNS_NAMESPACE} The namespace URI for namespace declarations, as defined by \citetitle[http://www.w3.org/TR/DOM-Level-2-Core/core.html]{Document Object Model (DOM) Level~2 Core Specification} (section~1.1.8). \versionadded{2.2}\end{datadesc} \begin{datadesc}{XHTML_NAMESPACE} The URI of the XHTML namespace as defined by \citetitle[http://www.w3.org/TR/xhtml1/]{XHTML 1.0: The Extensible HyperText Markup Language} (section~3.1.1). \versionadded{2.2}\end{datadesc} % Should the Node documentation go here? In addition, \module{xml.dom} contains a base \class{Node} class andthe DOM exception classes. The \class{Node} class provided by thismodule does not implement any of the methods or attributes defined bythe DOM specification; concrete DOM implementations must providethose. The \class{Node} class provided as part of this module doesprovide the constants used for the \member{nodeType} attribute onconcrete \class{Node} objects; they are located within the classrather than at the module level to conform with the DOMspecifications. \subsection{Objects in the DOM \label{dom-objects}} The definitive documentation for the DOM is the DOM specification fromthe W3C. Note that DOM attributes may also be manipulated as nodes instead ofas simple strings. It is fairly rare that you must do this, however,so this usage is not yet documented. \begin{tableiii}{l|l|l}{class}{Interface}{Section}{Purpose} \lineiii{DOMImplementation}{\ref{dom-implementation-objects}} {Interface to the underlying implementation.} \lineiii{Node}{\ref{dom-node-objects}} {Base interface for most objects in a document.} \lineiii{NodeList}{\ref{dom-nodelist-objects}} {Interface for a sequence of nodes.} \lineiii{DocumentType}{\ref{dom-documenttype-objects}} {Information about the declarations needed to process a document.} \lineiii{Document}{\ref{dom-document-objects}} {Object which represents an entire document.} \lineiii{Element}{\ref{dom-element-objects}} {Element nodes in the document hierarchy.} \lineiii{Attr}{\ref{dom-attr-objects}} {Attribute value nodes on element nodes.} \lineiii{Comment}{\ref{dom-comment-objects}} {Representation of comments in the source document.} \lineiii{Text}{\ref{dom-text-objects}} {Nodes containing textual content from the document.} \lineiii{ProcessingInstruction}{\ref{dom-pi-objects}} {Processing instruction representation.}\end{tableiii} An additional section describes the exceptions defined for workingwith the DOM in Python. \subsubsection{DOMImplementation Objects \label{dom-implementation-objects}} The \class{DOMImplementation} interface provides a way forapplications to determine the availability of particular features inthe DOM they are using. DOM Level~2 added the ability to create new\class{Document} and \class{DocumentType} objects using the\class{DOMImplementation} as well. \begin{methoddesc}[DOMImplementation]{hasFeature}{feature, version}Return true if the feature identified by the pair of strings\var{feature} and \var{version} is implemented.\end{methoddesc} \begin{methoddesc}[DOMImplementation]{createDocument}{namespaceUri, qualifiedName, doctype}Return a new \class{Document} object (the root of the DOM), with achild \class{Element} object having the given \var{namespaceUri} and\var{qualifiedName}. The \var{doctype} must be a \class{DocumentType}object created by \method{createDocumentType()}, or \code{None}.In the Python DOM API, the first two arguments can also be \code{None}in order to indicate that no \class{Element} child is to be created.\end{methoddesc} \begin{methoddesc}[DOMImplementation]{createDocumentType}{qualifiedName, publicId, systemId}Return a new \class{DocumentType} object that encapsulates the given\var{qualifiedName}, \var{publicId}, and \var{systemId} strings,representing the information contained in an XML document typedeclaration.\end{methoddesc} \subsubsection{Node Objects \label{dom-node-objects}} All of the components of an XML document are subclasses of\class{Node}. \begin{memberdesc}[Node]{nodeType}An integer representing the node type. Symbolic constants for thetypes are on the \class{Node} object:\constant{ELEMENT_NODE}, \constant{ATTRIBUTE_NODE},\constant{TEXT_NODE}, \constant{CDATA_SECTION_NODE},\constant{ENTITY_NODE}, \constant{PROCESSING_INSTRUCTION_NODE},\constant{COMMENT_NODE}, \constant{DOCUMENT_NODE},\constant{DOCUMENT_TYPE_NODE}, \constant{NOTATION_NODE}.This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{parentNode}The parent of the current node, or \code{None} for the document node.The value is always a \class{Node} object or \code{None}. For\class{Element} nodes, this will be the parent element, except for theroot element, in which case it will be the \class{Document} object.For \class{Attr} nodes, this is always \code{None}.This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{attributes}A \class{NamedNodeMap} of attribute objects. Only elements haveactual values for this; others provide \code{None} for this attribute.This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{previousSibling}The node that immediately precedes this one with the same parent. Forinstance the element with an end-tag that comes just before the\var{self} element's start-tag. Of course, XML documents are madeup of more than just elements so the previous sibling could be text, acomment, or something else. If this node is the first child of theparent, this attribute will be \code{None}.This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{nextSibling}The node that immediately follows this one with the same parent. Seealso \member{previousSibling}. If this is the last child of theparent, this attribute will be \code{None}.This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{childNodes}A list of nodes contained within this node.This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{firstChild}The first child of the node, if there are any, or \code{None}.This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{lastChild}The last child of the node, if there are any, or \code{None}.This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{localName}The part of the \member{tagName} following the colon if there is one,else the entire \member{tagName}. The value is a string.\end{memberdesc} \begin{memberdesc}[Node]{prefix}The part of the \member{tagName} preceding the colon if there is one,else the empty string. The value is a string, or \code{None}\end{memberdesc} \begin{memberdesc}[Node]{namespaceURI}The namespace associated with the element name. This will be astring or \code{None}. This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{nodeName}This has a different meaning for each node type; see the DOMspecification for details. You can always get the information youwould get here from another property such as the \member{tagName}property for elements or the \member{name} property for attributes.For all node types, the value of this attribute will be either astring or \code{None}. This is a read-only attribute.\end{memberdesc} \begin{memberdesc}[Node]{nodeValue}This has a different meaning for each node type; see the DOMspecification for details. The situation is similar to that with\member{nodeName}. The value is a string or \code{None}.\end{memberdesc} \begin{methoddesc}[Node]{hasAttributes}{}Returns true if the node has any attributes.\end{methoddesc} \begin{methoddesc}[Node]{hasChildNodes}{}Returns true if the node has any child nodes.\end{methoddesc} \begin{methoddesc}[Node]{isSameNode}{other}Returns true if \var{other} refers to the same node as this node.This is especially useful for DOM implementations which use any sortof proxy architecture (because more than one object can refer to thesame node). \begin{notice} This is based on a proposed DOM Level~3 API which is still in the ``working draft'' stage, but this particular interface appears uncontroversial. Changes from the W3C will not necessarily affect this method in the Python DOM interface (though any new W3C API for this would also be supported).\end{notice}\end{methoddesc} \begin{methoddesc}[Node]{appendChild}{newChild}Add a new child node to this node at the end of the list of children,returning \var{newChild}.\end{methoddesc} \begin{methoddesc}[Node]{insertBefore}{newChild, refChild}Insert a new child node before an existing child. It must be the casethat \var{refChild} is a child of this node; if not,\exception{ValueError} is raised. \var{newChild} is returned. If\var{refChild} is \code{None}, it inserts \var{newChild} at the end ofthe children's list.\end{methoddesc} \begin{methoddesc}[Node]{removeChild}{oldChild}Remove a child node. \var{oldChild} must be a child of this node; ifnot, \exception{ValueError} is raised. \var{oldChild} is returned onsuccess. If \var{oldChild} will not be used further, its\method{unlink()} method should be called.\end{methoddesc} \begin{methoddesc}[Node]{replaceChild}{newChild, oldChild}Replace an existing node with a new node. It must be the case that \var{oldChild} is a child of this node; if not,\exception{ValueError} is raised.\end{methoddesc} \begin{methoddesc}[Node]{normalize}{}Join adjacent text nodes so that all stretches of text are stored assingle \class{Text} instances. This simplifies processing text from aDOM tree for many applications.\versionadded{2.1}\end{methoddesc} \begin{methoddesc}[Node]{cloneNode}{deep}Clone this node. Setting \var{deep} means to clone all child nodes aswell. This returns the clone.\end{methoddesc} \subsubsection{NodeList Objects \label{dom-nodelist-objects}} A \class{NodeList} represents a sequence of nodes. These objects areused in two ways in the DOM Core recommendation: the\class{Element} objects provides one as its list of child nodes, andthe \method{getElementsByTagName()} and\method{getElementsByTagNameNS()} methods of \class{Node} returnobjects with this interface to represent query results. The DOM Level~2 recommendation defines one method and one attributefor these objects: \begin{methoddesc}[NodeList]{item}{i} Return the \var{i}'th item from the sequence, if there is one, or \code{None}. The index \var{i} is not allowed to be less then zero or greater than or equal to the length of the sequence.\end{methoddesc} \begin{memberdesc}[NodeList]{length} The number of nodes in the sequence.\end{memberdesc} In addition, the Python DOM interface requires that some additionalsupport is provided to allow \class{NodeList} objects to be used asPython sequences. All \class{NodeList} implementations must includesupport for \method{__len__()} and \method{__getitem__()}; this allowsiteration over the \class{NodeList} in \keyword{for} statements andproper support for the \function{len()} built-in function. If a DOM implementation supports modification of the document, the\class{NodeList} implementation must also support the\method{__setitem__()} and \method{__delitem__()} methods. \subsubsection{DocumentType Objects \label{dom-documenttype-objects}} Information about the notations and entities declared by a document(including the external subset if the parser uses it and can providethe information) is available from a \class{DocumentType} object. The\class{DocumentType} for a document is available from the\class{Document} object's \member{doctype} attribute; if there is no\code{DOCTYPE} declaration for the document, the document's\member{doctype} attribute will be set to \code{None} instead of aninstance of this interface. \class{DocumentType} is a specialization of \class{Node}, and adds thefollowing attributes: \begin{memberdesc}[DocumentType]{publicId} The public identifier for the external subset of the document type definition. This will be a string or \code{None}.\end{memberdesc} \begin{memberdesc}[DocumentType]{systemId} The system identifier for the external subset of the document type definition. This will be a URI as a string, or \code{None}.\end{memberdesc} \begin{memberdesc}[DocumentType]{internalSubset} A string giving the complete internal subset from the document. This does not include the brackets which enclose the subset. If the document has no internal subset, this should be \code{None}.\end{memberdesc} \begin{memberdesc}[DocumentType]{name} The name of the root element as given in the \code{DOCTYPE} declaration, if present.\end{memberdesc} \begin{memberdesc}[DocumentType]{entities} This is a \class{NamedNodeMap} giving the definitions of external entities. For entity names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). This may be \code{None} if the information is not provided by the parser, or if no entities are defined.\end{memberdesc} \begin{memberdesc}[DocumentType]{notations} This is a \class{NamedNodeMap} giving the definitions of notations. For notation names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). This may be \code{None} if the information is not provided by the parser, or if no notations are defined.\end{memberdesc} \subsubsection{Document Objects \label{dom-document-objects}} A \class{Document} represents an entire XML document, including itsconstituent elements, attributes, processing instructions, commentsetc. Remeber that it inherits properties from \class{Node}. \begin{memberdesc}[Document]{documentElement}The one and only root element of the document.\end{memberdesc} \begin{methoddesc}[Document]{createElement}{tagName}Create and return a new element node. The element is not insertedinto the document when it is created. You need to explicitly insertit with one of the other methods such as \method{insertBefore()} or\method{appendChild()}.\end{methoddesc} \begin{methoddesc}[Document]{createElementNS}{namespaceURI, tagName}Create and return a new element with a namespace. The\var{tagName} may have a prefix. The element is not inserted into thedocument when it is created. You need to explicitly insert it withone of the other methods such as \method{insertBefore()} or\method{appendChild()}.Lines 1–500 of 928. Load more
\section{\module{xml.dom} --- The Document Object Model API} \declaremodule{standard}{xml.dom} \modulesynopsis{Document Object Model API for Python.} \sectionauthor{Paul Prescod}{paul@prescod.net} \sectionauthor{Martin v. L\"owis}{martin@v.loewis.de} \versionadded{2.0} The Document Object Model, or ``DOM,'' is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents. A DOM implementation presents an XML document as a tree structure, or allows client code to build such a structure from scratch. It then gives access to the structure through a set of objects which provided well-known interfaces. The DOM is extremely useful for random-access applications. SAX only allows you a view of one bit of the document at a time. If you are looking at one SAX element, you have no access to another. If you are looking at a text node, you have no access to a containing element. When you write a SAX application, you need to keep track of your program's position in the document somewhere in your own code. SAX does not do it for you. Also, if you need to look ahead in the XML document, you are just out of luck. Some applications are simply impossible in an event driven model with no access to a tree. Of course you could build some sort of tree yourself in SAX events, but the DOM allows you to avoid writing that code. The DOM is a standard tree representation for XML data. %What if your needs are somewhere between SAX and the DOM? Perhaps %you cannot afford to load the entire tree in memory but you find the %SAX model somewhat cumbersome and low-level. There is also a module %called xml.dom.pulldom that allows you to build trees of only the %parts of a document that you need structured access to. It also has %features that allow you to find your way around the DOM. % See http://www.prescod.net/python/pulldom The Document Object Model is being defined by the W3C in stages, or ``levels'' in their terminology. The Python mapping of the API is substantially based on the DOM Level~2 recommendation. The mapping of the Level~3 specification, currently only available in draft form, is being developed by the \ulink{Python XML Special Interest Group}{http://www.python.org/sigs/xml-sig/} as part of the \ulink{PyXML package}{http://pyxml.sourceforge.net/}. Refer to the documentation bundled with that package for information on the current state of DOM Level~3 support. DOM applications typically start by parsing some XML into a DOM. How this is accomplished is not covered at all by DOM Level~1, and Level~2 provides only limited improvements: There is a \class{DOMImplementation} object class which provides access to \class{Document} creation methods, but no way to access an XML reader/parser/Document builder in an implementation-independent way. There is also no well-defined way to access these methods without an existing \class{Document} object. In Python, each DOM implementation will provide a function \function{getDOMImplementation()}. DOM Level~3 adds a Load/Store specification, which defines an interface to the reader, but this is not yet available in the Python standard library. Once you have a DOM document object, you can access the parts of your XML document through its properties and methods. These properties are defined in the DOM specification; this portion of the reference manual describes the interpretation of the specification in Python. The specification provided by the W3C defines the DOM API for Java, ECMAScript, and OMG IDL. The Python mapping defined here is based in large part on the IDL version of the specification, but strict compliance is not required (though implementations are free to support the strict mapping from IDL). See section \ref{dom-conformance}, ``Conformance,'' for a detailed discussion of mapping requirements. \begin{seealso} \seetitle[http://www.w3.org/TR/DOM-Level-2-Core/]{Document Object Model (DOM) Level~2 Specification} {The W3C recommendation upon which the Python DOM API is based.} \seetitle[http://www.w3.org/TR/REC-DOM-Level-1/]{Document Object Model (DOM) Level~1 Specification} {The W3C recommendation for the DOM supported by \module{xml.dom.minidom}.} \seetitle[http://pyxml.sourceforge.net]{PyXML}{Users that require a full-featured implementation of DOM should use the PyXML package.} \seetitle[http://www.omg.org/docs/formal/02-11-05.pdf]{Python Language Mapping Specification} {This specifies the mapping from OMG IDL to Python.} \end{seealso} \subsection{Module Contents} The \module{xml.dom} contains the following functions: \begin{funcdesc}{registerDOMImplementation}{name, factory} Register the \var{factory} function with the name \var{name}. The factory function should return an object which implements the \class{DOMImplementation} interface. The factory function can return the same object every time, or a new one for each call, as appropriate for the specific implementation (e.g. if that implementation supports some customization). \end{funcdesc} \begin{funcdesc}{getDOMImplementation}{\optional{name\optional{, features}}} Return a suitable DOM implementation. The \var{name} is either well-known, the module name of a DOM implementation, or \code{None}. If it is not \code{None}, imports the corresponding module and returns a \class{DOMImplementation} object if the import succeeds. If no name is given, and if the environment variable \envvar{PYTHON_DOM} is set, this variable is used to find the implementation. If name is not given, this examines the available implementations to find one with the required feature set. If no implementation can be found, raise an \exception{ImportError}. The features list must be a sequence of \code{(\var{feature}, \var{version})} pairs which are passed to the \method{hasFeature()} method on available \class{DOMImplementation} objects. \end{funcdesc} Some convenience constants are also provided: \begin{datadesc}{EMPTY_NAMESPACE} The value used to indicate that no namespace is associated with a node in the DOM. This is typically found as the \member{namespaceURI} of a node, or used as the \var{namespaceURI} parameter to a namespaces-specific method. \versionadded{2.2} \end{datadesc} \begin{datadesc}{XML_NAMESPACE} The namespace URI associated with the reserved prefix \code{xml}, as defined by \citetitle[http://www.w3.org/TR/REC-xml-names/]{Namespaces in XML} (section~4). \versionadded{2.2} \end{datadesc} \begin{datadesc}{XMLNS_NAMESPACE} The namespace URI for namespace declarations, as defined by \citetitle[http://www.w3.org/TR/DOM-Level-2-Core/core.html]{Document Object Model (DOM) Level~2 Core Specification} (section~1.1.8). \versionadded{2.2} \end{datadesc} \begin{datadesc}{XHTML_NAMESPACE} The URI of the XHTML namespace as defined by \citetitle[http://www.w3.org/TR/xhtml1/]{XHTML 1.0: The Extensible HyperText Markup Language} (section~3.1.1). \versionadded{2.2} \end{datadesc} % Should the Node documentation go here? In addition, \module{xml.dom} contains a base \class{Node} class and the DOM exception classes. The \class{Node} class provided by this module does not implement any of the methods or attributes defined by the DOM specification; concrete DOM implementations must provide those. The \class{Node} class provided as part of this module does provide the constants used for the \member{nodeType} attribute on concrete \class{Node} objects; they are located within the class rather than at the module level to conform with the DOM specifications. \subsection{Objects in the DOM \label{dom-objects}} The definitive documentation for the DOM is the DOM specification from the W3C. Note that DOM attributes may also be manipulated as nodes instead of as simple strings. It is fairly rare that you must do this, however, so this usage is not yet documented. \begin{tableiii}{l|l|l}{class}{Interface}{Section}{Purpose} \lineiii{DOMImplementation}{\ref{dom-implementation-objects}} {Interface to the underlying implementation.} \lineiii{Node}{\ref{dom-node-objects}} {Base interface for most objects in a document.} \lineiii{NodeList}{\ref{dom-nodelist-objects}} {Interface for a sequence of nodes.} \lineiii{DocumentType}{\ref{dom-documenttype-objects}} {Information about the declarations needed to process a document.} \lineiii{Document}{\ref{dom-document-objects}} {Object which represents an entire document.} \lineiii{Element}{\ref{dom-element-objects}} {Element nodes in the document hierarchy.} \lineiii{Attr}{\ref{dom-attr-objects}} {Attribute value nodes on element nodes.} \lineiii{Comment}{\ref{dom-comment-objects}} {Representation of comments in the source document.} \lineiii{Text}{\ref{dom-text-objects}} {Nodes containing textual content from the document.} \lineiii{ProcessingInstruction}{\ref{dom-pi-objects}} {Processing instruction representation.} \end{tableiii} An additional section describes the exceptions defined for working with the DOM in Python. \subsubsection{DOMImplementation Objects \label{dom-implementation-objects}} The \class{DOMImplementation} interface provides a way for applications to determine the availability of particular features in the DOM they are using. DOM Level~2 added the ability to create new \class{Document} and \class{DocumentType} objects using the \class{DOMImplementation} as well. \begin{methoddesc}[DOMImplementation]{hasFeature}{feature, version} Return true if the feature identified by the pair of strings \var{feature} and \var{version} is implemented. \end{methoddesc} \begin{methoddesc}[DOMImplementation]{createDocument}{namespaceUri, qualifiedName, doctype} Return a new \class{Document} object (the root of the DOM), with a child \class{Element} object having the given \var{namespaceUri} and \var{qualifiedName}. The \var{doctype} must be a \class{DocumentType} object created by \method{createDocumentType()}, or \code{None}. In the Python DOM API, the first two arguments can also be \code{None} in order to indicate that no \class{Element} child is to be created. \end{methoddesc} \begin{methoddesc}[DOMImplementation]{createDocumentType}{qualifiedName, publicId, systemId} Return a new \class{DocumentType} object that encapsulates the given \var{qualifiedName}, \var{publicId}, and \var{systemId} strings, representing the information contained in an XML document type declaration. \end{methoddesc} \subsubsection{Node Objects \label{dom-node-objects}} All of the components of an XML document are subclasses of \class{Node}. \begin{memberdesc}[Node]{nodeType} An integer representing the node type. Symbolic constants for the types are on the \class{Node} object: \constant{ELEMENT_NODE}, \constant{ATTRIBUTE_NODE}, \constant{TEXT_NODE}, \constant{CDATA_SECTION_NODE}, \constant{ENTITY_NODE}, \constant{PROCESSING_INSTRUCTION_NODE}, \constant{COMMENT_NODE}, \constant{DOCUMENT_NODE}, \constant{DOCUMENT_TYPE_NODE}, \constant{NOTATION_NODE}. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{parentNode} The parent of the current node, or \code{None} for the document node. The value is always a \class{Node} object or \code{None}. For \class{Element} nodes, this will be the parent element, except for the root element, in which case it will be the \class{Document} object. For \class{Attr} nodes, this is always \code{None}. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{attributes} A \class{NamedNodeMap} of attribute objects. Only elements have actual values for this; others provide \code{None} for this attribute. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{previousSibling} The node that immediately precedes this one with the same parent. For instance the element with an end-tag that comes just before the \var{self} element's start-tag. Of course, XML documents are made up of more than just elements so the previous sibling could be text, a comment, or something else. If this node is the first child of the parent, this attribute will be \code{None}. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{nextSibling} The node that immediately follows this one with the same parent. See also \member{previousSibling}. If this is the last child of the parent, this attribute will be \code{None}. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{childNodes} A list of nodes contained within this node. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{firstChild} The first child of the node, if there are any, or \code{None}. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{lastChild} The last child of the node, if there are any, or \code{None}. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{localName} The part of the \member{tagName} following the colon if there is one, else the entire \member{tagName}. The value is a string. \end{memberdesc} \begin{memberdesc}[Node]{prefix} The part of the \member{tagName} preceding the colon if there is one, else the empty string. The value is a string, or \code{None} \end{memberdesc} \begin{memberdesc}[Node]{namespaceURI} The namespace associated with the element name. This will be a string or \code{None}. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{nodeName} This has a different meaning for each node type; see the DOM specification for details. You can always get the information you would get here from another property such as the \member{tagName} property for elements or the \member{name} property for attributes. For all node types, the value of this attribute will be either a string or \code{None}. This is a read-only attribute. \end{memberdesc} \begin{memberdesc}[Node]{nodeValue} This has a different meaning for each node type; see the DOM specification for details. The situation is similar to that with \member{nodeName}. The value is a string or \code{None}. \end{memberdesc} \begin{methoddesc}[Node]{hasAttributes}{} Returns true if the node has any attributes. \end{methoddesc} \begin{methoddesc}[Node]{hasChildNodes}{} Returns true if the node has any child nodes. \end{methoddesc} \begin{methoddesc}[Node]{isSameNode}{other} Returns true if \var{other} refers to the same node as this node. 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). \begin{notice} This is based on a proposed DOM Level~3 API which is still in the ``working draft'' stage, but this particular interface appears uncontroversial. Changes from the W3C will not necessarily affect this method in the Python DOM interface (though any new W3C API for this would also be supported). \end{notice} \end{methoddesc} \begin{methoddesc}[Node]{appendChild}{newChild} Add a new child node to this node at the end of the list of children, returning \var{newChild}. \end{methoddesc} \begin{methoddesc}[Node]{insertBefore}{newChild, refChild} Insert a new child node before an existing child. It must be the case that \var{refChild} is a child of this node; if not, \exception{ValueError} is raised. \var{newChild} is returned. If \var{refChild} is \code{None}, it inserts \var{newChild} at the end of the children's list. \end{methoddesc} \begin{methoddesc}[Node]{removeChild}{oldChild} Remove a child node. \var{oldChild} must be a child of this node; if not, \exception{ValueError} is raised. \var{oldChild} is returned on success. If \var{oldChild} will not be used further, its \method{unlink()} method should be called. \end{methoddesc} \begin{methoddesc}[Node]{replaceChild}{newChild, oldChild} Replace an existing node with a new node. It must be the case that \var{oldChild} is a child of this node; if not, \exception{ValueError} is raised. \end{methoddesc} \begin{methoddesc}[Node]{normalize}{} Join adjacent text nodes so that all stretches of text are stored as single \class{Text} instances. This simplifies processing text from a DOM tree for many applications. \versionadded{2.1} \end{methoddesc} \begin{methoddesc}[Node]{cloneNode}{deep} Clone this node. Setting \var{deep} means to clone all child nodes as well. This returns the clone. \end{methoddesc} \subsubsection{NodeList Objects \label{dom-nodelist-objects}} A \class{NodeList} represents a sequence of nodes. These objects are used in two ways in the DOM Core recommendation: the \class{Element} objects provides one as its list of child nodes, and the \method{getElementsByTagName()} and \method{getElementsByTagNameNS()} methods of \class{Node} return objects with this interface to represent query results. The DOM Level~2 recommendation defines one method and one attribute for these objects: \begin{methoddesc}[NodeList]{item}{i} Return the \var{i}'th item from the sequence, if there is one, or \code{None}. The index \var{i} is not allowed to be less then zero or greater than or equal to the length of the sequence. \end{methoddesc} \begin{memberdesc}[NodeList]{length} The number of nodes in the sequence. \end{memberdesc} In addition, the Python DOM interface requires that some additional support is provided to allow \class{NodeList} objects to be used as Python sequences. All \class{NodeList} implementations must include support for \method{__len__()} and \method{__getitem__()}; this allows iteration over the \class{NodeList} in \keyword{for} statements and proper support for the \function{len()} built-in function. If a DOM implementation supports modification of the document, the \class{NodeList} implementation must also support the \method{__setitem__()} and \method{__delitem__()} methods. \subsubsection{DocumentType Objects \label{dom-documenttype-objects}} Information about the notations and entities declared by a document (including the external subset if the parser uses it and can provide the information) is available from a \class{DocumentType} object. The \class{DocumentType} for a document is available from the \class{Document} object's \member{doctype} attribute; if there is no \code{DOCTYPE} declaration for the document, the document's \member{doctype} attribute will be set to \code{None} instead of an instance of this interface. \class{DocumentType} is a specialization of \class{Node}, and adds the following attributes: \begin{memberdesc}[DocumentType]{publicId} The public identifier for the external subset of the document type definition. This will be a string or \code{None}. \end{memberdesc} \begin{memberdesc}[DocumentType]{systemId} The system identifier for the external subset of the document type definition. This will be a URI as a string, or \code{None}. \end{memberdesc} \begin{memberdesc}[DocumentType]{internalSubset} A string giving the complete internal subset from the document. This does not include the brackets which enclose the subset. If the document has no internal subset, this should be \code{None}. \end{memberdesc} \begin{memberdesc}[DocumentType]{name} The name of the root element as given in the \code{DOCTYPE} declaration, if present. \end{memberdesc} \begin{memberdesc}[DocumentType]{entities} This is a \class{NamedNodeMap} giving the definitions of external entities. For entity names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). This may be \code{None} if the information is not provided by the parser, or if no entities are defined. \end{memberdesc} \begin{memberdesc}[DocumentType]{notations} This is a \class{NamedNodeMap} giving the definitions of notations. For notation names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). This may be \code{None} if the information is not provided by the parser, or if no notations are defined. \end{memberdesc} \subsubsection{Document Objects \label{dom-document-objects}} A \class{Document} represents an entire XML document, including its constituent elements, attributes, processing instructions, comments etc. Remeber that it inherits properties from \class{Node}. \begin{memberdesc}[Document]{documentElement} The one and only root element of the document. \end{memberdesc} \begin{methoddesc}[Document]{createElement}{tagName} Create and return a new element node. The element is not inserted into the document when it is created. You need to explicitly insert it with one of the other methods such as \method{insertBefore()} or \method{appendChild()}. \end{methoddesc} \begin{methoddesc}[Document]{createElementNS}{namespaceURI, tagName} Create and return a new element with a namespace. The \var{tagName} may have a prefix. The element is not inserted into the document when it is created. You need to explicitly insert it with one of the other methods such as \method{insertBefore()} or \method{appendChild()}.