Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Xalan (XML + XSL) Transformation

  Asked By: Matilda    Date: Oct 30    Category: Java    Views: 1397
  

Please, what is wrong in this code?

It is a class to transform a XML file using a XSLT file.
It just works with the simple example of Apache Xalan.
With other files, I get this message:

javax.xml.transform.TransformerConfigurationException:
javax.xml.transform.TransformerException: java.lang.ClassCastException

I use Java 1.4.02, Apache Xalan 2.5.2 and Apache Xerces 2.6.0.


---------
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

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

public class ProcessorXSLT {

static {
System.setProperty(
"javax.xml.transform.TransformerFactory",

"org.apache.xalan.processor.TransformerFactoryImpl");

System.setProperty(
"javax.xml.parsers.DocumentBuilderFactory",

"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

System.setProperty(
"javax.xml.parsers.SAXParserFactory",

"org.apache.xerces.jaxp.SAXParserFactoryImpl");
}

public static void transform(String psArquivoXML, String
psArquivoXSL)
throws TransformerException, TransformerConfigurationException,
FileNotFoundException
{
// Create a transform factory instance.
TransformerFactory tfactory = TransformerFactory.newInstance();

InputStream xslIS = new BufferedInputStream(new
FileInputStream(psArquivoXSL));
StreamSource xslSource = new StreamSource(xslIS);
// Note that if we don't do this, relative URLs can not be
resolved correctly!
xslSource.setSystemId(psArquivoXSL);

// Create a transformer for the stylesheet.
Transformer transformer = tfactory.newTransformer(xslSource);

InputStream xmlIS = new BufferedInputStream(new
FileInputStream(psArquivoXML));
StreamSource xmlSource = new StreamSource(xmlIS);
// Note that if we don't do this, relative URLs can not be
resolved correctly!
xmlSource.setSystemId(psArquivoXML);

// Transform the source XML to System.out.
transformer.transform( xmlSource, new StreamResult(new
OutputStreamWriter(System.out)));
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Xalan (XML + XSL) Transformation Or get search suggestion and latest updates.




Tagged: