1

I am trying to parse XML as a String. No matter whether or not I'm using the response from a RESTful call or a simple string, i get a null Document when i parse it. i have found several posts telling me to follow the code (below), yet it doesn't seem to work for me. What have i done wrong?

import org.xml.sax.InputSource;
import org.w3c.dom.Document;
...
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String xml = responseEntity.getBody();
//String xml = "<foo\>";
Document doc = db.parse(new InputSource(new StringReader(xml)));

i followed: In Java, how do I parse XML as a String instead of a file?

David M
  • 2,511
  • 1
  • 15
  • 18

1 Answers1

0

Turns out the debugger showed [#document: null] when in fact it wasn't. i was able to continue on after parse() and find the elements i needed. never seen that before!

David M
  • 2,511
  • 1
  • 15
  • 18
  • I understand your frustration, but your debugger shows `null` when the variable is null. It does not show `[#document: null]`. So clearly, there was a document, just not represented in a way that you expected. What an `org.w3c.dom.Document` instance should print when `toString()` is called is not specified though, and its output in your case is really unhelpfull, but still different from `null`. – GPI Nov 19 '19 at 12:08