Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Castor XML binding and JAXB binding

What is the difference between Castor XML and JAXB binding since both are binding java object to XML and vice versa.

Updated :

As using Castor I can do this Assume packageA.ClassA and packageB.ClassA have same attributes and class name just that they were located in different package.

packageA.ClassA - > XML -> packageB.ClassA 

By using JAXB if I am doing this Marshall object packageA.ClassA to XML and from XML unmarshall into object packageB.ClassA I got Casting error.

like image 728
user236501 Avatar asked Jan 11 '10 06:01

user236501


People also ask

What binding means Jaxb?

A new Java API called Java Architecture for XML Binding (JAXB) can make it easier to access XML documents from applications written in the Java programming language.

What are the three modes in which Castor XML can be used?

To (un-)marshal data to and from XML, Castor XML can be used in one of three modes: introspection mode. mapping mode. descriptor mode (aka generation mode)

What does Exolab Castor XML XMLContext class offer?

XMLContext class has been added to the Castor marshalling framework. This new class provides a bootstrap mechanism for Castor XML, and allows easy (and efficient) instantiation of org. exolab.

Which annotation is used to map a Java class object to XML element?

The JAXB annotations defined in the javax. xml. bind. annotations package can be used to customize Java program elements to XML schema mapping.


2 Answers

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.

like image 164
Vasu Avatar answered Sep 19 '22 03:09

Vasu


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.

like image 27
skaffman Avatar answered Sep 21 '22 03:09

skaffman