小编典典

在XOM中解析XHTML文档时DTD下载错误

java

我试图解析具有声明为使用过渡性dtd的doctype的HTML文档,如下所示:

<!DOCTYPE html PUBLIC“-// W3C // DTD XHTML 1.0 Transitional // EN”“
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
”>

在文档上执行Builder.build时,出现以下异常:

  java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
       at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
       at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
       at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
       at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
       at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
       at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
       at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
       at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
       at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
       at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
       at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
       at nu.xom.Builder.build(Builder.java:1127)
       at nu.xom.Builder.build(Builder.java:1019)

如果我删除文档类型声明,它就可以解析。我可以从浏览器成功下载dtd,它告诉我该URL有效。我不想删除文档类型声明。有没有一种方法告诉构建器不要下载dtd或为其提供备用dtd?


阅读 130

收藏
2020-11-01

共1个答案

小编典典

快速浏览Builder的javadoc
,我想您可以通过采用XMLReader的构造函数提供EntityResolver。我会尽量避免让解析器从Internet下载文件。

2020-11-01