Java XML 绑定

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

Java XML Binding

javaxmljaxbcastorxmlbeans

提问by Paul Croarkin

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.

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

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

澄清:我不仅想了解您使用的框架,还想了解您使用一个框架而不是其他框架的原因。

采纳答案by Dan Dyer

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.

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

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.

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

回答by johnstok

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:

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

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

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

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

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.

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

回答by Dónal

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:

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

  • Compile XSD
  • Use generated java classes to read/write documents conforming to this schema
  • 编译 XSD
  • 使用生成的 java 类来读/写符合此模式的文档

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

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

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

回答by Steve K

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

我们使用xstream。编组/解组是微不足道的。有关示例,请参阅他们的教程

回答by MetroidFan2002

Jibxis 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).

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

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

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

回答by Spencer Kormos

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.

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

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

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

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.

从那以后我就没有使用过它,但我有一个即将到来的项目,它只需要这样一个框架,我很想知道其他人如何看待上述问题。

回答by anjanb

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

我 7 年前使用过蓖麻——它工作得相当好。使用 DTD。当时选择不多。

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.

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

BR,
~A

BR,
~A

回答by Eugene Yokota

回答by Cog

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

XmlBeans 是一个不错的选择,尤其是当您有“损坏的”XSD/WSDL 文件时。

Don mentioned

唐提到

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

员工文档 empDoc = 员工文档.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.

无需与命名空间争斗,遍历到要解组的对象,然后 Factory.parse 它。

Wish I had found it 2 weeks ago.

希望我在 2 周前找到了它。

回答by victor hugo

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.

我也在想完全一样的问题,最后我找到了IBM做的这个性能测试。http://www.ibm.com/developerworks/library/x-databdopt2/。我猜JiBX是我的选择,呵呵。