7.2 xml.dom -- The Document Object Model 文档对象模式 API

New in version 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.

文档对象模式,或称 "DOM" 是来自互联网协会 (W3C) 的一种交叉语言,用以访问和修改XML文档. DOM 可以将XML文档处理成为一种树状的数据结构,或是从客户代码生成这种数据结构. 并且提供了一套易读的对象来访问这种数据结构.

7.1

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.

DOM 对于随机访问的应用非常有用. 而对于 SAX,则 只允许你同一时刻只能访问一小片XML文档, 这意味着: 如果你正在关注一个SAX元素,则你无法访问另一个. 如果你正在访问一个文本结点,则你无法访问其容器元素. 当你写SAX应用时,你得在代码中跟踪你程序在XML文档中的作用位置. SAX不能替你进行位置的记录. 所以,如果你需要随时回到前面处理,SAX将无能为力!

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.

事务驱动模式的应用使你几乎不可能访问树形结构. 当然你可以自个儿架构某种结构在SAX的事务过程中来存放树结构, 但是DOM 省却了你的这种代码. DOM 是XML数据的标准树表达方式!

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 Python XML Special Interest Group as part of the PyXML package. Refer to the documentation bundled with that package for information on the current state of DOM Level 3 support.

文档对象模式 在W3C 的术语体系中被划分为 “阶段”(stages)或是“阶层”(levels). Python 充分的映射了DOM 阶层2 推荐的所有特性. 阶层3 的规范当前Python的支持仅仅在草案阶段Python XML 特殊兴趣组 也成为 PyXML 包的一部分.

7.2

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.

典型的DOM应用由解析一些XML成为DOM 开始. 这在Python 已经完全封装了DOM 阶层1 至阶层2的规范,仅仅除了一点限制: DOMImplementation 对象类提供Document 创建方法, 但是没有独立执行的 XML 读取/解析/文档结构创建 的方法. Python中,每次DOM 的执行都需使用函式getDOMImplementation(). DOM 阶层3 增加了 Load/Store 规范, 可以定义一个实例来进行XML的读取, 但是,这还没有在Python 标准库中实现.

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.

一但你获得了DOM文档对象,你便可以通过其提供的属性和方法来访问XML的各个部分. 这些属性在DOM规范中进行了定义; 手册的这一部分描述 Python 的DOM 规范.

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 7.2.3, ``Conformance,'' for a detailed discussion of mapping requirements.

W3C定义的DOM API 规范有Java,ECMAScript, 和 OMG IDL 版本的实现. Python的映射主要基于 IDL 版本, 但是并没有严密的匹配(尽管允许从IDL精确映射执行). 参考7.2.3一节, 一致性``Conformance,'' 讨论详细的映射必要条件.

See Also:

Document Object Model (DOM) Level 2 Specification DOM 阶层2 规范
The W3C recommendation upon which the Python DOM API is based. W3C 推荐!Python DOM API 完全符合.
Document Object Model (DOM) Level 1 Specification DOM 阶层1 规范
The W3C recommendation for the DOM supported by W3C 建议至少要支持的,Python 由xml.dom.minidom提供映射.
PyXML
Users that require a full-featured implementation of DOM should use the PyXML package. 需要全部可执行特性的用户,可以使用 PyXML 包.
CORBA Scripting with Python CORBA 分布式对象系统 脚本与Python
This specifies the mapping from OMG IDL to Python. 论述Python 对于OMG IDL 的映射



注脚

...P7.1
译按:(cross-language?交叉语言;)
...P7.2
译按:(levels?级别,阶层;)



子节目录