凝香小筑

岂能尽如人意 但求无愧于心

原创 Do not resolve DTD files when dom4j read xml file收藏

"dom4j, dtd, EntityResolver, 未找到外部实体, 慢"

Using dom4j to read xml file, it always resolve external or internal DTDs, then it makes reading slow or raise exception. I've found a way to resolve this.

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);
    }
}

发表于 @ 2007年06月20日 16:36:00|评论(loading...)

新一篇: Delphi编写ActiveForm,TPopupMenu不能在同一进程的两个IE中正常显示的问题 | 旧一篇: 问题:Required java version : 1.41......

Csdn Blog version 3.1a
Copyright © 书生