0

I have the following code

private void getdata(String stringfromDate,String stringtoDate ) throws IOException {
StringBuilder retVal = new StringBuilder();
URL oracle = new URL("https://xxxxxxxxxxxxxxxx" + stringfromDate +"&ToDate=" + stringtoDate +"" );
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

String newLine = "\n";
String inputLine;
while ((inputLine = in.readLine()) != null) {
            //System.out.println(inputLine);
retVal.append(inputLine).append(newLine);

}

in.close();

passSting(retVal);

    }

private void passSting(StringBuilder retVal) {
Document doc = null;
try {
doc = loadXMLFromString(retVal.toString());//pull in the XML data into a new doc
System.out.println(doc);
} catch (Exception ex) {
Logger.getLogger(JavaApplication63.class.getName()).log(Level.SEVERE, null, ex);
}
    }

public static org.w3c.dom.Document loadXMLFromString(String xml) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}   

I am passing through retVal to a method so that i can create a doc which can be read as an XML doc, however the doc value appears to be null what am i doing wrong? This is what i get in the console window [#document: null]

Ingram
  • 654
  • 2
  • 7
  • 29
  • 1
    Are you sure there is something in the `StringBuilder`? You have a print statement in the `while`, so I assume you tested that portion. – gonzo Dec 08 '15 at 22:00
  • Yes if i hover above the retVal.toString() i see the expected data ...its just that the doc appears null... – Ingram Dec 08 '15 at 22:06
  • How about if you add `factory.setNamespaceAware(true);` right before creating the `builder`. – gonzo Dec 08 '15 at 22:34
  • @gonzo doc still appears as null, however i have interrogated doc and found that the fNodeValue contains the data..even though doc itself shows null...is this of any use? – Ingram Dec 08 '15 at 22:39
  • Further interrogation shows fNodeName contains the data item tags. – Ingram Dec 08 '15 at 22:46
  • Yeah. Take a look at this [site](http://www.w3schools.com/xml/dom_nodes_get.asp). Very useful stuff. Just skimmed a few links and learned a bunch. – gonzo Dec 08 '15 at 22:59
  • Also [this](http://stackoverflow.com/questions/21762244/why-would-a-dom-document-show-as-null-when-it-is-actually-valid). Seems like the `toString` is null. Try printing `doc==null`. – gonzo Dec 08 '15 at 23:03
  • @gonzo that returns false – Ingram Dec 08 '15 at 23:18
  • which is good. that means the object is not null just the method `toString` returns null. – gonzo Dec 08 '15 at 23:21
  • yes that is very interesting ...hmm – Ingram Dec 08 '15 at 23:22

1 Answers1

1

org.w3c.dom.Document#toString method returns null. The actual object is not null however. If you print doc==null after parsing the String you should see that it returns false instead of true. Hope this helps.

gonzo
  • 2,103
  • 1
  • 15
  • 27
  • @ gonzo, so what should i do so that the contents show up in doc, since doc is being passed to a populate method which takes each and separates them out. – Ingram Dec 09 '15 at 13:57
  • @MrAssistance the data is still in your `doc` object you just don't access the data like you are currently doing. You need to use the API of the `doc`. Try doing something like `doc.getDocumentElement().getTextContent()` – gonzo Dec 09 '15 at 14:13
  • yes that appears to work giving me all the text content, what would i use to get the data like data – Ingram Dec 09 '15 at 19:13
  • @MrAssistance Did you check out that link from the comments? All the [API](http://www.w3schools.com/xml/dom_nodes_access.asp) is in there. Or if you want something a bit more [official](https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html). Or even doing a quick search here in [SO](http://stackoverflow.com/questions/4138754/getting-an-attribute-value-in-xml-element) – gonzo Dec 09 '15 at 19:16