suraj08
suraj08

Reputation: 119

XML transformation using XSLT in Java program

I am trying to transform my XML file using XSLT in java application. Below is my code but it is giving error for classNot found. I tried to resolve this but the method is not in use after jdk 5. Can you please give me better approach to transform XML using XSLT in java program. Below is my code :

import java.io.File;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;


class Transform {
    public static void main(String[] args) throws TransformerException {
    String stylesheetPathname = "C:/abc.xml";
    String inputPathname = "C:/scripts/transform.xslt";
    String outputPathname = "C:/abc_transformed.xml";

    TransformerFactory factory = TransformerFactory.newInstance();
    Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
    Transformer transformer = factory.newTransformer(stylesheetSource);
    Source inputSource = new StreamSource(new File(inputPathname).getAbsoluteFile());
    Result outputResult = new StreamResult(new File(outputPathname).getAbsoluteFile());
    transformer.transform(inputSource, outputResult);
   }
}

Error :

Exception in thread "main" javax.xml.transform.TransformerFactoryConfigurationError: Provider org.apache.xalan.processor.TransformerFactoryImpl not found
at javax.xml.transform.TransformerFactory.newInstance(TransformerFactory.java:107)
at Transform.main(Transform.java:18)


Caused by: java.lang.ClassNotFoundException:org.apache.xalan.processor.TransformerFactoryImpl
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at javax.xml.transform.FactoryFinder.getProviderClass(FactoryFinder.java:119)
at javax.xml.transform.FactoryFinder.newInstance(FactoryFinder.java:182)
at javax.xml.transform.FactoryFinder.findJarServiceProvider(FactoryFinder.java:364)
at javax.xml.transform.FactoryFinder.find(FactoryFinder.java:286)
at javax.xml.transform.TransformerFactory.newInstance(TransformerFactory.java:101

Upvotes: 0

Views: 4541

Answers (3)

suraj08
suraj08

Reputation: 119

I have added xml-apis.jar,xercesImpl.jar and the error is resolved. Thanks for your efforts and help.

Upvotes: 2

tmarwen
tmarwen

Reputation: 16354

It should be a service declaration somewhere in you code base, or it should have been falled back to the default xml factory.

A short answer would be to provide the xalan jar as classpath property. More informations can be found here.

Upvotes: 0

kensen john
kensen john

Reputation: 5519

Make sure the jar file for that particular class file is included in your classpath.

It is part of the Xalan Jar

You can find the latest jar file here

http://xml.apache.org/xalan-j/

This should solve the ClassNotFoundException

Upvotes: 0

Related Questions