本文介绍了DocumentBuilder.parse(InputStream)返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修复有关DocumentBuilder.parse的现有代码的错误。我有以下代码:

I am fixing a bug on an existing code concerning DocumentBuilder.parse. I have the below code:

 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的空值虽然我有输出和xmlSource变量的有效值。请帮忙。

I dont understand why i am getting a null value for xmlDoc though I have valid values for theOutput and xmlSource variables. Please help.

谢谢!

推荐答案

很有可能流解析正确,只因为
xmlDoc.toString()将始终为[#document:null]。这并不表示, DOM树是空的。如果文档有一些节点(子节点),请先检查。

There is a good chance that the stream has been parsed correct, just becausexmlDoc.toString() will always be "[#document: null]". This doesn't indicate, that the DOM tree is empty. Please check first, if the document has some nodes (children).

如果DOM真的是空的,那么我先打印输入流的内容到conso le(可能 xmlSource.getInputStream()。toString()已经返回内容)以检查内容是否格式正确,仔细检查dtd文件是否可访问(浏览器)最后,将XML文档和dtd转储到文件中以检查XML内容是否有效。

If the DOM really was empty, then I'd first print the content of the input stream to the console (maybe xmlSource.getInputStream().toString() already return the content) to check if the content is well-formed, double-check if the dtd file was accessible (browser) and finally, dump the XML document and the dtd into files to check if the XML content is valid.

啊,等一下,我认为第二个参数是DTD文件的URI,但字符串是xml文档的systemId( public StreamSource(InputStream inputStream,String systemId))。也许这是一个问题 - StreamSource类将使用此URI来解析相对URI(如您的DTD)。

Ahh, wait a second, I thought the second parameter was the URI of the DTD file, but the string is the systemId of the xml document (public StreamSource(InputStream inputStream, String systemId)). Maybe that's a problem - the StreamSource class will use this URI to resolve relative URIs (like your DTD).

这篇关于DocumentBuilder.parse(InputStream)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:59