【发布时间】:2011-01-02 09:30:35
【问题描述】:
我正在修复有关 DocumentBuilder.parse 的现有代码的错误。我有以下代码:
String theOutput;
theOutput = response.encodeURL(prefix + "/include/sampleForConversion.jsp?" + request.getQueryString();
StreamSource xmlSource = new StreamSource(new URL(theOutput).openStream(), "http://sampleApps.net/static/dataDef1.1.dtd");
Document xmlDoc = dBuilder.parse(xmlSource.getInputStream());
我不明白为什么我得到了 xmlDoc 的空值,尽管我有有效的 theOutput 和 xmlSource 变量值。请帮忙。
谢谢!
【问题讨论】:
-
只是为了澄清:你有@987654323@还是一个空文件(
[#document: null])? parse 方法应该要么返回一个文档,要么抛出一个异常,但永远不会返回null... -
嗨,Adreas,我得到 [#document: null]
-
[#document: null]并不意味着一个空文档,那只是Document的写得不好的toString()输出。 -
@Andreas_D 您在哪里发现这种“永不返回 null”的行为记录在案?谢谢!
-
@WeishiZeng,它被exhaustive documentation覆盖。如果发生任何解析异常,它会抛出
SAXException;IOException如果发生任何 I/O 错误;如果输入为null,则为IllegalArgumentException。即使这并没有穷尽所有错误情况,在 JDK 中,@returns文档中通常会提及null(如果它是可能的返回值)。