tags:

views:

407

answers:

3

Note: Editing this to rephrase it around JAXB in hopes of getting new answers. I'm using CXF, but it's using JAXB for the mappings.

I've got a POJO model. Right now I have it mapped via annotations and using JAXB to spew/read XML. However that's only one XML format and I need to map that POJO model to one of various XML formats depending on the 3rd party system I'm integrating with (e.g. various 3rd parties all have the concept of a "person", but map it differently). I've read through the entire JAXB tutorial, but everything is centered around annotations. Is there some external way to map the classes so I can read/write multiple mappings where I pick the mapping to use at any given point (i.e. I know I'm spewing a "person" to Foo Inc., so use the foo mapping)?

Edit: I just found something called JAXBIntroductions that might do the job. http://community.jboss.org/wiki/JAXBIntroductions

+1  A: 

hi, you can use XStream for this. you can work without annotations like this:

XStream xstream = new XStream();
XStream xstream = new XStream(new DomDriver());
xstream.alias("person", Person.class);
xstream.alias("phonenumber", PhoneNumber.class);

hope that helps

EDIT: create another XStream instance for a different output

Person demo = new Person("Chris");

XStream xStream = new XStream();
xStream.alias("person", Person.class);
System.out.println(xStream.toXML(demo));

XStream xStream2 = new XStream();
xStream2.alias("dev", Person.class);
System.out.println(xStream2.toXML(demo));

output:

<person>
  <name>Chris</name>
</person>
<dev>
  <name>Chris</name>
</dev>
Stefan De Boey
Does XStream let me customize how it maps? So, can I map the Java Person to <employer> in one mapping, but <employee> in another mapping?
Chris Kessel
yes, you can, if you create another XStream instance and configure it accordingly, i'll edit the answer with an example and the output
Stefan De Boey
Ah, interesting. I'll have to check that out. My mapping needs are going to need quite a bit of flexibility (for instance, some sub-Java objects may need values in the same XML element as the parent. E.g. Address and FullName may be a Java children, but in the XML it's spewed at the same level or as attributes rather than elements).
Chris Kessel
MOXy has this type of support, for more information on its XPath based mapping see: http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
Blaise Doughan
A: 

you can try http://code.google.com/p/jlibs/wiki/SAX2JavaBinding

This is also based on annotations. but annotations are not on your POJO.

Santhosh Kumar T
+1  A: 

If you are using EclipseLink JAXB (MOXy) you can take advantage of the externalized mapping feature to apply many XML representations to your POJOs.

Also, since MOXy's mappings are XPath based you can actually map your POJOs to a wide variety of XML schemas.

Blaise Doughan