0

感谢您查看我的问题。

我使用 saxon java api 作为 XSLT 处理器。很难通过 saxon jar 文件捕获异常返回。

我能够打印 javax 异常。但是需要在 saxon 返回的字符串中获取异常。

下面的函数我用来转换 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:/");

    }
}

你们能否建议将撒克逊异常放入字符串中。下面是异常返回的 saxon jar 文件的示例。

SXJE0008:无法将 xs:yearMonthDuration 转换为所需的 Java 类型

谢谢,迪帕克

4

1 回答 1

0

这似乎是一个非常基本的 Java 问题。

从 Java 异常中捕获消息文本的典型方法是:

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

撒克逊人对此没有什么特别的。

于 2015-05-03T16:02:51.220 回答