解决dom4j解析xml文件时访问DTD文件问题

主要代码是:

Reading codes:

        SAXReader saxReader = new SAXReader(false);
        NullEntityResolver resolver = new NullEntityResolver();
        saxReader.setEntityResolver(resolver);
        Document document = saxReader.read(xmlFileName);

EntityResolver codes:

class NullEntityResolver implements EntityResolver {
    static String emptyDtd = "";

    static ByteArrayInputStream byteIs = new ByteArrayInputStream(emptyDtd.getBytes());

    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        return new InputSource(byteIs);
    }
}


你可能感兴趣的:(dom4j)