Michal Pasinski
Michal Pasinski

Reputation: 578

JAXB and XSLT performance

I've got following code to perform XSLT transformation from one JAXB annotated object to another JAXB annotated object. Is the performance going to be really poor in this case? Does it marshall JAXB object to XML, transform it to another XML and unmarshall, or are there any better tricks here?

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(new StreamSource(mapping.getInputStream()));
JAXBResult result = new JAXBResult(JaxbUtils.getJAXBContext(CalypsoUploadDocument.class));
transformer.transform(new JAXBSource(JaxbUtils.getJAXBContext(CalypsoUploadDocument.class), uploadMessage),result);

return result.getResult();

Upvotes: 1

Views: 1876

Answers (3)

bdoughan
bdoughan

Reputation: 149017

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

Alternate Approach

If you happen to be using MOXy as your JAXB provider then you could have a single Java model that your map to both XML schemas. You could map to the first using the standard JAXB (JSR-222) annotations, and the map to the second using MOXy's external mapping document. Below is a link to my blog where I use this approach to map one object model to both the output from the Google and Yahoo weather services.

Upvotes: 2

Jörn Horstmann
Jörn Horstmann

Reputation: 34024

That depends on the XSLT processor. The commercial version of Saxon can do some transformations in streaming mode. JAXBSource extends SAXSource and so works on a stream of events too. For more complicated transforms or with other XSLT processors, the implementation will probably first create an internal tree-like structure. This structure is usually more optimized and better performing than a DOM tree, see Comparing DOM and other object models for some benchmarks. It would be best to benchmark or profile your code yourself and see if the performance is acceptable.

One note on your code, the JAXBContext is expensive to create and should be cached for the lifetime of your application. If your JaxbUtils class does not already do this, reusing the context could give a nice performance boost.

Upvotes: 2

rmuller
rmuller

Reputation: 12859

If using Java 6 (my production experience is based on the Oracle JDK) this is a valid approach we also have done. Performance is quite good. A minor improvement is to use a Templates instance.

On the other hand I do not recommend this approach if your application has one or just a few transformations and the transformations itself will not change over time. Then a direct approach is much faster (java instance -> java mapping -> java instance)

Upvotes: 2

Related Questions