Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentBuilder.parse(InputStream) returns null

Tags:

java

xml

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());

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

thanks!

like image 280
Pink Angel Avatar asked Jan 07 '10 07:01

Pink Angel


1 Answers

There is a good chance that the stream has been parsed correct, just because xmlDoc.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).

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.

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).

like image 158
Andreas Dolk Avatar answered Sep 25 '22 23:09

Andreas Dolk