Transform From one JAXB object to another using XS

2019-02-27 07:21发布

Is there a way to transform a JAXB generated object to another JAXB object using an XSLT template file. The two objects are generated by two different JAXB bindings.

I know that I can marshall the object to strings and then using a XSLT processor to transform it to the other format. After that unmarshall it to the other JAXB object.

The question is if this is possible in to do in JAXB.

标签: java xml xslt jaxb
2条回答
成全新的幸福
2楼-- · 2019-02-27 08:04

I don't think its possible without any intermediate serialization or dom tree construction, but serializing to a string would be the worst option imho. The best option would probably to marshal to a TransformerHandler which does the xsl transformation and builds a dom tree. This tree can then be unmarshalled again. Here is some (untested) sample code showing how this could work:

Source xsl = ...
SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler(xsl);

DOMResult result = new DOMResult();

handler.setResult(result);

marshaller.marshal(inputObject, handler);

transformedObject = unmarshaller.unmarshal(result.getNode());
查看更多
Evening l夕情丶
3楼-- · 2019-02-27 08:06

No, it is not possible. JAXB converts Java to XML or XML to Java. If you have XML, then use XSLT to transform the document. If you have a Java object, then use Java to transform an object. There is no intermediary format.

查看更多
登录 后发表回答