gpt4 book ai didi

android - XMLPullParser,如何从一个标签获取多个值,android

转载 作者:行者123 更新时间:2023-11-29 21:07:14 33 4
gpt4 key购买 nike

我正在尝试解析来自互联网的 xml 文件:http://www.yr.no/sted/Norge/Sogn_og_Fjordane/Gloppen/Sandane/varsel.xml .有些标签有多个值,例如:

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

我一直在研究这个问题,但我一次只能使用一个值(代码中排在第一位的那个)。对此有解决方案还是(此)解析器无法实现?

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;
}

最佳答案

您可以使用 getAttributeValue :

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

关于android - XMLPullParser,如何从一个标签获取多个值,android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24171622/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com