tags:

views:

251

answers:

2

Can XmlJavaTypeAdapter marshal from ClassA object to ClassB object? So that when :

public void createClassB (ClassB b) { }

I can pass in ClassA object. Does it possible?

+1  A: 

Yes it can, but not quite like this. I mean you can not pass a ClassA object, where ClassB is required, unless ClassA is a sub-class of ClassB. Read through this example, its showing how you can marshal a Currency object as a String into your XML. So, quiet similar to the example, you should be able to marshal ClassA object to ClassB or something.

Adeel Ansari
Hi thanks the example very clear, so which mean there is no way to use xmljavatypeadapter so that i pass in ClassA object to this cresteClassB method even their attributes and class name is the same just from different package?
I think you would be able to do that if change your method definition something like this, `public void createClassB (ClassA a) { }`. Now you would be able to pass `ClassA` type of object as an argument.
Adeel Ansari
A: 

A little different solution, which is odd, and which you probably aren't looking for, but anyway:

  1. Annotate both classes with the same annotations
  2. When marshalling, put only your ClassA in the JAXB Context
  3. When unmarshalling, put only your ClassB in the JAXB Context
Bozho
Hi what you mean by Annotate both classes with same annotations?
JAXB has annotations, with which you can configure your classes details. @XmlType, @XmlElement, etc. Use the same annotations (or the same xml, if you like) on both classes. This is more of a workaround, though.
Bozho