What is the difference between Castor XML binding and JAXB binding

戏子无情 提交于 2019-12-05 01:57:28

Please note that JAXB is an API, and there are multiple implementations available.

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.

There is a great article in 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.

Simple is an easy to use binding framework, and works with minimal 'simple' configuration.

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.

You get the class cast exception because a given JAXBContext instance associates each root XML element name with one binding class.

So when you marshal packageA.ClassA to XML, and then unmarshal it back again, the result will be a packageA.ClassA, and you can't cast that.

If you want to unmarshal to a packageB.ClassA, then you need to build a second JAXBContext. The first JAXBContext knows 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!