JAXB: unmarshalling xml with multiple names for the same element

sizory

New Member
I figure this will be easy for someone who really understands JAXB binding files...Basic QuestionHow do you configure JAXB to unmarshal multiple elements into the same class?Note: I want to avoid adding another dependency to my project (like MOXy). Ideally, this can be accomplished with annotations or a custom bindings file.BackgroundI have an XML document that contains lots of variations of the same element--each with the exact same properties. Using my example below, all I care about is "Employees" but the XML specifies "directors, managers and staff." For our purposes, these are all subclasses of the same parent and we only need to work with the parent type (Employee) and our object model doesn't have or need instances of the subclasses.I want JAXB to bind any instance of \[code\]director, manager, or staff\[/code\] elements into an \[code\]Employee\[/code\] object. Exampleinput:\[code\]<organization> <director> <fname>Dan</fname> <lname>Schman</lname> </director> <manager> <fname>Joe</fname> <lname>Schmo</lname> </manager> <staff> <fname>Ron</fname> <lname>Schwan</lname> </staff> <staff> <fname>Jim</fname> <lname>Schwim</lname> </staff> <staff> <fname>Jon</fname> <lname>Schwon</lname> </staff> </organization>\[/code\]output: After unmarshalling this example, I would end up with an \[code\]Organization\[/code\] object with one property: \[code\]List<Employees> employees\[/code\] where each employee only has a firstName and lastName.(Note: each employee would be of type \[code\]Employee\[/code\] NOT \[code\]Director/Manager/Staff\[/code\]. Subclass information would be lost when unmarshalling. We also don't care about marshaling back out--we only need to create objects from XML)Can this be done without extensions like MOXy? Can a custom bindings.xjb file save the day?
 
Back
Top