[英]Transforming one JAXB Object to another using XSLT
I found this question, which helped me a bit, but not enough: Transform From one JAXB object to another using XSLT template 我发现了这个问题,这对我有所帮助,但还不够: 使用XSLT模板从一个JAXB对象转换到另一个JAXB对象
What I have is this: 我有的是这个:
What I'm trying is this: 我正在尝试的是这个:
/**
* Transforms one JAXB object into another with XSLT
* @param src The source object to transform
* @param xsltPath Path to the XSLT file to use for transformation
* @return The transformed object
*/
public static <T, U> U transformObject(final T src, final String xsltPath) {
// Transform the JAXB object to another JAXB object with XSLT, it's magic!
// Marshal the original JAXBObject to a DOMResult
DOMResult domRes = Marshaller.marshalObject(src);
// Do something here
SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
StreamSource xsltSrc = new StreamSource(xsltPath);
TransformerHandler th = tf.newTransformerHandler(xsltSrc);
th.setResult(domRes);
}
At this point, I'm puzzled. 在这一点上,我很困惑。 How do I obtain my transformed Document?
我如何获得转换后的文档? From that point, unmarshalling it back to a JAXB object shouldn't be too hard, I think.
从那时起,我认为将其解组回JAXB对象应该不会太难。
As far as I know, there's no way to do this without marshalling, right? 据我所知,没有编组就没办法做到这一点,对吧?
UPDATE UPDATE
Here is a full working example, using Saxon specifically as my XSLT's are using XSLT 2.0: 这是一个完整的工作示例,特别是使用Saxon,因为我的XSLT正在使用XSLT 2.0:
/**
* Transforms one JAXB object into another with an XSLT Source
*
* @param src
* The source (JAXB)object to transform
* @param xsltSrc
* Source of the XSLT to use for transformation
* @return The transformed (JAXB)object
*/
@SuppressWarnings("unchecked")
public static <T, U> U transformObject(final T src, final Source xsltSrc, final Class<U> clazz) {
try {
final JAXBSource jxSrc = new JAXBSource(JAXBContext.newInstance(src.getClass()), src);
final TransformerFactory tf = new net.sf.saxon.TransformerFactoryImpl();
final Transformer t = tf.newTransformer(xsltSrc);
final JAXBResult jxRes = new JAXBResult(JAXBContext.newInstance(clazz));
t.transform(jxSrc, jxRes);
final U res = (U) jxRes.getResult();
return res;
} catch (JAXBException | TransformerException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
You can instantiate the xsltSrc through Source xsltSrc = new StreamSource(new File(...));
您可以通过
Source xsltSrc = new StreamSource(new File(...));
实例化xsltSrc Source xsltSrc = new StreamSource(new File(...));
You could use JAXBSource
and JAXBResult
directly with your transformation. 您可以直接使用
JAXBSource
和JAXBResult
进行转换。
JAXBSource source = new JAXBSource(marshaller, src);
JAXBResult result = new JAXBResult(jaxbContext);
transformer.transform(source, result);
Object result = result.getResult();
For More Information 欲获得更多信息
You can find an example of using JAXB with the javax.xml.transform
APIs on my blog: 您可以在我的博客上找到使用JAXB和
javax.xml.transform
API的示例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.