[每日一学]apache camel|XSLT|SAXON

时间:2023-03-10 05:12:59
[每日一学]apache camel|XSLT|SAXON

最近工作中,用到xslt文件来描述和配置xml文件的转换规则和业务逻辑,开始用jdk自带的TransformerFactory,

有严重的性能问题

后来用Saxon 的com.saxonica.config.ProfessionalTransformerFactory,性能提高了10以上。

example code:(from stackoverfow:http://*.com/questions/5516580/using-saxon-and-xslt-to-transform-jdom-xml-documents)

// Get a TransformerFactory
System.setProperty("javax.xml.transform.TransformerFactory",
"com.saxonica.config.ProfessionalTransformerFactory");
TransformerFactory tfactory = TransformerFactory.newInstance();
ProfessionalConfiguration config = (ProfessionalConfiguration)((TransformerFactoryImpl)tfactory).getConfiguration(); // Get a SAXBuilder
SAXBuilder builder = new SAXBuilder(); //Build JDOM Document
Document toTransform = builder.build(inputFileHandle); //Give it a Saxon wrapper
DocumentWrapper docw = new DocumentWrapper(toTransform, inputHandle.getAbsolutePath(), config); // Compile the stylesheet
Templates templates = tfactory.newTemplates(new StreamSource(styleSheet));
Transformer transformer = templates.newTransformer(); // Now do a transformation
ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);
transformer.transform(docw, new StreamResult(outStream)); ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
Document transformed = builder.build(inStream);