Java XML 绑定
您使用什么将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您想做出明智的决定,您需要清楚为什么要在 XML 和 Java 对象之间进行转换。 原因是这个领域的不同技术试图解决不同的问题。 不同的工具分为两类:
对于 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:
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.
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.
如果您有 XML 的 XSD,并且不需要将数据绑定到现有的一组类,那么我真的很喜欢 XMLBean。 基本上,它的工作原理如下:
将 XML 文档绑定到生成的类非常简单:
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:
Binding an XML document to the generated classes is as simple as:
我们使用xstream。 编组/解组很简单。 有关示例,请参阅他们的教程。
We use xstream. Marshalling / unmarshalling is trivial. See their tutorial for examples.
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).
我使用 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.
我七年前使用过 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
相关:Java 中的 XML 序列化?
Related: XML serialization in Java?
XmlBeans 是一个不错的选择,特别是如果您有“损坏的”XSD/WSDL 文件。
唐提到
..但它也可以采用节点、文件或任何源。
不与命名空间发生冲突,
遍历到要解组的对象,
和 Factory.parse 它。
希望我两周前就找到了它。
XmlBeans is a good choice especially if you have 'broken' XSD/WSDL files.
Don mentioned
..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.
我们使用蓖麻。 它非常适合我们的需求。
We use Castor. It suits our needs fairly well.
我也想知道同样的问题,最后我找到了 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.