开发者

Multiple XML mappings for the same Java object using JAXB?

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 a开发者_运维技巧nd 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


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

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML

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


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>


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜