JAXB: Unmarshall for different classes based on element attribute value

I would like to know if there is a way to decouple the XML containing the fixed name of the element whose attribute belongs to many classes. Consider the following XML:

Case No. 1:

<?xml version="1.0" encoding="UTF-8"?> <response> <request-status>valid</request-status> <interface name="person"> <f-name>Joe</f-name> <l-name>Blow</l-name> <age>25</age> <email> joe.blow@email.com </email> </interface> </response> 

Case No. 2:

 <?xml version="1.0" encoding="UTF-8"?> <response> <request-status>valid</request-status> <interface name="vehicle"> <make>Ford</make> <type>truck</type> <year>1989</year> <model>F150</model> </interface> </response> 

In both cases, the contained class is the "response", with two instance variables: requestStatus (String) and an interface (some superclass?). I need help on how to write the contained "Answer" class with the correct JAXB annotations so that unmarshall creates the correct class instances for the "interface" variable.

Thanks for the help for any help.

+4
source share
1 answer

Note. I have EclipseLink JAXB (MOXy) leads and a member of the JAXB 2 group (JSR-222) .

You can use the MOXy extension @XmlDescriminatorNode / @XmlDescriminatorValue JAXB.

Base

 @XmlDiscriminatorNode("@name") public abstract class Base { } 

Person

 @XmlDiscriminatorValue("person") public class Person extends Base { } 

Car

 @XmlDiscriminatorValue("vehicle") public class Vehicle extends Base { } 

Additional Information

Below is the answer to a similar question that uses an older version of the MOXy API:

+3
source

All Articles