Java JAXP 和 JAXB 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2801502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between JAXP and JAXB?
提问by Jothi
What is the difference between JAXP and JAXB?
JAXP 和 JAXB 有什么区别?
采纳答案by skaffman
JAXP (Java API for XML Processing) is a rather outdated umbrella term covering the various low-level XML APIs in JavaSE, such as DOM, SAX and StAX.
JAXP(用于 XML 处理的 Java API)是一个相当过时的总称,涵盖了 JavaSE 中的各种低级 XML API,例如 DOM、SAX 和 StAX。
JAXB (Java Architecture for XML Binding) is a specific API (the stuff under javax.xml.bind
) that uses annotations to bind XML documents to a java object model.
JAXB(用于 XML 绑定的 Java 架构)是一个特定的 API(下面的内容javax.xml.bind
),它使用注释将 XML 文档绑定到 Java 对象模型。
回答by gmhk
JAXP is Java API for XML Processing, which provides a platform for us to Parse the XML Files with the DOM Or SAX Parsers.
JAXP 是 Java API for XML Processing,它为我们提供了一个使用 DOM 或 SAX 解析器解析 XML 文件的平台。
Where as JAXB is Java Architecture for XML Binding, it will make it easier to access XML documents from applications written in the Java programming language.
由于 JAXB 是用于 XML 绑定的 Java 体系结构,因此可以更轻松地从用 Java 编程语言编写的应用程序访问 XML 文档。
For Example : Computer.xml File, if we want to access the data with JAXP, we will be performing the following steps
例如:Computer.xml 文件,如果我们想用 JAXP 访问数据,我们将执行以下步骤
- Create a SAX Parser or DOM Parser and then PArse the data, if we use DOM, it may be memory intensive if the document is too big. Suppose if we use SAX parser, we need to identify the beginning of the document. When it encounters something significant (in SAX terms, an "event") such as the start of an XML tag, or the text inside of a tag, it makes that data available to the calling application.
- Then Create a content handler that defines the methods to be notified by the parser when it encounters an event. These methods, known as callback methods, take the appropriate action on the data they receive.
- 创建一个 SAX Parser 或 DOM Parser 然后解析数据,如果我们使用 DOM,如果文档太大可能会占用大量内存。假设如果我们使用 SAX 解析器,我们需要识别文档的开头。当它遇到重要的事情(在 SAX 术语中,“事件”)时,例如 XML 标记的开始或标记内的文本,它使调用应用程序可以使用该数据。
- 然后创建一个内容处理程序,它定义解析器在遇到事件时要通知的方法。这些方法称为回调方法,对它们接收到的数据采取适当的操作。
The Same Operations if it is performed by JAXB, the following steps needs to be performed to access the Computer.xml
同样的操作如果是由JAXB执行,需要执行以下步骤才能访问Computer.xml
- Bind the schema for the XML document.
- Unmarshal the document into Java content objects. The Java content objects represent the content and organization of the XML document, and are directly available to your program. After unmarshalling, your program can access and display the data in the XML document simply by accessing the data in the Java content objects and then displaying it. There is no need to create and use a parser and no need to write a content handler with callback methods. What this means is that developers can access and process XML data without having to know XML or XML processing
- 绑定 XML 文档的架构。
- 将文档解组为 Java 内容对象。Java 内容对象表示 XML 文档的内容和组织,可直接用于您的程序。解组后,您的程序可以通过访问 Java 内容对象中的数据然后显示它来访问和显示 XML 文档中的数据。无需创建和使用解析器,也无需使用回调方法编写内容处理程序。这意味着开发人员可以访问和处理 XML 数据,而无需了解 XML 或 XML 处理
回答by Denis Wang
The key difference is which role the xml Schema plays. JAXP is outdated without awareness of the XML Schema while JAXB handles the schema binding as the very first step.
主要区别在于 xml Schema 扮演的角色。JAXP 在不了解 XML 模式的情况下已经过时,而 JAXB 作为第一步处理模式绑定。