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

查看:572
本文介绍了什么是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 Schema并创建一组与该模式对应的类。 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 Schema非常重要(许多级别的层次结构,某些字段可以重复,其他字段不能重复),因此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天全站免登陆