It's not a secret anymore!
The moose likes XML and Related Technologies and the fly likes Can JAXB handle Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Can JAXB handle Polymorphism" Watch "Can JAXB handle Polymorphism" New topic

Can JAXB handle Polymorphism

Rr Kumaran
Ranch Hand

Joined: Sep 17, 2001
Posts: 548
Hi All,

Say I have below class structures:

say if I am trying to marshalling my java object to xml using JAXB & below code snippet :

then can JAXB handle this or does it throw any exceptions. Please suggest ?

Thanks & Regards,

RR Kumaran
SCJP 1.4
Gotur Gotur

Joined: Jul 20, 2005
Posts: 4

It doest not do the way u given the code JAXB classes are generated for each "ComplexType" in ur XML Schema.One class(for Parent element in ur XML schema) will be generated which contains the instance objects for other complex elements, and each individual classes will be there for other child complex types in ur schema.
By accessing the main root element class, u can access the instances for other child elements, thats how the concepts of marshalling/unmarshalling works.
If u want to explore more, try to generate the JAXB classes for ur schema using JWSDP(Java webservices package) package using Ant Build tool.

JAXB concepts are wonderfull technoilogy in reading XML, file.

If u need more, just buzz
Rr Kumaran
Ranch Hand

Joined: Sep 17, 2001
Posts: 548
say we have a schema that resembles my class structure. I think we can represent inheritance in xml schemas.
I agree. Here's the link:
subject: Can JAXB handle Polymorphism
It's not a secret anymore!