我正在尝试解析来自互联网的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;
}
答案 0 :(得分:2)
您可以使用getAttributeValue:
String min = parser.getAttributeValue(null, "minvalue");
String max = parser.getAttributeValue(null, "maxvalue");