Reputation: 24921
I unmarshall an XML that comes in a specific format e.g.
<root>
<a/>
<b/>
<c>
<x/>
</c>
<d/>
</root>
After playing around with the Java object, I want to send it to another service that uses a different schema, e.g.
<anotherRoot>
<a/>
<x/>
<something>
<d/>
</something>
</anotherRoot>
Can this be done "easily" w/ JAXB?
Upvotes: 3
Views: 1012
Reputation: 149017
Using any JAXB (JSR-222) implementation you could use XSLT on a JAXBSource
and the javax.xml.transform
APIs to produce a secondary XML structure:
JAXBContext jc = JAXBContext.newInstance(Foo.class);
// Output XML conforming to first XML Schema
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(foo, System.out);
// Create Transformer on Style Sheet that converts XML to
// conform the second XML Schema
TransformerFactory tf = TransformerFactory.newInstance();
StreamSource xslt = new StreamSource(
"src/example/stylesheet.xsl");
Transformer transformer = tf.newTransformer(xslt);
// Source
JAXBSource source = new JAXBSource(jc, foo);
// Result
StreamResult result = new StreamResult(System.out);
// Transform
transformer.transform(source, result);
Full Example
Upvotes: 3
Reputation: 27104
You can create create a proxy for the other service and regard its beans as simple data transfer objects.
So when you wish to call the service, you manually fill the beans based on the values of your proper model objects (the one you play around with, the ones that contain business logic) and call the service using the beans.
If changes happen to the service's interface, you can recreate the proxy and the compiler will help you fix the transformation.
Upvotes: 1