0

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!

Nano
  • 57
  • 1
  • 9
  • The values inside the temperature tag are attributes with a name and you should read them as such not as a tag. – Juan May 17 '18 at 16:22
  • @Juan thanks for your reply, I tried to use getAttribute() but I wasn't able to do it correctly should I use getElementsByTagName("temprature").item(0).getAttribute("value").getTextContent()); Because it didn't work – Nano May 17 '18 at 16:25
  • In this answer https://stackoverflow.com/questions/4138754/getting-an-attribute-value-in-xml-element they do it this way: System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue()); – Juan May 17 '18 at 16:33
  • @Juan Could you please check my edit. I tried item(0,1,2) and all the same respone. – Nano May 17 '18 at 16:50

1 Answers1

0

This answer is based on this question
In this way you can get the value of the temperature. I have added more steps so you can check the intermediate values using the debugger.
From the same attrs you can get the unit the temperature is meassured in.

Note the method name is missleading because it doesn't return anything. It's only puropse is to show the code for accessing the temperature value.

Imports:

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

The method:

public void getTemperature() throws SAXException, IOException, ParserConfigurationException {

        String xml = "<current>\r\n" + 
        "<city id=\"2643743\" name=\"London\">\r\n" + 
        "<coord lon=\"-0.13\" lat=\"51.51\"/>\r\n" + 
        "<country>GB</country>\r\n" + 
        "<sun rise=\"2017-01-30T07:40:36\" set=\"2017-01-30T16:47:56\"/>\r\n" + 
        "</city>\r\n" + 
        "<temperature value=\"280.15\" min=\"278.15\" max=\"281.15\" unit=\"kelvin\"/>\r\n" + 
        "<humidity value=\"81\" unit=\"%\"/>\r\n" + 
        "<pressure value=\"1012\" unit=\"hPa\"/>\r\n" + 
        "<wind>\r\n" + 
        "<speed value=\"4.6\" name=\"Gentle Breeze\"/>\r\n" + 
        "<gusts/>\r\n" + 
        "<direction value=\"90\" code=\"E\" name=\"East\"/>\r\n" + 
        "</wind>\r\n" + 
        "<clouds value=\"90\" name=\"overcast clouds\"/>\r\n" + 
        "<visibility value=\"10000\"/>\r\n" + 
        "<precipitation mode=\"no\"/>\r\n" + 
        "<weather number=\"701\" value=\"mist\" icon=\"50d\"/>\r\n" + 
        "<lastupdate value=\"2017-01-30T15:50:00\"/>\r\n" + 
        "</current>";


        Document doc = DocumentBuilderFactory
                .newInstance()
                .newDocumentBuilder()
                .parse(new InputSource (new StringReader(xml)));

        NodeList errNodes=doc.getElementsByTagName("current");
        if(errNodes.getLength()>0){
            Element err=(Element) errNodes.item(0);
            NodeList temps = err.getElementsByTagName("temperature");
            Node temp = temps.item(0);
            NamedNodeMap attrs = temp.getAttributes();
            Node val = attrs.getNamedItem("value");
            String strValue = val.getNodeValue();
            System.out.println(strValue);
        }
}
Juan
  • 5,525
  • 2
  • 15
  • 26