使 DocumentBuilder.parse 忽略 DTD 引用
当我在此方法中解析 xml 文件(变量 f)时,出现错误
C:\Documents and Settings\joe\Desktop\aicpcudev\OnlineModule\map.dtd(系统找不到指定的路径)
我知道我没有 dtd,也不需要它。 如何将此 File 对象解析为 Document 对象,同时忽略 DTD 引用错误?
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;
}
When I parse my xml file (variable f) in this method, I get an error
C:\Documents and Settings\joe\Desktop\aicpcudev\OnlineModule\map.dtd (The system cannot find the path specified)
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是另一个遇到同样问题的用户: http://forums.sun .com/thread.jspa?threadID=284209&forumID=34
用户 ddssot 在该帖子中说,
该用户进一步提到“如您所见,当解析器命中 DTD 时,会调用实体解析器。我认识到我的DTD 及其特定 ID 并返回一个空的 XML 文档而不是真正的 DTD,从而停止所有验证...”
希望这会有所帮助。
here's another user who got the same issue : http://forums.sun.com/thread.jspa?threadID=284209&forumID=34
user ddssot on that post says
The user further mentions "As you can see, when the parser hits the DTD, the entity resolver is called. I recognize my DTD with its specific ID and return an empty XML doc instead of the real DTD, stopping all validation..."
Hope this helps.
我正在使用 sonarqube,eclipse 的 sonarlint 向我展示了应在不解析外部数据的情况下解析不受信任的 XML (squid:S2755)
我设法使用以下方法解决了它:
I'm working with sonarqube, and sonarlint for eclipse showed me Untrusted XML should be parsed without resolving external data (squid:S2755)
I managed to solve it using:
尝试在 DocumentBuilderFactory 上设置功能:
最终,我认为这些选项特定于解析器实现。 这里是 Xerces2 的一些文档(如果有帮助的话)。
Try setting features on the DocumentBuilderFactory:
Ultimately, I think the options are specific to the parser implementation. Here is some documentation for Xerces2 if that helps.
与 @anjanb 建议的方法类似,
我发现只需返回一个空的InputSource也能正常工作吗?
A similar approach to the one suggested by @anjanb
I found that simply returning an empty InputSource worked just as well?
源 XML(带 DTD)
Java DOM 实现,用于接受上述 XML 作为字符串并删除 DTD 声明
目标 XML(不带 DTD)
Source XML (With DTD)
Java DOM implementation for accepting above XML as String and removing DTD declaration
Destination XML (Without DTD)
我发现了一个问题,DTD 文件与 XML 一起位于 jar 文件中。 我根据此处的示例解决了该问题,如下所示:-
I found an issue where the DTD file was in the jar file along with the XML. I solved the issue based on the examples here, as follows: -
我对这个说法表示怀疑; 您的文档是否包含任何实体引用? 如果是这样,您肯定需要 DTD。
不管怎样,防止这种情况发生的常用方法是使用 XML 目录来定义“map.dtd”的本地路径。
I am suspicious of this statement; does your document contain any entity references? If so, you definitely need the DTD.
Anyway, the usual way of preventing this from happening is using an XML catalog to define a local path for "map.dtd".