Java XML 绑定

发布于 2024-07-07 21:37:47 字数 167 浏览 11 评论 0原文

您使用什么将 XML 绑定到 Java? JAXB、Castor 和 XMLBeans 是一些可用的选择。 我见过的比较都是三四年前的事了。 我愿意接受其他建议。 编组/解组性能和易用性特别令人感兴趣。

澄清:我不仅想了解您使用的框架,还想了解您使用其中一种框架而不是其他框架的理由。

What are you using for binding XML to Java? JAXB, Castor, and XMLBeans are some of the available choices. The comparisons that I've seen are all three or four years old. I'm open to other suggestions. Marshalling / unmarshalling performance and ease of use are of particular interest.

Clarification: I'd like to see not just what framework you use, but your reasoning for using one over the others.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

一杆小烟枪 2024-07-14 21:37:47

如果您想做出明智的决定,您需要清楚为什么要在 XML 和 Java 对象之间进行转换。 原因是这个领域的不同技术试图解决不同的问题。 不同的工具分为两类:

  1. XML 数据绑定 - 指将 XML 文档中的信息表示为计算机内存中的对象的过程。 通常,这意味着定义 XSD 并生成等效的 java 源代码。 不同语言之间的互操作是重中之重(因此使用 XSD)——最典型的是用于基于 SOAP 的 Web 服务的实现。
  2. XML 序列化 - 是指将内存中对象的图形写入流,以便可以在某个地方或其他时间重新构建它。 您手工编写 Java 类; xml 表示是次要的。 此外,对性能的需求通常更高,而与其他语言(例如 .net)互操作的需求通常更低。

对于 xml 序列化,Xstream 很难被击败。 JAXB 是 XML 绑定的标准。

无论哪种情况,如果您使用 J2EE,您都需要特别注意从 JPA 检索的类,因为类代理和持久性特定集合类型可能会混淆绑定/序列化工具。

If you want to make an informed decision you need to be clear why you are translating between XML and java objects. The reason being that the different technologies in this space try to solve different problems. The different tools fall into two categories:

  1. XML data binding - refers to the process of representing the information in an XML document as an object in computer memory. Typically, this means defining an XSD and generating a java source code equivalent. Interop between different languages is top priority (hence the use of XSD) - most typically for the implementation of SOAP-based web services.
  2. XML serialisation - refers to writing out a graph of in memory objects to a stream, so that it can be reconstituted somewhere or sometime else. You write the java classes by hand; the xml representation is of secondary importance. Also, the need for performance is often greater and the need for interoperation with other languages such as .net is often lower.

For xml serialisation, Xstream is hard to beat. JAXB is the standard for XML binding.

In either case, if you are using J2EE you'll need to pay careful attention to classes retrieved from JPA since class proxies and persistence specific collection types can confuse binding / serialization tools.

月光色 2024-07-14 21:37:47

JiBX。 之前我使用 Castor XML,但事实证明 JiBX 明显更好,特别是在性能方面(将某些应用程序代码从 Castor XML 直接移植到 JiBX 使其速度提高了 9 倍)。 我还发现 JiBX 的映射格式比 Castor 的更优雅。

JiBX 通过使用编译后字节码操作而不是 Castor 采用的反射方法来实现其性能。 这样做的优点是对编写映射类的方式提出了更少的要求。 不需要仅仅为了满足工具的需要而使用 getter、setter 和无参数构造函数。 大多数时候,您可以在不考虑映射问题的情况下编写类,然后在不进行修改的情况下映射它。

JiBX. Previously I used Castor XML, but JiBX proved to be significantly better, particularly in terms of performance (a straight port of some application code from Castor XML to JiBX made it 9x faster). I also found the mapping format for JiBX to be more elegant than Castor's.

JiBX achieves its performance by using post-compilation bytecode manipulation rather than the reflection approach adopted by Castor. This has the advantage that it places fewer demands on the way that you write your mapped classes. There is no need for getters, setters and no-arg constructors just to satisfy the tools. Most of the time you can write the class without considering mapping issues and then map it without modifications.

倾城泪 2024-07-14 21:37:47

如果您有 XML 的 XSD,并且不需要将数据绑定到现有的一组类,那么我真的很喜欢 XMLBean。 基本上,它的工作原理如下:

  • 编译 XSD
  • 使用生成的 java 类来读取/写入符合此模式的文档

将 XML 文档绑定到生成的类非常简单:

EmployeesDocument empDoc = EmployeesDocument.Factory.parse(xmlFile); 

If you have an XSD for the XML, and you don't need to bind the data to an existing set of classes, then I really like XMLBeans. Basically, it works like this:

  • Compile XSD
  • Use generated java classes to read/write documents conforming to this schema

Binding an XML document to the generated classes is as simple as:

EmployeesDocument empDoc = EmployeesDocument.Factory.parse(xmlFile); 
小清晰的声音 2024-07-14 21:37:47

我们使用xstream。 编组/解组很简单。 有关示例,请参阅他们的教程

We use xstream. Marshalling / unmarshalling is trivial. See their tutorial for examples.

堇年纸鸢 2024-07-14 21:37:47

Jibx 是这里使用的。 它非常快,但绑定可能有点棘手。 然而,如果您有描述域对象的 XML 模式,它就特别有用,因为它确实可以很好地映射到 XSD(甚至有一个测试版工具 XSD2Jibx,它可以采用 XSD 并创建存根域类和映射,然后您可以将其获取并引导到适合您现有的域模型)。

它操作字节码,因此必须在 Java .class 文件初始编译后运行。 你可以使用Maven插件,或者直接使用它(Eclipse插件似乎不适合我)。

Jibx is what is used around here. It is very fast, but the bindings can be a little tricky. However, it is especially useful if you have XML schemas describing your domain objects, as it really maps well to XSD (there's even a beta tool XSD2Jibx which can take XSDs and create stub domain classes and mappings, which you can then take and coax to fit your existing domain model).

It manipulates bytecode, so it must be run after the initial compilation of the Java .class files. You can use the Maven plugin for it, or just use it directly (the Eclipse plugin didn't seem to work for me).

梦开始←不甜 2024-07-14 21:37:47

我使用 Jaxb 取得了不同程度的成功。 当时(几年前)整体文档乏善可陈,基本使用文档(包括在哪里下载实现)很难找到或多种多样。

编写 Java 类的解析器非常好,与原始 XSD 几乎没有差异(尽管我认为它在支持抽象 XML 元素方面存在问题)。

从那以后我就没有使用过它,但我有一个即将到来的项目,它需要这样一个框架,我有兴趣知道其他人如何公平对待上述框架。

I've used Jaxb with varying success. At the time (a couple of years back) the overall documentation was lackluster and the basic usage documentation (including where to download implementations) was difficult to find or varied.

The parser which wrote the Java classes was quite good with little discrepancy against the original XSD (though I think it had problems supporting abstract XML elements).

I haven't used it since, but I have an upcoming project which will require just such a framework and I will be interested to know how anyone else fairs with the above.

心不设防 2024-07-14 21:37:47

我七年前使用过 Castor ——效果相当好。 使用 DTD。 那个时候选择不多。

在当前的项目中,我使用了
1) JAXB——基于标准,提供参考实现,提供命令行和ant工具。 最新版本 - 2.1.8 需要 java 5+。
2) XStream——用于 Soap 解组——需要 Java 5+。 不如最新的 JAXB 快且符合标准。

BR,
~A

I used castor 7 years ago -- it worked fairly well. used DTDs. Not many choices at that time.

In current projects, I've used
1) JAXB -- standards based, Reference implementation available, command line and ant tools available. latest version - 2.1.8 needs java 5+.
2) XStream -- for Soap unmarshalling -- needs Java 5+. Is not as fast and standards compliant as JAXB latest.

BR,

~A

云归处 2024-07-14 21:37:47

XmlBeans 是一个不错的选择,特别是如果您有“损坏的”XSD/WSDL 文件。

唐提到

EmployeesDocument empDoc =EmployeesDocument.Factory.parse(xmlFile);

..但它也可以采用节点、文件或任何源。

不与命名空间发生冲突,
遍历到要解组的对象,
和 Factory.parse 它。

希望我两周前就找到了它。

XmlBeans is a good choice especially if you have 'broken' XSD/WSDL files.

Don mentioned

EmployeesDocument empDoc = EmployeesDocument.Factory.parse(xmlFile);

..but it can also take a Node, or a File, or just about any source.

No fighting with namespaces,
traverse to the object you want to unmarshall,
and Factory.parse it.

Wish I had found it 2 weeks ago.

寻找我们的幸福 2024-07-14 21:37:47

我们使用蓖麻。 它非常适合我们的需求。

We use Castor. It suits our needs fairly well.

也只是曾经 2024-07-14 21:37:47

我也想知道同样的问题,最后我找到了 IBM 所做的性能测试。 http://www.ibm.com/developerworks/library/x-databdopt2/< /a>. 我想 JiBX 是我的选择,呵呵。

I was wondering exactly the same question, and finally I found this performance tests made by IBM. http://www.ibm.com/developerworks/library/x-databdopt2/. JiBX is my choice I guess, hehe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文