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

本文介绍使用Saxon的ProfessionalTransformerFactory替代JDK自带的TransformerFactory来提高XSLT处理XML文件转换效率的方法。通过具体示例代码展示如何集成Saxon并实现显著的性能改进。

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

有严重的性能问题

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

example code:(from stackoverfow:http://stackoverflow.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);

转载于:https://www.cnblogs.com/gyc567/p/5343536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值