32

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!

6
  • 4
    Just for clarification: do you have (xmlDoc == null) = true or do you get an empty document ([#document: null])? The parse method should either return a document or throw an exception but never return null... Commented Jan 7, 2010 at 8:39
  • 1
    hi Adreas, I get [#document: null] Commented Jan 7, 2010 at 8:53
  • 14
    [#document: null] does not mean a null document, that's just Document's badly-written toString() output. Commented Jan 7, 2010 at 9:57
  • @Andreas_D where did you find this "never return null" behavior documented? Thx! Commented Jun 6, 2016 at 20:51
  • @WeishiZeng, it's covered by the exhaustive documentation. It throws SAXException if any parse exception occurs; IOException if any I/O error occurs; and IllegalArgumentException if the input is null. Even if that did not exhaust all the error cases, it's conventional in the JDK for the @returns documentation to mention null if it's a possible return value. Commented May 8, 2019 at 20:39

1 Answer 1

56

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

Sign up to request clarification or add additional context in comments.

2 Comments

Will this method ever return null or throw exception in those cases? DocumentBuilder.parse(InputStream)
Thanks for letting me know that: xmlDoc.toString() will always be "[#document: null]"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.