Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to configure java.xml.transform.transformer in spring

Tags:

spring

How can i configure java.xml.transform.Transformer in spring framework ? I need instance of transformer to transform xml to text via xslt. Hence it is critical that the configured transformer should have knowledge of xslt stylesheet. I am not using this in a web project, rather i am using this in a standalone java program.

like image 546
Jimm Avatar asked Jan 23 '23 00:01

Jimm


1 Answers

Well, the Java to configure a Transformer is like this:

Source stylesheetSource = new StreamSource(new File("/path/to/my/stylesheet.xslt"));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(stylesheetSource);

If you really want to do this purely in Spring XML, this is the equivalent:

<bean id="stylesheetSource" class="javax.xml.transform.stream.StreamSource">
    <property name="systemId" value="/path/to/my/stylesheet.xslt"/>
</bean>

<bean id="transformerFactory" class="javax.xml.transform.TransformerFactory" factory-method="newInstance"/>

<bean id="transformer" factory-bean="transformerFactory" factory-method="newTransformer">
    <constructor-arg ref="stylesheetSource"/>
</bean>
like image 175
skaffman Avatar answered Feb 01 '23 11:02

skaffman