Xerces 简明教程

Apache Xerces - DOM Parser Overview

文档对象模型是万维网联盟(W3C)的正式建议。它定义了使程序能够访问和更新 XML 文档的样式、结构和内容的接口。支持 DOM 的 XML 解析器实施了该接口。

The Document Object Model is an official recommendation of the World Wide Web Consortium (W3C). It defines an interface that enables programs to access and update the style, structure, and contents of the XML documents. XML parsers that support the DOM, implement that interface.

When to use?

你应在下列情况下使用 DOM 解析器:

You should use a DOM parser when −

  1. You need to know a lot about the structure of a document.

  2. You need to move parts of the document around (you might want to sort certain elements, for example).

  3. You need to use the information in the document more than once.

What you get?

使用 DOM 解析器解析 XML 文档时,你会得到一个包含你的文档所有元素的树形结构。DOM 提供了各种函数让你能够检查文档的内容和结构。

When you parse an XML document with a DOM parser, you get back a tree structure that contains all of the elements of your document. The DOM provides a variety of functions you can use to examine the contents and structure of the document.

Advantages

DOM 是用于操作文档结构的通用接口。它的一个设计目标是,为一个 DOM 兼容解析器编写的 Java 代码应该可以在任何其他 DOM 兼容解析器上运行,无需进行更改。

The DOM is a common interface for manipulating document structures. One of its design goals is that the Java code written for one DOM-compliant parser should run on any other DOM-compliant parser without changes.

DOM interfaces

DOM 定义了若干 Java 接口。这里是最常见的接口 −

The DOM defines several Java interfaces. Here are the most common interfaces −

  1. Node − The base datatype of the DOM.

  2. Element − The vast majority of the objects you will deal with are Elements.

  3. Attr − Represents an attribute of an element.

  4. Text − The actual content of an Element or Attr.

  5. Document − Represents the entire XML document. A Document object is often referred to as a DOM tree.

Common DOM methods

当使用 DOM 时,有几种经常使用的方法 −

When you are working with the DOM, there are several methods that are used often −

  1. Document.getDocumentElement() − Returns the root element of the document.

  2. Node.getFirstChild() − Returns the first child of a given Node.

  3. Node.getLastChild() − Returns the last child of a given Node.

  4. Node.getNextSibling() − These methods return the next sibling of a given Node.

  5. Node.getPreviousSibling() − These methods return the previous sibling of a given Node.

  6. Node.getAttribute(attrName) − For a given Node, returns the attribute with the requested name.