0

Thanks for reviewing my question.

I working with saxon java api as XSLT processor. Getting difficulty to catch the exception return by saxon jar file.

I am able to print the javax exception. But need to get the exception in string returned by saxon.

Below function i am using to transform the xml:

import java.io.File;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {

    /**
     * Simple transformation method.
     * @param sourcePath - Absolute path to source xml file.
     * @param xsltPath - Absolute path to xslt file.
     * @param resultDir - Directory where you want to put resulting files.
     */
    public static void simpleTransform(String sourcePath, String xsltPath,
                                       String resultDir) {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        try {
            Transformer transformer =
                tFactory.newTransformer(new StreamSource(new File(xsltPath)));

            transformer.transform(new StreamSource(new File(sourcePath)),
                                  new StreamResult(new File(resultDir)));
        } catch (Exception e) {
            e.message();
        }
    }

    public static void main(String[] args) {
        //Set saxon as transformer.
        System.setProperty("javax.xml.transform.TransformerFactory",
                           "net.sf.saxon.TransformerFactoryImpl");

        simpleTransform("d:/project/hob/AppModule.xml",
                        "d:/project/hob/create-fragment.xslt", "C:/");

    }
}

Could you guys suggest to get the saxon exception into the string. Below is the example of exception returned saxon jar file.

SXJE0008: cannot convert xs:yearMonthDuration to the required Java type

Thanks, Deepak

  • Have you checked if [Exception.getCause()](http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getCause()) holds the Saxon exception? – wonderb0lt Apr 27 '15 at 10:12
  • @wonderb0lt: I have tried getcause() but its returning not returning the jar file exception. – Deepak singh May 01 '15 at 07:16

1 Answers1

0

This seems to be a very basic Java question.

The typical way to capture the message text from a Java exception is:

String err = null;
try {
  ....
} catch (Exception e) {
  err = e.getMessage();
}

There's nothing specific to Saxon about this.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • This isn't always so simple. I'm encountering an issue where the newTransformer call doesn't pass up any exceptions, since they'll be contained in an errorListener provided w/ factory.setErrorListener. But then, we can't throw an exception on the spot unless we look through this list of strings for a fatal one (and not a warning). That's rough. – Alkanshel Aug 07 '15 at 23:16