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!

Pink Angel
  • 391
  • 2
  • 6
  • 15
  • 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`... – Andreas Dolk Jan 07 '10 at 08:39
  • 1
    hi Adreas, I get [#document: null] – Pink Angel Jan 07 '10 at 08:53
  • 14
    `[#document: null]` does not mean a null document, that's just `Document`'s badly-written `toString()` output. – skaffman Jan 07 '10 at 09:57
  • @Andreas_D where did you find this "never return null" behavior documented? Thx! – Weishi Z Jun 06 '16 at 20:51
  • @WeishiZeng, it's covered by the [exhaustive documentation](https://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilder.html#parse-java.io.InputStream-). 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. – 0xbe5077ed May 08 '19 at 20:39
  • I found that using getDocumentElement() should allow you to see some content for the DOMElement instead of that document#null – ctatro85 Oct 25 '19 at 16:06

1 Answers1

54

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

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Will this method ever return null or throw exception in those cases? `DocumentBuilder.parse(InputStream)` – Weishi Z Jun 06 '16 at 20:49
  • 3
    Thanks for letting me know that: xmlDoc.toString() will always be "[#document: null]" – Emily Apr 13 '21 at 19:40