Marshalling nested objects to a “flat” XML structure

Arolkiforself

New Member
How might I marshall an object hierarchy such that, instead of having the component objects becoming nested XML elements, their properties become direct children of the root element with their names prefixed by their type.For example, given:\[code\] (A)public class Customer { protected String firstName; protected String lastName; protected Address address;}public class Address { protected String street; protected String city;}\[/code\]Using the usual JAXB annotations would result in\[code\] (B)<customer> <firstName>Juan</firstName> <lastName>dela Cruz</lastName> <address> <street>123 Rizal Avenue</street> <city>Manila</city> </address></customer>\[/code\]But, instead, I need to marshall the same as\[code\] (C)<customer> <firstName>Juan</firstName> <lastName>dela Cruz</lastName> <address_street>123 Rizal Avenue</address_street> <address_city>Manila</address_city></customer>\[/code\]It would be great if there were some JAXB incantation to solve my needs since I'm already using JAXB for most of the things around this problem. In fact, these present some constraints to my specific situation:[*]The Java classes in (A) are generated by JAXB from an existing schema that corresponds to the XML structure in (B). I would prefer not to maintain modified versions of the generated classes.[*]I do not own or maintain the said schema. The actual schema is quite large and is often subject to minor modifications. It would be tedious to come up with and maintain an equivalent schema. Also, to keep up with schema modifications, I rely on the automated class generation by JAXB.[*]If it makes things easier, nesting is only up to one level deep. In the example, \[code\]Address\[/code\] would not contain any other complex type.I'm looking at the \[code\]@XmlPath\[/code\] annotation from MOXy, but I can't figure out how to get the node names prefixed as in (C).I dream of a solution where some xjc customizations providing the right JAXB annotations get me going, but that's looking unlikely from my searches so far. Any non-JAXB solution would suffice.
 
Back
Top