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