help with Java implementation of Saxon
Added by Anonymous over 19 years ago
Legacy ID: #3246186 Legacy Poster: Jim Neff (jim_neff)
Greetings, I've been using Saxon for a few months in a vb.net application and now I am trying to create a java class to implement Saxon and need help with the Java code (i am new to Java). This class will have two parameters. The first one for the source XML document, the other the stylesheet document. It's out put will be the transformed document (nevermind errors right now). Here is my code so far: import java.io.; import java.io.Reader; import javax.xml.transform.; import javax.xml.transform.stream.; import net.sf.saxon.; public class ApplyStylesheet { public String apply(String style, String source) { System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); // source doc Reader sourceXML = StringReader(source); StreamSource sourceStreamSource = StreamSource(sourceXML); // stylesheet doc Reader stylesheetDoc = StringReader(style); StreamSource styleStreamSource = StreamSource(stylesheetDoc); TransformerFactory transformerfactory = TransformerFactory.newInstance(); Transformer transformer = transformerfactory.newTransformer(styleStreamSource); java.io.ByteArrayOutputStream byteArrayStream; StreamResult resultStream; resultStream.setOutputStream(byteArrayStream); transformer.transform(sourceXML, resultStream); return byteArrayStream.toString(); } } when I compile from the command line using this statement: C:\temp\stylesheetTest>c:\j2sdk1.4.2_06\bin\javac -classpath c:\saxon\saxon8.jar ApplyStylesheet.java I get this error: ApplyStylesheet.java:14: cannot resolve symbol symbol : method StringReader (java.lang.String) location: class ApplyStylesheet Reader sourceXML = StringReader(source); ^ What am I doing wrong?
Replies (2)
RE: help with Java implementation of Saxon - Added by Anonymous over 19 years ago
Legacy ID: #3246583 Legacy Poster: Michael Kay (mhkay)
You want Reader sourceXML = new StringReader(source); Michael Kay
RE: help with Java implementation of Saxon - Added by Anonymous over 19 years ago
Legacy ID: #3246584 Legacy Poster: Jim Neff (jim_neff)
I finally got it to compile with this source code: import java.io.; import java.io.Reader; import java.io.StringReader; import javax.xml.transform.; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import net.sf.saxon.*; import net.sf.saxon.TransformerFactoryImpl; public class ApplyStylesheet { public static void main(String[] args) throws javax.xml.transform.TransformerException { String stylesheet = "<xsl:transform xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0' xmlns:xs='http://www.w3.org/2001/XMLSchema'>"; stylesheet = stylesheet + "<xsl:output method='text'/><xsl:template match='docroot'><xsl:value-of select='test_element' />"; stylesheet = stylesheet + "</xsl:template></xsl:transform>"; String xmlsourcedoc = "<?xml version='1.0' encoding='UTF-8'?><docroot><test_element>Hello World!</test_element></docroot>"; String myoutput = apply(stylesheet, xmlsourcedoc); System.out.println(myoutput); } static String apply(String style, String source) throws javax.xml.transform.TransformerException { java.lang.System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(source); javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(style); javax.xml.transform.Result result = new javax.xml.transform.stream.StreamResult(); javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance( ); javax.xml.transform.Transformer trans = transFact.newTransformer(xsltSource); trans.transform(xmlSource, result); return result.toString(); } } When I run this from the command line : java ApplyStylesheet I get this: Exception in thread "main" javax.xml.transform.TransformerFactoryConfigurationError: Provider net.sf.saxon.TransformerFactoryImpl could not be instantiated: jav a.lang.NullPointerException Can someone show me what I'm doing wrong please? Thanks, Jim
Please register to reply