简体   繁体   中英

java-to-java transformation using XSLT

I would like to create a method in a webservice that can recieve an object of a Java Class that has been created with JAXB and generate an object of another Java Class (created with JAXB as well) using an XSL file that defines the relation between the XML files that I have used with JAXB.

Any help?

Thank you very much

You're on the right track by looking at the javax.xml.transform APIs. A Transformer takes its input from an object that implements Source and sends its output to an object that implements Result , and there are JAXBSource and JAXBResult classes that will support the use case you're after

MySourceJaxbClass sourceObject = // ...
Transformer transformer = transformerFactory.newTransformer(xsltSource);
JAXBContext context = // however you need to create your context
Source src = new JAXBSource(context, sourceObject);
JAXBResult res = new JAXBResult(context);
transformer.transform(src, res);

MyTargetJaxbClass resultObject = (MyTargetJaxbClass)res.getResult();

Of course you don't necessarily have to use the same JAXBContext for both the source and the result - if they're unrelated class hierarchies it might make more sense to use a separate context for each.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM