"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);
}
}
本文介绍了一种使用dom4j解析XML文件时避免解析DTD的方法,通过自定义EntityResolver来提高读取速度并防止外部实体引发的问题。
6454

被折叠的 条评论
为什么被折叠?



