0

I am reading XML data from a web service and storing it in an String:

String output;
String dataReceived = "";
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
       dataReceived = dataReceived + output;
}

But when I try to create a DOM Document object from the String, I get a NPE. I am following this examples:

Option 1: How do I load an org.w3c.dom.Document from XML in a string?

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

public void parseXML(String dataReceived) {

//Option 1
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(dataReceived.getBytes()));
System.out.println(document);


//Option 2
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(dataReceived));
Document document = builder.parse(is);
System.out.println(document);

}

But as I said I get a NPE. Why?

[#document: null]
java.lang.NullPointerException

UPDATE: I print the received String at the beginning of parseXML function and this is what I get

Received data: <?xml version="1.0" encoding="UTF-8"?><GeocodeResponse> <status>OK</status> <result> <type>street_address</type> <formatted_address>Calle de José Abascal, 1, 28003 Madrid, Spain</formatted_address> <address_component> <long_name>1</long_name> <short_name>1</short_name> <type>street_number</type> </address_component> <address_component> <long_name>Calle de José Abascal</long_name> <short_name>Calle de José Abascal</short_name> <type>route</type> </address_component> <address_component> <long_name>Madrid</long_name> <short_name>Madrid</short_name> <type>locality</type> <type>political</type> </address_component> <address_component> <long_name>Madrid</long_name> <short_name>M</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>Comunidad de Madrid</long_name> <short_name>Comunidad de Madrid</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>Spain</long_name> <short_name>ES</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>28003</long_name> <short_name>28003</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>40.4387423</lat> <lng>-3.7022840</lng> </location> <location_type>ROOFTOP</location_type> <viewport> <southwest> <lat>40.4373933</lat> <lng>-3.7036330</lng> </southwest> <northeast> <lat>40.4400913</lat> <lng>-3.7009350</lng> </northeast> </viewport> </geometry> <partial_match>true</partial_match> <place_id>ChIJP58jD_YoQg0RPwBTvmkE2qQ</place_id> </result></GeocodeResponse>

UPDATE 2:

As minus has said that is not the part that launches the NPE. It is the following code. I have thought as when printing System.out.println(document); it shows null, maybe the parse was erroneous. I will take a look at this code. Thanks.

 XPath xPath = XPathFactory.newInstance().newXPath();

   Node nodeLat = (Node) xPath.evaluate("//geometry/location/lat",
                        document, XPathConstants.NODE);
   Node nodeLng = (Node) xPath.evaluate("//geometry/location/lng",
                        document, XPathConstants.NODE);

   System.out.println("n " + nodeLat);
   latLong[0] = Double.parseDouble(nodeLat.getNodeValue());
   latLong[1] = Double.parseDouble(nodeLng.getNodeValue());

UPDATE 3

Just BTW The correct way to read the node's value is:

String expression = "//geometry/location/lat";
Node node = (Node) xPath.compile(expression).evaluate(document, XPathConstants.NODE);
System.out.println("latitude: " + node.getTextContent());

And thanks again

Community
  • 1
  • 1
v8rs
  • 197
  • 4
  • 17
  • 1
    Have you printed out `dataReceived` to verify it's populated? If it is, try just translating that to XML via a file and see if it's valid. – Dan W May 08 '17 at 19:46
  • @Dan W, yes, the String is populated. OK, I will try – v8rs May 08 '17 at 19:47
  • give an example of input that fails. – njzk2 May 08 '17 at 19:59
  • @njzk Updated with an exmple of a String that fails – v8rs May 08 '17 at 20:07
  • 1
    By the way; `getBytes(StandardCharsets.UTF_8)`. Looks like debugging is needed. Check that you close things too, check the line of the NPE; is it really on document. – Joop Eggen May 08 '17 at 20:09
  • `[#document: null]` is a document and not a null reference, so your parse succeeded, post the exact line which throws the NPE. – minus May 08 '17 at 20:15
  • Instead of storing the XML in a String, why not just create an InputSource from your original Reader and parse that into a Document? – VGR May 08 '17 at 20:26
  • @minus. Oh, you are right. I updated the question with the code that launches the error (when trying to convert null value to double) and why I though the parse was erroneous – v8rs May 08 '17 at 20:29
  • @minus. But it isn't strange that when I print the document it shows null? – v8rs May 08 '17 at 20:40
  • Your parse is correct and your XML is a `FirstChild` of `Document`, not `document` it-self. Check what is in `document.getFirstChild()` - your XML is there. – Vadim May 08 '17 at 23:34

0 Answers0