Java 什么是 JAXB,我为什么要使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/607141/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:47:58  来源:igfitidea点击:

What is JAXB and why would I use it?

javajaxb

提问by Jay R.

There is guy here swearing that JAXB is the greatest thing since sliced bread. I am curious to see what Stack Overflow users think the use case is for JAXB and what makes it a good or a bad solution for that case.

这里有人发誓 JAXB 是自切片面包以来最伟大的东西。我很想知道 Stack Overflow 用户认为 JAXB 的用例是什么,以及是什么使它成为该用例的好或坏解决方案。

采纳答案by poundifdef

I'm a big fan of JAXB for manipulating XML. Basically, it provides a solution to this problem (I'm assuming familiarity with XML, Java data structures, and XML Schemas):

我非常喜欢使用 JAXB 来操作 XML。基本上,它为这个问题提供了一个解决方案(我假设熟悉 XML、Java 数据结构和 XML 模式):

Working with XML is difficult. One needs a way to take an XML file - which is basically a text file - and convert it into some sort of data structure, which your program can then manipulate.

使用 XML 很困难。人们需要一种方法来获取 XML 文件(基本上是一个文本文件)并将其转换为某种数据结构,然后您的程序可以对其进行操作。

JAXB will take an XML Schema that you write and create a set of classes that correspond to that schema. The JAXB utilities will create the hierarchy of data structures for manipulating that XML.

JAXB 将采用您编写的 XML 模式并创建一组对应于该模式的类。JAXB 实用程序将创建用于操作 XML 的数据结构层次结构。

JAXB can then be used to read an XML file, and then create instances of the generated classes - laden with the data from your XML. JAXB also does the reverse: takes java classes, and generates the corresponding XML.

然后可以使用 JAXB 读取 XML 文件,然后创建生成的类的实例 - 加载来自 XML 的数据。JAXB 也做相反的事情:获取 java 类,并生成相应的 XML。

I like JAXB because it is easy to use, and comes with Java 1.6 (if you are using 1.5, you can download the JAXB .jars.) The way it creates the class hierarchy is intuitive, and in my experience, does a decent job abstracting away the "XML" so that I can focus on "data".

我喜欢 JAXB,因为它易于使用,并且带有 Java 1.6(如果您使用的是 1.5,您可以下载 JAXB .jars。)它创建类层次结构的方式很直观,而且根据我的经验,它做得不错抽象出“XML”,以便我可以专注于“数据”。

So to answer your question: I would expect that, for small XML files, JAXB might be overkill. It requires you to create and maintain an XML schema, and to use "standard textbook methods" of utilizing Java classes for data structures. (Main classes, small inner-classes to represent "nodes", and a huge hierarchy of them.) So, JAXB is probably not that great for a simple linear list of "preferences" for an application.

所以要回答你的问题:我希望,对于小的 XML 文件,JAXB 可能有点矫枉过正。它要求您创建和维护 XML 模式,并使用“标准教科书方法”将 Java 类用于数据结构。(主类、表示“节点”的小内部类,以及它们的巨大层次结构。)因此,对于应用程序的“首选项”的简单线性列表,JAXB 可能不是那么好。

But if you have a rather complex XML schema, and lots of data contained within it, then JAXB is fantastic. In my project, I was converting large amounts of data between binary (which was consumed by a C program) and XML (so that humans could consume and modify that data). The resulting XML Schema was nontrivial (many levels of hierarchy, some fields could be repeated, others could not) so JAXB was helpful in being able to manipulate that.

但是,如果您有一个相当复杂的 XML 模式,并且其中包含大量数据,那么 JAXB 非常棒。在我的项目中,我在二进制(由 C 程序使用)和 XML(以便人类可以使用和修改该数据)之间转换大量数据。生成的 XML Schema 非常重要(许多层次结构,某些字段可以重复,其他字段不能),因此 JAXB 有助于对其进行操作。

回答by Tom Hawtin - tackline

It's an "ORM for XML". Most often used alongside JAX-WS (and indeed the Sun implementations are developed together) for WS Death Star systems.

它是“XML 的 ORM”。最常与 JAX-WS 一起使用(实际上 Sun 实现是一起开发的)用于 WS 死星系统。

回答by Fabian Steeg

With JAXB you can automatically create XML representations of your objects (marshalling) and object representations of the XML (unmarshalling).

使用 JAXB,您可以自动创建对象的 XML 表示(编组)和 XML 的对象表示(解组)。

As far as the XML Schema is concerned, you have two choices:

就 XML Schema 而言,您有两种选择:

  • Generate Java classes from an XSD
  • Generate an XSD from your Java classes
  • 从 XSD 生成 Java 类
  • 从 Java 类生成 XSD

There are also some simpler XML serialization libraries like XStream, Digesteror XMLBeansthat might be alternatives.

还有一些更简单的 XML 序列化库,如XStreamDigesterXMLBeans,它们可能是替代品。

回答by codefinger

Here's a reason not to use it: performance suffers. There is a good deal of overhead when marshaling and unmarshaling. You might also want to consider another API for XML-Object binding -- such as JiBX: http://jibx.sourceforge.net/

这是不使用它的一个原因:性能受到影响。编组和解组时有大量开销。您可能还想考虑另一个用于 XML 对象绑定的 API —— 例如 JiBX:http: //jibx.sourceforge.net/

回答by oxbow_lakes

JAXB is great if you have to code to some external XML spec defined as an XML schema (xsd).

如果您必须对定义为 XML 模式 ( xsd) 的某些外部 XML 规范进行编码,则 JAXB 非常有用。

For example, you have a trading application and you must report the trades to the Uber Lame Trade Reporting Appand they've given you ultra.xsdto be getting on with. Use the $JAVA_HOME/bin/xjccompiler to turn the XML into a bunch of Java classes (e.g. UltraTrade).

例如,您有一个交易应用程序,您必须向Uber Lame Trade Reporting App报告交易,他们已经让您ultra.xsd继续进行交易。使用$JAVA_HOME/bin/xjc编译器将 XML 转换为一堆 Java 类(例如UltraTrade)。

Then you can just write a simple adapterlayer to convert your trade objects to UltraTradesand use the JAXBto marshal the data across to Ultra-Corp. Mucheasier than messing about converting your trades into their XML format.

然后,您只需编写一个简单的适配器层即可将您的交易对象转换为UltraTrades并使用JAXB将数据编组到 Ultra-Corp。很多比搞乱转换您的交易到他们的XML格式更容易。

Where it all breaks down is when Ultra-Corp haven't actually obeyed their own spec, and the trade pricewhich they have down as a xsd:floatshould actually be expressed as a double!

当 Ultra-Corp 没有真正遵守他们自己的规范时,一切都会崩溃,而price他们作为交易的失败xsd:float实际上应该表示为double

回答by artgon

I use JAXB at work all the time and I really love it. It's perfect for complex XML schemas that are always changing and especially good for random access of tags in an XML file.

我一直在工作中使用 JAXB,我真的很喜欢它。它非常适合经常变化的复杂 XML 模式,尤其适合随机访问 XML 文件中的标签。

I hate to pimp but I just started a blog and this is literally the first thing I posted about!

我讨厌拉皮条,但我刚开始写博客,这实际上是我发布的第一件事!

Check it out here:

在这里查看:

http://arthur.gonigberg.com/2010/04/21/getting-started-with-jaxb/

http://arthur.gonigberg.com/2010/04/21/getting-started-with-jaxb/

回答by LostMohican

You can also check out JIBX too. It is also a very good xml data binder, which is also specialized in OTA (Open Travel Alliance) and is supported by AXIS2 servers. If you're looking for performance and compatibility, you can check it out :

您也可以查看 JIBX。它也是一个非常好的xml数据绑定器,它也专门用于OTA(开放旅行联盟),并由AXIS2服务器支持。如果您正在寻找性能和兼容性,您可以查看:

http://jibx.sourceforge.net/index.html

http://jibx.sourceforge.net/index.html

回答by LostMohican

JAXB provides improved performance via default marshalling optimizations. JAXB defines a programmer API for reading and writing Java objects to and from XML documents, thus simplifying the reading and writing of XML via Java.

JAXB 通过默认编组优化提供改进的性能。JAXB 定义了一个程序员 API,用于从 XML 文档读取和写入 Java 对象,从而简化了通过 Java 读取和写入 XML。

回答by Prashant_M

Why we need JAXB?The remote components (written in Java) of web services uses XML as a mean to exchange messages between each other. Why XML?Because XML is considered light weight option to exchange message on Networks with limited resources. So often we need to convert these XML documents into objects and vice versa. E.g: Simple Java POJO Employee can be used to send Employee data to remote component( also a Java programme).

为什么我们需要 JAXB?Web 服务的远程组件(用 Java 编写)使用 XML 作为彼此交换消息的手段。为什么是 XML?因为 XML 被认为是在资源有限的网络上交换消息的轻量级选项。因此,我们经常需要将这些 XML 文档转换为对象,反之亦然。例如:简单的 Java POJO Employee 可用于将 Employee 数据发送到远程组件(也是一个 Java 程序)。

class Employee{
 String name;
 String dept;
 ....
}

This Pojo should be converted (Marshall) in to XML document as follow:

这个 Pojo 应该被转换(Marshall)成 XML 文档,如下所示:

<Employee>
  <Name>...</Name>
  <Department>...</Department>
</Employee>

And at the remote component, back to Java object from XML document (Un-Marshall).

在远程组件,从 XML 文档返回 Java 对象(Un-Marshall)。

What is JAXB?

什么是 JAXB?

JAXB is a library or a tool to perform this operation of Marshalling and UnMarshalling. It spares you from this headache, as simple as that.

JAXB 是一个库或工具,用于执行 Marshalling 和 UnMarshalling 的这种操作。它让您免于头痛,就这么简单。