xml.dom --- Document 物件模型 API¶
The Document Object Model, or "DOM," is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents. A DOM implementation presents an XML document as a tree structure, or allows client code to build such a structure from scratch. It then gives access to the structure through a set of objects which provided well-known interfaces.
The DOM is extremely useful for random-access applications. SAX only allows you a view of one bit of the document at a time. If you are looking at one SAX element, you have no access to another. If you are looking at a text node, you have no access to a containing element. When you write a SAX application, you need to keep track of your program's position in the document somewhere in your own code. SAX does not do it for you. Also, if you need to look ahead in the XML document, you are just out of luck.
Some applications are simply impossible in an event driven model with no access to a tree. Of course you could build some sort of tree yourself in SAX events, but the DOM allows you to avoid writing that code. The DOM is a standard tree representation for XML data.
The Document Object Model is being defined by the W3C in stages, or "levels" in their terminology. The Python mapping of the API is substantially based on the DOM Level 2 recommendation.
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 DOMImplementation object class which
provides access to 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
Document object. In Python, each DOM implementation will provide a
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 Conformance for a detailed discussion of mapping requirements.
也參考
- Document Object Model (DOM) Level 2 Specification
The W3C recommendation upon which the Python DOM API is based.
- Document Object Model (DOM) Level 1 Specification
The W3C recommendation for the DOM supported by
xml.dom.minidom.- Python Language Mapping Specification
This specifies the mapping from OMG IDL to Python.
模組內容¶
The xml.dom contains the following functions:
- xml.dom.registerDOMImplementation(name, factory)¶
Register the factory function with the name name. The factory function should return an object which implements the
DOMImplementationinterface. 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).
- xml.dom.getDOMImplementation(name=None, features=())¶
Return a suitable DOM implementation. The name is either well-known, the module name of a DOM implementation, or
None. If it is notNone, imports the corresponding module and returns aDOMImplementationobject if the import succeeds. If no name is given, and if the environment variablePYTHON_DOMis 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
ImportError. The features list must be a sequence of(feature, version)pairs which are passed to thehasFeature()method on availableDOMImplementationobjects.
Some convenience constants are also provided:
- xml.dom.EMPTY_NAMESPACE¶
The value used to indicate that no namespace is associated with a node in the DOM. This is typically found as the
namespaceURIof a node, or used as the namespaceURI parameter to a namespaces-specific method.
- xml.dom.XML_NAMESPACE¶
The namespace URI associated with the reserved prefix
xml, as defined by Namespaces in XML (section 4).
- xml.dom.XMLNS_NAMESPACE¶
The namespace URI for namespace declarations, as defined by Document Object Model (DOM) Level 2 Core Specification (section 1.1.8).
- xml.dom.XHTML_NAMESPACE¶
The URI of the XHTML namespace as defined by XHTML 1.0: The Extensible HyperText Markup Language (section 3.1.1).
In addition, xml.dom contains a base Node class and the DOM
exception classes. The 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 Node class
provided as part of this module does provide the constants used for the
nodeType attribute on concrete Node objects; they are located
within the class rather than at the module level to conform with the DOM
specifications.
Objects in the DOM¶
The definitive documentation for the DOM is the DOM specification from the W3C.
Note that DOM attributes may also be manipulated as nodes instead of as simple strings. It is fairly rare that you must do this, however, so this usage is not yet documented.
Interface |
Section |
Purpose |
|---|---|---|
|
Interface to the underlying implementation. |
|
|
Base interface for most objects in a document. |
|
|
Interface for a sequence of nodes. |
|
|
Information about the declarations needed to process a document. |
|
|
Object which represents an entire document. |
|
|
Element nodes in the document hierarchy. |
|
|
Attribute value nodes on element nodes. |
|
|
Representation of comments in the source document. |
|
|
Nodes containing textual content from the document. |
|
|
Processing instruction representation. |
An additional section describes the exceptions defined for working with the DOM in Python.
DOMImplementation 物件¶
The 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 Document and
DocumentType objects using the DOMImplementation as well.
- DOMImplementation.hasFeature(feature, version)¶
Return
Trueif the feature identified by the pair of strings feature and version is implemented.
- DOMImplementation.createDocument(namespaceUri, qualifiedName, doctype)¶
Return a new
Documentobject (the root of the DOM), with a childElementobject having the given namespaceUri and qualifiedName. The doctype must be aDocumentTypeobject created bycreateDocumentType(), orNone. In the Python DOM API, the first two arguments can also beNonein order to indicate that noElementchild is to be created.
- DOMImplementation.createDocumentType(qualifiedName, publicId, systemId)¶
Return a new
DocumentTypeobject that encapsulates the given qualifiedName, publicId, and systemId strings, representing the information contained in an XML document type declaration.
Node Objects¶
All of the components of an XML document are subclasses of Node.
- Node.nodeType¶
An integer representing the node type. Symbolic constants for the types are on the
Nodeobject:ELEMENT_NODE,ATTRIBUTE_NODE,TEXT_NODE,CDATA_SECTION_NODE,ENTITY_NODE,PROCESSING_INSTRUCTION_NODE,COMMENT_NODE,DOCUMENT_NODE,DOCUMENT_TYPE_NODE,NOTATION_NODE. This is a read-only attribute.
- Node.parentNode¶
The parent of the current node, or
Nonefor the document node. The value is always aNodeobject orNone. ForElementnodes, this will be the parent element, except for the root element, in which case it will be theDocumentobject. ForAttrnodes, this is alwaysNone. This is a read-only attribute.
- Node.attributes¶
A
NamedNodeMapof attribute objects. Only elements have actual values for this; others provideNonefor this attribute. This is a read-only attribute.
- Node.previousSibling¶
The node that immediately precedes this one with the same parent. For instance the element with an end-tag that comes just before the self element's start-tag. Of course, XML documents are made up of more than just elements so the previous sibling could be text, a comment, or something else. If this node is the first child of the parent, this attribute will be
None. This is a read-only attribute.
- Node.nextSibling¶
The node that immediately follows this one with the same parent. See also
previousSibling. If this is the last child of the parent, this attribute will beNone. This is a read-only attribute.
- Node.childNodes¶
A list of nodes contained within this node. This is a read-only attribute.
- Node.firstChild¶
The first child of the node, if there are any, or
None. This is a read-only attribute.
- Node.lastChild¶
The last child of the node, if there are any, or