user2378812
user2378812

Reputation: 59

XMLPullParser, how to get multiple values from one tag, android

I'm trying to parse an xml file from the internet: http://www.yr.no/sted/Norge/Sogn_og_Fjordane/Gloppen/Sandane/varsel.xml. Some tags have multiple values like:

     <precipitation value="0" minvalue="0" maxvalue="0.2"/>

I've been playing around with this, but I can only get one value to work at a time (the one that comes first in my code). Is there a solution to this or is this just not possible with (this) parser?

private ArrayList<NewsItem> parseNews(InputStream in) throws XmlPullParserException, IOException {

ArrayList<NewsItem> newsList = new ArrayList<NewsItem>();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
pullParser.setInput(in, "UTF-8");

int eventType = pullParser.getEventType();

NewsItem item = null;

while (eventType != XmlPullParser.END_DOCUMENT) {String tagName;
     if (eventType == XmlPullParser.START_TAG) {tagName = pullParser.getName();
     if (tagName.equals(TAG_TIME)) {item = new NewsItem();}
     else if (tagName.equals(TAG_TIME)) {
         if (item != null) {
             item.mTime_SV = pullParser.nextText();
             System.out.println("time"+ item.mTime_SV);
         }
     }

     else if (tagName.equals(TAG_TIME_PERIOD)) {
         if (item != null) {
             item.mTime_period_SV = pullParser.getAttributeValue(null, "from");
             System.out.println("period"+ item.mTime_period_SV);
         }
     }

     else if (tagName.equals(TAG_TEMP)) {
        if (item != null) {
            item.mTemp_SV = pullParser.getAttributeValue(null,"value");
            System.out.println("temp " + item.mTemp_SV);
        }
     }


     else if (tagName.equals(TAG_REGEN_MIN)) {
         if (item != null) {
             item.mRegen_min_SV = pullParser.getAttributeValue(null,"minvalue");
             System.out.println("regen min " + item.mRegen_min_SV);
         }
     }

     else if (tagName.equals(TAG_REGEN_MAX)) {
         if (item != null) {
             item.mRegen_max_SV = pullParser.getAttributeValue(null,"maxvalue");
             System.out.println("regen max " + item.mRegen_max_SV);
         }
     }

     else if (tagName.equals(TAG_WIND_DESC)) {
        if (item != null) {
            item.mWind_desc_SV = pullParser.getAttributeValue(null,"name");
            System.out.println("wind " + item.mWind_desc_SV);
        }
     }

    else if (tagName.equals(TAG_WIND_RICHTING)) {
        if (item != null) {
            item.mWind_richting_SV = pullParser.getAttributeValue(null,"name");
            System.out.println("wind richting " + item.mWind_richting_SV);
        }
    }
}

else if (eventType == XmlPullParser.END_TAG) { tagName = pullParser.getName();
    if (tagName.equals(TAG_TIME)) {
        newsList.add(item);
        item = null;
    }
}

eventType = pullParser.next();
}

return newsList;
 }

Upvotes: 1

Views: 1146

Answers (1)

Green goblin
Green goblin

Reputation: 9996

You can use getAttributeValue:

String min = parser.getAttributeValue(null, "minvalue"); 
String max = parser.getAttributeValue(null, "maxvalue"); 

Upvotes: 2

Related Questions