使 DocumentBuilder.parse 忽略 DTD 引用 [英] Make DocumentBuilder.parse ignore DTD references
本文介绍了使 DocumentBuilder.parse 忽略 DTD 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我用这种方法解析我的 xml 文件(变量 f)时,我得到一个错误
When I parse my xml file (variable f) in this method, I get an error
C:Documents and SettingsjoeDesktopaicpcudevOnlineModulemap.dtd(系统找不到指定的路径)
C:Documents and SettingsjoeDesktopaicpcudevOnlineModulemap.dtd (The system cannot find the path specified)
我知道我没有 dtd,也不需要它.如何在忽略 DTD 引用错误的同时将此 File 对象解析为 Document 对象?
I know I do not have the dtd, nor do I need it. How can I parse this File object into a Document object while ignoring DTD reference errors?
private static Document getDoc(File f, String docId) throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
return doc;
}
推荐答案
与 @anjanb
builder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
if (systemId.contains("foo.dtd")) {
return new InputSource(new StringReader(""));
} else {
return null;
}
}
});
我发现简单地返回一个空的 InputSource 也能正常工作?
I found that simply returning an empty InputSource worked just as well?
这篇关于使 DocumentBuilder.parse 忽略 DTD 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文