0

Java - spring - 使用 dtd 作为实体解析器的 xsl 转换。

我有以下方法

 private Source resolveDTDEntity(Reader input, final String dtdName) throws ParserConfigurationException, SAXException, IOException {

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

        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        //Satisfying dtd dependency
        docBuilder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                if (systemId.contains(dtdName)) {
                    InputStream dtdStream = XslRdcSourceDocTransformer.class
                            .getResourceAsStream("/dtds/" + dtdName);
                    return new InputSource(dtdStream);
                } else {
                    return null;
                }
            }
        });

        //Exception on following line
        return new DOMSource(docBuilder.parse(new ReaderInputStream(input))); 
    }

我得到以下异常

StackTrace: org.xml.sax.SAXParseException: Premature end of file.
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
        at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
        at com.service.XslSourceDocTransformer.resolveDTDEntity(XslSourceDocTransformer.java:137)
        at com.service.XslSourceDocTransformer.transform(XslSourceDocTransformer.java:56)
        at com.service.ContentProcessor.process(ContentProcessor.java:127)
        at com.lexisnexis.job.LexisNexisJob.execute(LexisNexisJob.java:67)
        at com.core.job.JobController$JobExecutor.run(JobController.java:367)
        at com.core.job.JobController$JobThread.run(JobController.java:245)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)

Exception: Premature end of file
4

1 回答 1

0

发生此错误“tackTrace: org.xml.sax.SAXParseException: Premature end of file”。因为我正在从磁盘读取多个 xml 文件,其中一个是空的,这是导致此错误的原因。

我使用了以下语句

docBuilder.parse(new InputSource(reader))
于 2012-12-24T14:52:11.510 回答