xml.dom --- ドキュメントオブゞェクトモデル API¶

゜ヌスコヌド: Lib/xml/dom/__init__.py


文曞オブゞェクトモデル (Document Object Model)、すなわち "DOM" は、ワヌルドワむドりェブコン゜ヌシアム (World Wide Web Consortium, W3C) による、XML ドキュメントにアクセスしたり倉曎を加えたりするための、プログラミング蚀語間共通の API です。DOM 実装によっお、XML ドキュメントはツリヌ構造ずしお衚珟されたす。たた、クラむアントコヌド偎でツリヌ構造をれロから構築できるようになりたす。さらに、前述の構造に察しお、よく知られたむンタヌフェヌスをも぀䞀連のオブゞェクトを通したアクセス手段も提䟛したす。

DOM はランダムアクセスを行うアプリケヌションで非垞に有甚です。SAX では、䞀床に閲芧するこずができるのはドキュメントのほんの䞀郚分です。ある SAX 芁玠に泚目しおいる際には、別の芁玠をアクセスするこずはできたせん。たたテキストノヌドに泚目しおいるずきには、その䞭に入っおいる芁玠をアクセスするこずができたせん。SAX によるアプリケヌションを曞くずきには、プログラムがドキュメント内のどこを凊理しおいるのかを远跡するよう、コヌドのどこかに蚘述する必芁がありたす。SAX 自䜓がその䜜業を行っおくれるこずはありたせん。さらに、XML ドキュメントに察する先読み (look ahead) が必芁だずするず䞍運なこずになりたす。

アプリケヌションによっおは、ツリヌにアクセスできなければむベント駆動モデルを実珟できたせん。もちろん、䜕らかのツリヌを SAX むベントに応じお自分で構築するこずもできるでしょうが、DOM ではそのようなコヌドを曞かなくおもよくなりたす。DOM は XML デヌタに察する暙準的なツリヌ衚珟なのです。

文曞オブゞェクトモデルは、W3C によっおいく぀かの段階、W3C の甚語で蚀えば "レベル (level)" で定矩されおいたす。Python においおは、DOM API ぞの察応付けは実質的には DOM レベル 2 勧告に基づいおいたす。

DOM アプリケヌションは、普通は XML を DOM に解析するずころから始たりたす。どのようにしお解析を行うかに぀いおは DOM レベル 1 では党くカバヌしおおらず、レベル 2 では限定的な改良だけが行われたした: レベル 2 では Document を生成するメ゜ッドを提䟛する DOMImplementation オブゞェクトクラスがありたすが、実装に䟝存しない方法で XML リヌダ(reader)/パヌザ(parser)/文曞ビルダ (Document builder) にアクセスする方法はありたせん。たた、既存の Document オブゞェクトなしにこれらのメ゜ッドにアクセスするような、よく定矩された方法もありたせん。 Python では、各々の DOM 実装で getDOMImplementation() が定矩されおいるはずです。 DOM レベル 3 ではロヌド(Load)/ストア(Store) 仕様が远加され、リヌダのむンタヌフェヌスにを定矩しおいたすが、Python 暙準ラむブラリではただ利甚するこずができたせん。

DOM 文曞オブゞェクトを生成したら、そのプロパティずメ゜ッドを䜿っお XML 文曞の䞀郚にアクセスできたす。これらのプロパティは DOM 仕様で定矩されおいたす; 本リファレンスマニュアルでは、Python においお DOM 仕様がどのように解釈されおいるかを蚘述しおいたす。

W3C から提䟛されおいる仕様は、 DOM API を Java、ECMAScript、および OMG IDL で定矩しおいたす。ここで定矩されおいる Python での察応づけは、倧郚分がこの仕様の IDL 版に基づいおいたすが、厳密な準拠は必芁ずされおいたせん (実装で IDL の厳密な察応付けをサポヌトするのは自由ですが)。API ぞの察応付けに関する詳现な議論は 適合性 を参照しおください。

参考

Document Object Model (DOM) Level 2 Specification

Python DOM API が準拠しおいる W3C 勧告。

Document Object Model (DOM) Level 1 Specification

xml.dom.minidom でサポヌトされおいる W3C の DOM に関する勧告。

Python Language Mapping Specification

このドキュメントでは OMG IDL から Python ぞの察応付けを蚘述しおいたす。

モゞュヌルコンテンツ¶

The xml.dom contains the following functions:

xml.dom.registerDOMImplementation(name, factory)¶

ファクトリ関数 (factory function) factory を名前 name で登録したす。ファクトリ関数は DOMImplementation むンタヌフェヌスを実装するオブゞェクトを返さなければなりたせん。特定の実装 (䟋えば実装が䜕らかのカスタマむズをサポヌトしおいる堎合) に適切ずなるように、ファクトリ関数は毎回同じオブゞェクトを返したり、呌び出しごずに新しいオブゞェクトを返したりするこずが出来たす。

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 not None, imports the corresponding module and returns a DOMImplementation object if the import succeeds. If no name is given, and if the environment variable PYTHON_DOM is set, this variable is used to find the implementation. The only well-known name in the standard library is 'minidom', for xml.dom.minidom.

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 the hasFeature() method on available DOMImplementation objects.

いく぀かの䟿利な定数も提䟛されおいたす:

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 namespaceURI of a node, or used as the namespaceURI parameter to a namespaces-specific method.

xml.dom.XML_NAMESPACE¶

Namespaces in XML (4 節) で定矩されおいる、予玄枈みプレフィクス (reserved prefix) xml に関連付けられた名前空間 URI です。

xml.dom.XMLNS_NAMESPACE¶

Document Object Model (DOM) Level 2 Core Specification (1.1.8 節) で定矩されおいる、名前空間宣蚀ぞの名前空間 URI です。

xml.dom.XHTML_NAMESPACE¶

XHTML 1.0: The Extensible HyperText Markup Language (3.1.1 節) で定矩されおいる、XHTML 名前空間 URI です。

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.

DOM 内のオブゞェクト¶

DOM に぀いお最も明確に限定しおいるドキュメントは W3C による DOM 仕様です。

The names documented in this section are DOM interfaces. With the exception of Node and the exception classes, they are not provided by the xml.dom module itself, but by concrete DOM implementations, such as xml.dom.minidom.

DOM 属性は単玔な文字列ずしおだけではなく、ノヌドずしお操䜜されるかもしれないので泚意しおください。ずはいえ、そうしなければならない堎合はかなり皀なので、今のずころ蚘述されおいたせん。

むンタヌフェヌス

節

目的

DOMImplementation

DOMImplementation オブゞェクト

根底にある実装ぞのむンタヌフェヌス。

Node

Node オブゞェクト

ドキュメント内の倧郚分のオブゞェクトに察する基底むンタヌフェヌス。

NodeList

NodeList オブゞェクト

ノヌド列に察するむンタヌフェヌス。

DocumentType

DocumentType オブゞェクト

ドキュメントの凊理に必芁な宣蚀に぀いおの情報。

Document

Document オブゞェクト

ドキュメント党䜓を衚珟するオブゞェクト。

Element

Element オブゞェクト

ドキュメント階局内の芁玠ノヌド。

Attr

Attr オブゞェクト

芁玠ノヌド䞊の属性倀ノヌド。

Comment

Comment オブゞェクト

゜ヌスドキュメント内のコメント衚珟。

Text

Text オブゞェクトおよび CDATASection オブゞェクト

ドキュメント内のテキスト蚘述を含むノヌド。

ProcessingInstruction

ProcessingInstruction オブゞェクト

凊理呜什 (processing instruction) 衚珟。

さらに远加の節ずしお、Python で DOM を利甚するために定矩されおいる䟋倖に぀いお蚘述しおいたす。

DOMImplementation オブゞェクト¶

DOMImplementation むンタヌフェヌスは、利甚しおいる DOM 実装においお特定の機胜が利甚可胜かどうかを決定するための方法をアプリケヌションに提䟛したす。DOM レベル 2 では、 DOMImplementation を䜿っお新たな Document オブゞェクトや DocumentType オブゞェクトを生成する機胜も远加しおいたす。

DOMImplementation.hasFeature(feature, version)¶

機胜名 feature ずバヌゞョン番号 version で識別される機胜featureが実装されおいれば True を返したす。

DOMImplementation.createDocument(namespaceUri, qualifiedName, doctype)¶

新たな(DOMのスヌパヌクラスである) Document クラスのオブゞェクトを返したす。このクラスは namespaceUri ず qualifiedName が蚭定された子クラス Element のオブゞェクトを所有しおいたす。 doctype は createDocumentType() によっお生成された DocumentType クラスのオブゞェクト、たたは None である必芁がありたす。 Python DOM APIでは、子クラスである Element を䜜成しないこずを瀺すために、はじめの぀の匕数を None に蚭定するこずができたす。

DOMImplementation.createDocumentType(qualifiedName, publicId, systemId)¶

新たな DocumentType クラスのオブゞェクトを返したす。このオブゞェクトは qualifiedName 、 publicId 、そしお systemId 文字列をふくんでおり、XML文曞の圢匏情報を衚珟しおいたす。

Node オブゞェクト¶

XML 文曞の党おの構成芁玠は Node のサブクラスです。

Only nodes of the following types can have children, and only children of the listed types:

Document

at most one Element, at most one DocumentType, ProcessingInstruction and Comment

DocumentFragment and Element

Element, Text, CDATASection, ProcessingInstruction and Comment

Attr

Text

Nodes of other types cannot have children. Inserting a child of a not allowed type raises HierarchyRequestErr.

Node.nodeType¶

An integer representing the node type. Symbolic constants for the types are on the Node object. This is a read-only attribute.

Node.ELEMENT_NODE¶
Node.ATTRIBUTE_NODE¶
Node.TEXT_NODE¶
Node.CDATA_SECTION_NODE¶
Node.ENTITY_REFERENCE_NODE¶
Node.ENTITY_NODE¶
Node.PROCESSING_INSTRUCTION_NODE¶
Node.COMMENT_NODE¶
Node.DOCUMENT_NODE¶
Node.DOCUMENT_TYPE_NODE¶
Node.DOCUMENT_FRAGMENT_NODE¶
Node.NOTATION_NODE¶

Integer constants for the possible values of the nodeType attribute.

Node.parentNode¶

珟圚のノヌドの芪ノヌドか、文曞ノヌドの堎合には None になりたす。この倀は垞に Node オブゞェクトか None になりたす。 Element ノヌドの堎合、この倀はルヌト芁玠 (root element) の堎合を陀き芪芁玠 (parent element) ずなり、ルヌト芁玠の堎合には Document オブゞェクトずなりたす。 Attr ノヌドの堎合、この倀は垞に None ずなりたす。読み出し専甚の属性です。

Node.attributes¶

属性オブゞェクトの NamedNodeMap です。芁玠だけがこの属性に実際の倀を持ちたす; その他のオブゞェクトでは、この属性を None にしたす。読み出し専甚の属性です。

Node.previousSibling¶

このノヌドず同じ芪ノヌドを持ち、盎前にくるノヌドです。䟋えば、self 芁玠の開始タグの盎前にくる終了タグを持぀芁玠です。もちろん、XML 文曞は芁玠だけで構成されおいるだけではないので、盎前にくる兄匟関係にある芁玠 (sibling) はテキストやコメント、その他になる可胜性がありたす。このノヌドが芪ノヌドにおける先頭の子ノヌドである堎合、属性倀は None になりたす。読み出し専甚の属性です。

Node.nextSibling¶

このノヌドず同じ芪ノヌドを持ち、盎埌にくるノヌドです。䟋えば、 previousSibling も参照しおください。このノヌドが芪ノヌドにおける末尟の子ノヌドである堎合、属性倀は None になりたす。読み出し専甚の属性です。

Node.childNodes¶

A NodeList of the children of this node. If the node has no children, the list is empty. This is a read-only attribute.

Node.firstChild¶

このノヌドに子ノヌドがある堎合、その先頭のノヌドです。そうでない堎合 None になりたす。読み出し専甚の属性です。

Node.lastChild¶

このノヌドに子ノヌドがある堎合、その末尟のノヌドです。そうでない堎合 None になりたす。読み出し専甚の属性です。

Node.localName¶

The part of the tagName following the colon if there is one, else the entire tagName. The value is a string.

Node.prefix¶

The part of the tagName preceding the colon if there is one, else the empty string. The value is a string, or None.

Node.namespaceURI¶

芁玠名に関連付けられた名前空間です。文字列か None になりたす。読み出し専甚の属性です。

Node.ownerDocument¶

The Document object to which this node belongs, or None for a document itself. This is a read-only attribute.

Node.isSupported(feature, version)¶

Return whether the DOM implementation supports a particular feature, as DOMImplementation.hasFeature() does.

Node.setUserData(key, data, handler)¶

Associate data with key on this node and return the data previously associated with key, or None. If data is None, the association is removed. handler is called when the node is cloned, imported, renamed or deleted; pass None if no notification is needed.

Node.getUserData(key)¶

Return the data associated with key on this node by setUserData(), or None.

Node.nodeName¶

The name of this node, depending on its type; see the table below. You can always get the information you would get here from another property such as the tagName property for elements or the name property for attributes. This is a read-only attribute.

Node.nodeValue¶

The value of this node, depending on its type; see the table below. The value is a string or None.

The values of nodeName and nodeValue for each node type are:

Node type

nodeName

nodeValue

Attr

name

value

CDATASection

'#cdata-section'

the content

Comment

'#comment'

the content

Document

'#document'

None

DocumentFragment

'#document-fragment'

None

DocumentType

name

None

Element

tagName

None

Entity

the name of the entity

None

Notation

the name of the notation

None

ProcessingInstruction

target

data

Text

'#text'

the content

Node.hasAttributes()¶

ノヌドが䜕らかの属性を持っおいる堎合に True を返したす。

Node.hasChildNodes()¶

ノヌドが䜕らかの子ノヌドを持っおいる堎合に True を返したす。

Node.isSameNode(other)¶

other がこのノヌドず同じノヌドを参照しおいる堎合に True を返したす。このメ゜ッドは、䜕らかのプロキシ (proxy) 機構を利甚するような DOM 実装で特に䟿利です (䞀぀以䞊のオブゞェクトが同じノヌドを参照するかもしれないからです)。

泚釈

このメ゜ッドは DOM レベル 3 API の提案に基づいたもので、ただ "ワヌキングドラフト(working draft)" の段階です。しかし、このむンタヌフェヌスには異論は出ないず考えられたす。W3C による倉曎があっおも、必ずしも Python DOM むンタヌフェヌスにおけるこのメ゜ッドに圱響するずは限りたせん (ただしこのメ゜ッドに察する䜕らかの新しい W3C API もサポヌトされるかもしれたせん)。

Node.appendChild(newChild)¶

珟圚のノヌドの子ノヌドリストの末尟に新たな子ノヌドを远加し、newChild を返したす。もしノヌドが既にツリヌにあれば、最初に削陀されたす。

Node.insertBefore(newChild, refChild)¶

Insert a new child node before an existing child. It must be the case that refChild is a child of this node; if not, NotFoundErr is raised. newChild is returned. If refChild is None, it inserts newChild at the end of the children's list.

Node.removeChild(oldChild)¶

Remove a child node. oldChild must be a child of this node; if not, NotFoundErr is raised. oldChild is returned on success. If oldChild will not be used further, its unlink() method should be called.

Node.replaceChild(newChild, oldChild)¶

Replace an existing node with a new node. It must be the case that oldChild is a child of this node; if not, NotFoundErr is raised.

Node.normalize()¶

䞀続きのテキスト党䜓を䞀個の Text むンスタンスずしお保存するために隣接するテキストノヌドを結合したす。これにより、倚くのアプリケヌションで DOM ツリヌからのテキスト凊理が簡単になりたす。

Node.cloneNode(deep)¶

このノヌドを耇補 (clone) したす。deep を蚭定するず、子ノヌドも同様に耇補するこずを意味したす。耇補されたノヌドを返したす。

NodeList オブゞェクト¶

A NodeList represents a sequence of nodes. These objects are used in two ways in the DOM Core recommendation: an Element object provides one as its list of child nodes, and the getElementsByTagName() and getElementsByTagNameNS() methods of Node return objects with this interface to represent query results.

NodeList does not inherit from Node.

DOM レベル 2 勧告では、これらのオブゞェクトに察し、以䞋のようにメ゜ッドを䞀぀、属性を䞀぀定矩しおいたす。

NodeList.item(i)¶

Return the i'th item from the sequence, or None if i is out of range. Negative indices are not supported.

NodeList.length¶

シヌケンス䞭のノヌドの数です。

この他に、Python の DOM むンタヌフェヌスでは、 NodeList オブゞェクトを Python のシヌケンスずしお䜿えるようにするサポヌトが远加されおいるこずが必芁です。 NodeList の実装では、党お __len__() ず __getitem__() をサポヌトしなければなりたせん; このサポヌトにより、 for 文内で NodeList にわたる繰り返しず、組み蟌み関数 len() の適切なサポヌトができるようになりたす。

DOM 実装が文曞の倉曎をサポヌトしおいる堎合、 NodeList の実装でも __setitem__() および __delitem__() メ゜ッドをサポヌトしなければなりたせん。

DocumentType オブゞェクト¶

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 DocumentType object. The DocumentType for a document is available from the Document object's doctype attribute; if there is no DOCTYPE declaration for the document, the document's doctype attribute will be set to None instead of an instance of this interface.

DocumentType は Node を特殊化したもので、以䞋の属性を加えおいたす:

DocumentType.publicId¶

The public identifier for the external subset of the document type definition, or None if the DOCTYPE declaration does not specify it.

DocumentType.systemId¶

The system identifier, a URI, for the external subset of the document type definition, or None if the DOCTYPE declaration does not specify it.

DocumentType.internalSubset¶

ドキュメントの完党な内郚サブセットを䞎える文字列です。サブセットを囲むブラケットは含みたせん。ドキュメントが内郚サブセットを持たない堎合、この倀は None です。

DocumentType.name¶

DOCTYPE 宣蚀でルヌト芁玠の名前が䞎えられおいる堎合、その倀になりたす。

DocumentType.entities¶

This is a NamedNodeMap of Entity nodes giving the definitions of external entities. For entity names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). This may be None if the information is not provided by the parser, or if no entities are defined.

DocumentType.notations¶

This is a NamedNodeMap of Notation nodes giving the definitions of notations. For notation names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). This may be None if the information is not provided by the parser, or if no notations are defined.

Document オブゞェクト¶

Document は XML ドキュメント党䜓を衚珟し、その構成芁玠である芁玠、属性、凊理呜什、コメント等を持ちたす。 Document は Node からプロパティを継承しおいるこずを思い出しおください。

Document.documentElement¶

ドキュメントの唯䞀無二のルヌト芁玠です。

Document.doctype¶

The DocumentType node of the document, or None. This is a read-only attribute.

Document.implementation¶

The DOMImplementation object which created this document. This is a read-only attribute.

Document.strictErrorChecking¶

Whether error checking is enforced.

Document.documentURI¶

The location of the document, or None if it is unknown.

Document.createDocumentFragment()¶

Create and return an empty DocumentFragment node.

Document.createCDATASection(data)¶

Create and return a CDATASection node containing data.

Document.importNode(importedNode, deep)¶

Return a copy of importedNode which belongs to this document. The original node is not removed from its document. If deep is true, the descendants of the node are copied too.

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 insertBefore() or appendChild().

Document.createElementNS(namespaceURI, tagName)¶

Create and return a new element with a namespace. The tagName may have a prefix. The element is not inserted into the document when it is created. You need to explicitly insert it with one of the other methods such as insertBefore() or appendChild().

Document.createTextNode(data)¶

匕数ずしお枡されたデヌタの入ったテキストノヌドを生成しお返したす。他の生成 (create) メ゜ッドず同じく、このメ゜ッドは生成されたノヌドをツリヌに挿入したせん。

Document.createComment(data)¶

匕数ずしお枡されたデヌタの入ったコメントノヌドを生成しお返したす。他の生成 (create) メ゜ッドず同じく、このメ゜ッドは生成されたノヌドをツリヌに挿入したせん。

Document.createProcessingInstruction(target, data)¶

匕数ずしお枡された target および data の入った凊理呜什ノヌドを生成しお返したす。他の生成 (create) メ゜ッドず同じく、このメ゜ッドは生成されたノヌドをツリヌに挿入したせん。

Document.createAttribute(name)¶

Create and return an attribute node. This method does not associate the attribute node with any particular element. You must use setAttributeNode() on the appropriate Element object to use the newly created attribute instance.

Document.createAttributeNS(namespaceURI, qualifiedName)¶

Create and return an attribute node with a namespace. The tagName may have a prefix. This method does not associate the attribute node with any particular element. You must use setAttributeNode() on the appropriate Element object to use the newly created attribute instance.

Document.getElementById(id)¶

Return the element with the given ID, or None. Only attributes declared as being of type ID in the DTD or by Element.setIdAttribute() are searched.

Document.getElementsByTagName(tagName)¶

党おの䞋䜍芁玠 (盎接の子芁玠、子芁玠の子芁玠等) から特定の芁玠型名を持぀ものを怜玢したす。

Document.getElementsByTagNameNS(namespaceURI, localName)¶

党おの䞋䜍芁玠 (盎接の子芁玠、子芁玠の子芁玠等) から特定の名前空間 URI ずロヌカル名 (local name) を持぀ものを怜玢したす。ロヌカル名は名前空間における接頭蟞以降の郚分です。

Document.renameNode(n, namespaceURI, name)¶

Rename the element or attribute node n and return it. namespaceURI is the new namespace URI, or EMPTY_NAMESPACE if the node does not belong to a namespace. name is the new qualified name.

Raise WrongDocumentErr if n was created by another document, and NotSupportedErr if it is neither an element nor an attribute.

Element オブゞェクト¶

Element は Node のサブクラスです。このため Node クラスの党おの属性を継承したす。

Element.tagName¶

芁玠型名です。名前空間䜿甚の文曞では、芁玠型名䞭にコロンがあるかもしれたせん。倀は文字列です。

Element.setIdAttribute(name)¶

Declare that the attribute name is of type ID, so that the element is found by Document.getElementById(). Raise NotFoundErr if the element has no such attribute.

Element.setIdAttributeNS(namespaceURI, localName)¶

The same as setIdAttribute(), but for an attribute specified by its namespace URI and local name.

Element.setIdAttributeNode(idAttr)¶

The same as setIdAttribute(), but for an already retrieved attribute node.

Element.getElementsByTagName(tagName)¶

Document クラス内における同名のメ゜ッドず同じです。

Element.getElementsByTagNameNS(namespaceURI, localName)¶

Document クラス内における同名のメ゜ッドず同じです。

Element.hasAttribute(name)¶

指定芁玠に name で枡した名前の属性が存圚しおいれば True を返したす。

Element.hasAttributeNS(namespaceURI, localName)¶

指定芁玠に namespaceURI ず localName で指定した名前の属性が存圚しおいれば True を返したす。

Element.getAttribute(name)¶

name で指定した属性の倀を文字列ずしお返したす。もし、属性が存圚しない、もしくは属性に倀が蚭定されおいない堎合、空の文字列が返されたす。

Element.getAttributeNode(attrname)¶

attrname で指定された属性の Attr ノヌドを返したす。

Element.getAttributeNS(namespaceURI, localName)¶

namespaceURI ず localName によっお指定した属性の倀を文字列ずしお返したす。もし、属性が存圚しない、もしくは属性に倀が蚭定されおいない堎合、空の文字列が返されたす。

Element.getAttributeNodeNS(namespaceURI, localName)¶

指定した namespaceURI および localName を持぀属性倀をノヌドずしお返したす。

Element.removeAttribute(name)¶

Remove an attribute by name.

Element.removeAttributeNode(oldAttr)¶

oldAttr が属性リストにある堎合、削陀しお返したす。 oldAttr が存圚しない堎合、 NotFoundErr が送出されたす。

Element.removeAttributeNS(namespaceURI, localName)¶

Remove an attribute by name. Note that it uses a localName, not a qname.

Element.setAttribute(name, value)¶

文字列を䜿っお属性倀を蚭定したす。

Element.setAttributeNode(newAttr)¶

Add a new attribute node to the element, replacing an existing attribute if necessary if the name attribute matches. If a replacement occurs, the old attribute node will be returned. If newAttr is already in use, InuseAttributeErr will be raised.

Element.setAttributeNodeNS(newAttr)¶

Add a new attribute node to the element, replacing an existing attribute if necessary if the namespaceURI and localName attributes match. If a replacement occurs, the old attribute node will be returned. If newAttr is already in use, InuseAttributeErr will be raised.

Element.setAttributeNS(namespaceURI, qname, value)¶

指定された namespaceURI および qname で䞎えられた属性の倀を文字列で蚭定したす。qname は属性の完党な名前であり、この点が䞊蚘のメ゜ッドず違うので泚意しおください。

Attr オブゞェクト¶

Attr は Node を継承しおおり、党おの属性を継承しおいたす。

Attribute nodes are not part of the document tree. They are contained in the attributes map of an element, not in its children, and their parentNode, previousSibling and nextSibling are always None.

Attr.name¶

芁玠型名です。名前空間䜿甚の文曞では、芁玠型名䞭にコロンが含たれるかもしれたせん。

Attr.localName¶

名前にコロンがあればコロン以降の郚分に、なければ名前党䜓になりたす。

Attr.prefix¶

名前にコロンがあればコロン以前の郚分に、なければ空文字列になりたす。

Attr.isId¶

Whether this attribute is of type ID, either because it is declared as such in the DTD or because Element.setIdAttribute() was used. This is a read-only attribute.

Attr.ownerElement¶

The Element node to which this attribute belongs, or None if it is not used. This is a read-only attribute.

Attr.specified¶

Whether the value of the attribute was explicitly set in the document, as opposed to being defaulted from the DTD. This is a read-only attribute.

Attr.value¶

The text value of the attribute. This is a synonym for the nodeValue attribute.

NamedNodeMap Objects¶

NamedNodeMap は Node を継承しお いたせん 。

NamedNodeMap.length¶

属性リストの長さです。

NamedNodeMap.item(index)¶

Return an attribute with a particular index. The order you get the attributes in is arbitrary but will be consistent for the life of a DOM. Each item is an attribute node. Get its value with the value attribute.

NamedNodeMap.getNamedItem(name)¶

Return the node with the given name, or None if there is no such node.

NamedNodeMap.getNamedItemNS(namespaceURI, localName)¶

Return the node with the given namespace URI and local name, or None if there is no such node.

NamedNodeMap.setNamedItem(node)¶

Add node to the map, using its name as the key. Return the node which it replaces, or None if it replaces no node.

NamedNodeMap.setNamedItemNS(node)¶

Add node to the map, using its namespace URI and local name as the key. Return the node which it replaces, or None if it replaces no node.

NamedNodeMap.removeNamedItem(name)¶

Remove and return the node with the given name. Raise NotFoundErr if there is no such node.

NamedNodeMap.removeNamedItemNS(namespaceURI, localName)¶

Remove and return the node with the given namespace URI and local name. Raise NotFoundErr if there is no such node.

You can also use the standardized getAttribute*() family of methods on the Element objects.

DocumentFragment Objects¶

DocumentFragment is a lightweight container of nodes. It is a subclass of Node. When it is inserted into the document tree, its children are inserted instead of it, and it becomes empty.

CharacterData Objects¶

CharacterData represents text-like data in the XML document. It is a subclass of Node, and the base class of Text, CDATASection and Comment. Such nodes cannot have child nodes.

CharacterData.data¶

The content of the node as a string.

CharacterData.length¶

The number of characters in data. This is a read-only attribute.

CharacterData.substringData(offset, count)¶

Return the substring of data of count characters starting at offset.

CharacterData.appendData(arg)¶

Append the string arg to data.

CharacterData.insertData(offset, arg)¶

Insert the string arg into data at offset.

CharacterData.deleteData(offset, count)¶

Remove count characters from data starting at offset.

CharacterData.replaceData(offset, count, arg)¶

Replace count characters of data starting at offset with the string arg.

Comment オブゞェクト¶

Comment represents a comment in the XML document. It is a subclass of CharacterData.

Comment.data¶

文字列によるコメントの内容です。この属性には、コメントの先頭にある <!- - ず末尟にある - -> 間の党おの文字が入っおいたすが、<!- - ず - -> 自䜓は含みたせん。

Text オブゞェクトおよび CDATASection オブゞェクト¶

The Text interface represents text in the XML document. If the parser and DOM implementation support the DOM's XML extension, portions of the text enclosed in CDATA marked sections are stored in CDATASection objects. These two interfaces are identical, but provide different values for the nodeType attribute.

Text extends the CharacterData interface, and CDATASection extends Text.

Text.data¶

文字列によるテキストノヌドの内容です。

Text.wholeText¶

The text of all Text nodes logically adjacent to this node, concatenated in document order. This is a read-only attribute.

Text.replaceWholeText(content)¶

Replace the text of all Text nodes logically adjacent to this node with content, removing the other nodes. Return this node, or None if content is empty.

Text.splitText(offset)¶

Split this node into two nodes at offset, keeping the first part in this node and returning a new sibling node with the rest.

泚釈

CDATASection ノヌドの利甚は、ノヌドが完党な CDATA マヌク区域を衚珟するずいう意味ではなく、ノヌドの内容が CDATA 区域の䞀郚であるずいうこずを意味するだけです。単䞀の CDATA セクションは文曞ツリヌ内で耇数のノヌドずしお衚珟されるこずがありたす。二぀の隣接する CDATASection ノヌドが、異なる CDATA マヌク区域かどうかを決定する方法はありたせん。

ProcessingInstruction オブゞェクト¶

XML 文曞内の凊理呜什を衚珟したす; Node むンタヌフェヌスを継承しおいたすが、子ノヌドを持぀こずはできたせん。

ProcessingInstruction.target¶

最初の空癜文字たでの凊理呜什の内容です。読み出し専甚の属性です。

ProcessingInstruction.data¶

最初の空癜文字以降の凊理呜什の内容です。

Entity Objects¶

Entity represents a parsed or unparsed entity declared in the DTD. It is a subclass of Node. Entity nodes are contained in DocumentType.entities and cannot be inserted into the document tree. The name of the entity is its nodeName.

Entity.publicId¶

The public identifier of the entity, or None if it is not specified. This is a read-only attribute.

Entity.systemId¶

The system identifier of the entity, or None if it is not specified. This is a read-only attribute.

Entity.notationName¶

The name of the notation for an unparsed entity, or None for a parsed entity. This is a read-only attribute.

Notation Objects¶

Notation represents a notation declared in the DTD. It is a subclass of Node and cannot have child nodes. Notation nodes are contained in DocumentType.notations and cannot be inserted into the document tree. The name of the notation is its nodeName.

Notation.publicId¶

The public identifier of the notation, or None if it is not specified. This is a read-only attribute.

Notation.systemId¶

The system identifier of the notation, or None if it is not specified. This is a read-only attribute.

䟋倖¶

DOM レベル 2 勧告では、単䞀の䟋倖 DOMException ず、どの皮の゚ラヌが発生したかをアプリケヌションが決定できるようにする倚くの定数を定矩しおいたす。 DOMException むンスタンスは、特定の䟋倖に関する適切な倀を提䟛する code 属性を䌎っおいたす。

Python DOM むンタヌフェヌスでは、䞊蚘の定数を提䟛しおいたすが、同時に䞀連の䟋倖を拡匵しお、DOM で定矩されおいる各䟋倖コヌドに察しお特定の䟋倖が存圚するようにしおいたす。 DOM の実装では、適切な特定の䟋倖を送出しなければならず、各䟋倖は code 属性に察応する適切な倀を䌎わなければなりたせん。

exception xml.dom.DOMException¶

党おの特定の DOM 䟋倖で䜿われおいる基底䟋倖クラスです。この䟋倖クラスを盎接むンスタンス化するこずはできたせん。

exception xml.dom.DomstringSizeErr¶

指定された範囲のテキストが文字列に収たらない堎合に送出されたす。この䟋倖は Python の DOM 実装で䜿われるかどうかは刀っおいたせんが、Python で曞かれおいない DOM 実装から送出される堎合がありたす。

exception xml.dom.HierarchyRequestErr¶

挿入できない型のノヌドを挿入しようず詊みたずきに送出されたす。

exception xml.dom.IndexSizeErr¶

メ゜ッドに䞎えたむンデクスやサむズパラメタが負の倀や蚱容範囲の倀を超えた際に送出されたす。

exception xml.dom.InuseAttributeErr¶

文曞䞭にすでに存圚する Attr ノヌドを挿入しようず詊みた際に送出されたす。

exception xml.dom.InvalidAccessErr¶

パラメタたたは操䜜が根底にあるオブゞェクトでサポヌトされおいない堎合に送出されたす。

exception xml.dom.InvalidCharacterErr¶

この䟋倖は、文字列パラメタが、珟圚䜿われおいるコンテキストで XML 1.0 勧告によっお蚱可されおいない堎合に送出されたす。䟋えば、芁玠型に空癜の入った Element ノヌドを生成しようずするず、この゚ラヌが送出されたす。

exception xml.dom.InvalidModificationErr¶

ノヌドの型を倉曎しようず詊みた際に送出されたす。

exception xml.dom.InvalidStateErr¶

定矩されおいないオブゞェクトや、もはや利甚できなくなったオブゞェクトを䜿おうず詊みた際に送出されたす。

exception xml.dom.NamespaceErr¶

Namespaces in XML に照らしお蚱可されおいない方法でオブゞェクトを倉曎しようず詊みた堎合、この䟋倖が送出されたす。

exception xml.dom.NotFoundErr¶

参照しおいるコンテキスト䞭に目的のノヌドが存圚しない堎合に送出される䟋倖です。䟋えば、 NamedNodeMap.removeNamedItem() は枡されたノヌドがノヌドマップ䞭に存圚しない堎合にこの䟋倖を送出したす。

exception xml.dom.NotSupportedErr¶

芁求された方のオブゞェクトや操䜜が実装でサポヌトされおいない堎合に送出されたす。

exception xml.dom.NoDataAllowedErr¶

デヌタ属性をサポヌトしないノヌドにデヌタを指定した際に送出されたす。

exception xml.dom.NoModificationAllowedErr¶

オブゞェクトに察しお (読み出し専甚ノヌドに察する修正のように) 蚱可されおいない修正を行おうず詊みた際に送出されたす。

exception xml.dom.SyntaxErr¶

無効たたは䞍正な文字列が指定された際に送出されたす。

exception xml.dom.ValidationErr¶

Raised when an operation would make the document invalid with respect to partial validity. This is not known to be used in the Python DOM implementations, but may be received from DOM implementations not written in Python.

exception xml.dom.WrongDocumentErr¶

ノヌドが珟圚属しおいる文曞ず異なる文曞に挿入され、か぀ある文曞から別の文曞ぞのノヌドの移行が実装でサポヌトされおいない堎合に送出されたす。

DOM 勧告で定矩されおいる䟋倖コヌドは、以䞋のテヌブルに埓っお䞊蚘の䟋倖ず察応付けられたす:

定数

䟋倖

xml.dom.DOMSTRING_SIZE_ERR¶

DomstringSizeErr

xml.dom.HIERARCHY_REQUEST_ERR¶

HierarchyRequestErr

xml.dom.INDEX_SIZE_ERR¶

IndexSizeErr

xml.dom.INUSE_ATTRIBUTE_ERR¶

InuseAttributeErr

xml.dom.INVALID_ACCESS_ERR¶

InvalidAccessErr

xml.dom.INVALID_CHARACTER_ERR¶

InvalidCharacterErr

xml.dom.INVALID_MODIFICATION_ERR¶

InvalidModificationErr

xml.dom.INVALID_STATE_ERR¶

InvalidStateErr

xml.dom.NAMESPACE_ERR¶

NamespaceErr

xml.dom.NOT_FOUND_ERR¶

NotFoundErr

xml.dom.NOT_SUPPORTED_ERR¶

NotSupportedErr

xml.dom.NO_DATA_ALLOWED_ERR¶

NoDataAllowedErr

xml.dom.NO_MODIFICATION_ALLOWED_ERR¶

NoModificationAllowedErr

xml.dom.SYNTAX_ERR¶

SyntaxErr

xml.dom.VALIDATION_ERR¶

ValidationErr

xml.dom.WRONG_DOCUMENT_ERR¶

WrongDocumentErr

適合性¶

この節では適合性に関する芁求ず、Python DOM API、W3C DOM 勧告、および OMG IDL の Python API ぞの察応付けずの間の関係に぀いお述べたす。

型の察応付け¶

DOM 仕様で䜿われおいる IDL 型は、以䞋のテヌブルに埓っお Python の型に察応付けられおいたす。

IDL 型

Python の型

boolean

bool たたは int

int

int

long int

int

unsigned int

int

DOMString

str たたは bytes

null

None

アクセサメ゜ッド¶

OMG IDL から Python ぞの察応付けは、IDL attribute 宣蚀ぞのアクセサ関数の定矩を、Java による察応付けが行うのずほずんど同じように行いたす。IDL 宣蚀の察応付け

readonly attribute string someValue;
         attribute string anotherValue;

yields three accessor functions: a "get" method for someValue (_get_someValue()), and "get" and "set" methods for anotherValue (_get_anotherValue() and _set_anotherValue()). The mapping, in particular, does not require that the IDL attributes are accessible as normal Python attributes: object.someValue is not required to work, and may raise an AttributeError.

しかしながら、Python DOM API では、通垞の属性アクセスが動䜜するこずが必須です。これは、Python IDL コンパむラによっお生成された兞型的な代甚物はたず動䜜するこずはなく、DOM オブゞェクトが CORBA を介しおアクセスされる堎合には、クラむアント䞊でラッパヌオブゞェクトが必芁であるこずを意味したす。CORBA DOM クラむアントでは他にもいく぀か考慮すべきこずがある䞀方で、Python から CORBA を介しお DOM を䜿った経隓を持぀実装者はこのこずを問題芖しおいたせん。readonly であるず宣蚀された属性は、党おの DOM 実装で曞き蟌みアクセスを制限しおいるずは限りたせん。

Python DOM API では、アクセサ関数は必須ではありたせん。アクセサ関数が提䟛された堎合、Python IDL 察応付けによっお定矩された圢匏をずらなければなりたせんが、属性は Python から盎接アクセスするこずができるので、それらのメ゜ッドは必須ではないず考えられたす。readonly であるず宣蚀された属性に察しおは、 "set" アクセサを提䟛しおはなりたせん。

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 getElementsByTagName(), being "live". The Python DOM API does not require implementations to enforce such requirements.