什么是 JAXB,我为什么要使用它? [英] What is JAXB and why would I use it?

查看:45
本文介绍了什么是 JAXB,我为什么要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 来处理 XML.基本上,它为这个问题提供了解决方案(我假设熟悉 XML、Java 数据结构和 XML 模式):

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):

使用 XML 很困难.需要一种方法来获取 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.

JAXB 将采用您编写的 XML 模式并创建一组对应于该模式的类.JAXB 实用程序将创建用于操作该 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 文件,然后创建生成的类的实例 - 加载来自 XML 的数据.JAXB 也做相反的事情:获取 java 类,并生成相应的 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,因为它易于使用,并且带有 Java 1.6(如果您使用的是 1.5,您可以下载 JAXB .jars.)它创建类层次结构的方式很直观,而且根据我的经验,确实如此把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".

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

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 就很棒.在我的项目中,我在二进制(由 C 程序使用)和 XML(以便人类可以使用和修改该数据)之间转换大量数据.生成的 XML 模式非常重要(层次结构有很多层次,有些字段可以重复,有些则不能),因此 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.

这篇关于什么是 JAXB,我为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆