Android XmlPullParser trouble

85 Views Asked by At

I am having trouble fetching a specific value in an XML value using Android XMLPullParser. Here is a snippet of the XML file containing the information I need,

<parameters applicable-location="point1">
<temperature type="apparent" units="Fahrenheit" time-layout="k-p1h-n1-1">
<value>50</value>
</temperature>
<temperature type="dew point" units="Fahrenheit" time-layout="k-p1h-n1-1">
<value>33</value>
</temperature>

I am new to parsing to XML, but I need to retrieve the

<value>

of the:

<temperature type="apparent" units="Fahrenheit" time-layout="k-p1h-n1-1">

Now to me I would just do something like this:

if(xpp.getName().equalsIgnoreCase("temperature") {

   xpp.next(); //To bring the parser to that value tag

   if(xpp.getName().equalsIgnoreCase("value") {

      weather_apparenttemp.setText(xpp.getName());
   }
}

But it is not working and I feel I am missing or doing something wrong :( any help? Thanks!

1

There are 1 best solutions below

0
On

Try this code:

if(xpp.getName().equalsIgnoreCase("temperature") {
if (xpp.getAttributeValue(null, "type").equals("apparent")){
    xpp.next(); //To bring the parser to that value tag
    if(xpp.getName().equalsIgnoreCase("value") {
        weather_apparenttemp.setText(xpp.getName());
    }
}

This is for retrieve the tag name <value> inside the temperature typed as apparent. If you want to retrieve the value, advance the xpp with xpp.next() inside the second condition and get the text xpp.getText().