Java Xml 简明教程

Java XML Parsers

Java 有各种支持对象类型和事件类型标准的 XML 解析器。我们可以使用这些 API 读取、创建、查询和修改 XML 文档。API 提供了表示 XML 文档、用于检索和修改 XML 文档中元素和属性的函数的接口。

Java has various XML parsers that support object type and event type standards. We can read, create, query and modify the XML documents using these APIs. APIs provide interfaces that represent the XML documents, methods to retrieve and modify the elements and attributes in XML documents.

XML Parsers

XML 解析器是帮助客户端应用程序通过接口与 XML 文档交互的软件库或包。它们用于检查 XML 的语法并根据 DTD 或 XML 架构验证它。解析器可以基于文档或基于事件。

XML Parsers are the software libraries or packages that help client applications to interact with XML documents through interfaces. They are used to check the syntax of XML and validate it according to the DTD or XML schema. Parsers can be either document based or event based.

Types of XML parsers

以下 XML 解析器的两个主要类型 -

Following are the two main types of XML parsers −

  1. DOM (Document Object Model)

  2. SAX (Simple API for XML)

DOM 由 W3C(万维网联盟)提出。它是一个基于树的 API,并在主内存中将整个 XML 文档创建成一个解析树。因此,它会消耗更多的内存。DOM API 提供了用于访问、添加和修改独立于编程语言文档的接口。由于 DOM 会消耗更多的内存,因此它适用于小型文档。我们可以使用 DOM API 同时读取和创建文档。

DOM is proposed by W3C (World Wide Web Consortium). It is a tree based API and creates the entire XML document into a parse tree inside the main memory. Hence, it consumes more memory. DOM API provides interfaces to access, add and modify the documents that are independent of programming languages. DOM is suitable for small documents as it consumes more memory. We can both read and create documents using DOM APIs.

dom parser

SAX 是基于事件的 API。它不会加载整个文档。相反,它加载 XML 文件的小部分内容。SAX 是一个只读 API,我们无法使用 SAX 创建 XML 文档。它用于处理大型 XML 文档,因为它消耗的内存较少。

SAX is an event based API. It will not load the entire document. Instead, it loads small parts of XML file. SAX is a read-only API, we cannot create XML documents using SAX. It is used to process large XML documents as it consumes less memory.

sax parser

Java XML Parsers

JAXP(用于 XML 处理的 Java API)API 提供了标准接口来处理 Java 应用程序中的 XML 文档。它具有支持 DOM 和 SAX 标准的接口。

The JAXP (*J*ava *A*PI for *X*ML *P*rocessing) APIs provide the standard interfaces to process XML documents in Java applications. It has interfaces that support both DOM and SAX standards.

下表描述了各种 XML 解析器及其在 java 中的相对类或接口。

The below table describes various XML parsers and their relative classes or interfaces in java.