2014-06-11 2 views
1

인터넷에서 xml 파일을 구문 분석하려고합니다 : http://www.yr.no/sted/Norge/Sogn_og_Fjordane/Gloppen/Sandane/varsel.xml. 일부 태그와 같은 여러 값이 있습니다XMLPullParser, 한 태그에서 여러 값을 가져 오는 방법, android

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

답변

2

당신은 getAttributeValue를 사용할 수 있습니다

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

내 코드에서 두 라인을 가지고 있지만 첫 번째는 값의 반환합니다. – user2378812

+1

다른 속성에 액세스하려면 parser.next()를 호출해야합니다. –

+0

일부 코드를 추가했습니다 – user2378812