java Castor XML 绑定和 JAXB 绑定有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2040233/
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
What is the difference between Castor XML binding and JAXB binding
提问by user236501
What is the difference between Castor XML and JAXB binding since both are binding java object to XML and vice versa.
Castor XML 和 JAXB 绑定有什么区别,因为两者都将 java 对象绑定到 XML,反之亦然。
Updated :
更新 :
As using Castor I can do this Assume packageA.ClassAand packageB.ClassAhave same attributes and class name just that they were located in different package.
在使用 Castor 时,我可以这样做 AssumepackageA.ClassA并且packageB.ClassA具有相同的属性和类名,只是它们位于不同的包中。
packageA.ClassA - > XML -> packageB.ClassA
By using JAXB if I am doing this Marshall object packageA.ClassAto XML and from XML unmarshall into object packageB.ClassAI got Casting error.
通过使用 JAXB,如果我将这个 Marshall 对象转换packageA.ClassA为 XML,并从 XML 解组到对象中,packageB.ClassA我得到了 Casting 错误。
回答by Vasu
Please note that JAXB is an API, and there are multiple implementations available.
请注意,JAXB 是一个 API,并且有多种实现可用。
Sun provides a reference implementation and package it with J2EE (its available in J2SE 1.6 also). Castor was born before JAXB came out from Sun, and offers some extra features. But if all you want is plain XML binding, then the reference Sun implementation should work great.
Sun 提供了一个参考实现并将其与 J2EE 打包在一起(也可在 J2SE 1.6 中使用)。Castor 诞生于 JAXB 从 Sun 出来之前,并提供了一些额外的功能。但是,如果您只需要纯 XML 绑定,那么参考 Sun 实现应该会很好用。
There is a great articlein JavaWorld on this. A bit old but most ideas explained there still holds good. And you wont find the article mentioning JAXB annotations, which have made things easier nowadays.
JavaWorld 中有一篇关于此的很棒的文章。有点旧,但那里解释的大多数想法仍然有效。而且您不会找到提到 JAXB 注释的文章,它使现在的事情变得更容易。
Simple is an easy to use binding framework, and works with minimal 'simple' configuration.
Simple 是一个易于使用的绑定框架,并且使用最少的“简单”配置。
DOM is an entrirely different concept - its all about parsing and does nothing about binding. Using a DOM parser, you can pull out data from XML. But it doesn't give you an object mapping facility. So you still have to pull the data using DOM, and then write code to push this data to a java object.
DOM 是一个完全不同的概念——它完全与解析有关,而与绑定无关。使用 DOM 解析器,您可以从 XML 中提取数据。但它没有为您提供对象映射功能。所以还是得用DOM拉取数据,然后写代码把这些数据push到java对象中。
回答by skaffman
You get the class cast exception because a given JAXBContextinstance associates each root XML element name with one binding class.
您会得到类转换异常,因为给定的JAXBContext实例将每个根 XML 元素名称与一个绑定类相关联。
So when you marshal packageA.ClassAto XML, and then unmarshal it back again, the result will be a packageA.ClassA, and you can't cast that.
因此,当您编组packageA.ClassA到 XML,然后再次解组它时,结果将是 a packageA.ClassA,并且您不能强制转换。
If you want to unmarshal to a packageB.ClassA, then you need to build a second JAXBContext. The first JAXBContextknows about packageA.ClassA, the second knows about packageB.ClassA. Use the first one for marshalling to XML, the second one for unmarshalling . That will work as you expect.
如果要解组到 a packageB.ClassA,则需要构建第二个JAXBContext. 第一个JAXBContext知道packageA.ClassA,第二个知道packageB.ClassA。使用第一个用于编组到 XML,第二个用于解组。这将按您的预期工作。

