Transform From one JAXB object to another using XSLT template

前端 未结 2 1038
旧时难觅i
旧时难觅i 2021-01-23 03:56

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

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-23 04:55

    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());
    

提交回复
热议问题