I am trying to parse the value of the temperature in this XML response.
<current>
<city id="2643743" name="London">
<coord lon="-0.13" lat="51.51"/>
<country>GB</country>
<sun rise="2017-01-30T07:40:36" set="2017-01-30T16:47:56"/>
</city>
<temperature value="280.15" min="278.15" max="281.15" unit="kelvin"/>
<humidity value="81" unit="%"/>
<pressure value="1012" unit="hPa"/>
<wind>
<speed value="4.6" name="Gentle Breeze"/>
<gusts/>
<direction value="90" code="E" name="East"/>
</wind>
<clouds value="90" name="overcast clouds"/>
<visibility value="10000"/>
<precipitation mode="no"/>
<weather number="701" value="mist" icon="50d"/>
<lastupdate value="2017-01-30T15:50:00"/>
</current>
I was able to parse the country for example because there is only one value inside the tag,but what I want is only the temperature value which is 280.15 in this example.
My code for parsing is:
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource (new StringReader(response.toString())));
NodeList errNodes=doc.getElementsByTagName("current");
if(errNodes.getLength()>0){
Element err=(Element) errNodes.item(0);
System.out.println(err.getElementsByTagName("country").item(0).getTextContent());
}else{
}
When I change the getElementByTagName to temperature it doesn't recognize it, of course because inside the temperature tag there are 4 values and it doesn't know which one to choose and the whole temperature tag structure is different from the country tag structure.
Is there any simple way to parse the value of the temperature only?
Edit: I have changed the the parsing function and now it's like this:
NodeList errNodes=doc.getElementsByTagName("temprature");
if(errNodes.getLength()>0){
// Element err=(Element) errNodes.item(0);
System.out.println(errNodes.item(2).getAttributes().getNamedItem("value").getNodeValue());
But still not able to receive any values!