java - spring - xsl transformation dtd not found错误

时间:2012-12-20 15:18:59

标签: java spring xslt transformation dtd

我有一个使用xsl转换xml文件的代码,我的代码安静如下。我的问题是,当我运行执行点时,它会给我以下错误。

StackTrace: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: /home/app/myapp/bin/xhtml11-flat.dtd (No such file or directory)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:720)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
    at com.core.util.XmlUtils.transform(XmlUtils.java:151)
    at com.core.util.XmlUtils.transform(XmlUtils.java:147)

长话短说,它试图在我执行应用程序的bin目录中找到dtd文件。

/home/app/myapp/bin/xhtml11-flat.dtd

我有xhtml11-flat.dtd文件,如果我在bin目录中复制此文件它工作正常,而不是bin目录我想从classpath加载它任何想法我怎么能用最小的变化实现这个? 我不知道它在哪里是laoding .dtd代码,所以我可以在其中设置我的路径。

//Execution Point
function transform(){
    Templates templates = getTemplates();
    StringWriter result = new StringWriter();
    XmlUtils.transform(templates.newTransformer(), input, new StreamResult(result));

    ...
}

private Templates getTemplates() throws Exception {
    if (templates == null) {
        templates = XmlUtils.createTemplates(XslRdcSourceDocTransformer.class.getResourceAsStream("/xsl/" + getXslFileName()));
    }
    return templates;
}


public static Templates createTemplates(InputStream stream) throws Exception {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    return tfactory.newTemplates(new StreamSource(stream));
}

1 个答案:

答案 0 :(得分:4)

您的xml文件可能包含doctype声明,其中包含dtd的相对路径:

<!DOCTYPE html SYSTEM "xhtml11-flat.dtd">

变换器api尝试将此路径解析为java程序的当前工作目录。要自定义路径的解析方式,您需要实现EntityResolver。这个EntityResolver可以返回InputSource,指的是从类路径加载的dtd的副本。

public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException {
    if ("xhtml11-flat.dtd".equals(systemId)) {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputSource is = new InputSource();
        is.setSystemId(systemId);
        is.setByteStream(cl.getResourceAsStream("/com/example/dtd/xhtml11-flat.dtd"));
        return is;
    } else {
        return null;
    }
}

如何使用此类取决于转换的源类型。对于DOMSource,您必须配置DocumentBuilder

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);

DocumentBuilder builder = factory.newDocumentBuilder();
DocumentBuilder builder = ...
builder.setEntityResolver(entityResolver);

Source source = new DOMSource(builder.parse(inputStream));

对于SAXSource设置位于XMLReader实例:

SAXParserFactory factory1 = SAXParserFactory.newInstance();
factory1.setValidating(false);
factory1.setNamespaceAware(true);

SAXParser parser = factory1.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
xmlreader.setEntityResolver(entityResolver);

Source source = new SAXSource(xmlreader, new InputSource(stream));

无论源类型如何,转换的代码都是相同的,并且应该与您XmlUtils类中当前的代码类似:

Templates templates = ...
Result result = new StreamResult(...);
Transformer transformer = templates.newTransformer();
transformer.transform(source, result);