2012-07-17 5 views
2

Android에서 XML을 구문 분석하려고합니다. 그래서XmlPullParser Android 용 XML

try { 
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    XmlPullParser xpp = factory.newPullParser(); 
    URL url = new URL("http://services.tvrage.com/feeds/last_updates.php?hours=" + hours); 
    InputStream stream = url.openStream(); 
    xpp.setInput(stream, null); 
    int eventType = xpp.getEventType(); 

    while (eventType != XmlPullParser.END_DOCUMENT) { 
     if (eventType == XmlPullParser.START_DOCUMENT) { 
     } 
     else if (eventType == XmlPullParser.END_DOCUMENT) { 
     } 
     else if (eventType == XmlPullParser.START_TAG) { 
      if (xpp.getName().equalsIgnoreCase("id")) { 
       showIds.add(Integer.parseInt(xpp.nextText())); 
      } 
      else if (eventType == XmlPullParser.END_TAG) { 
      } 
      else if (eventType == XmlPullParser.TEXT) { 
      } 
      eventType = xpp.next(); 
     } 
    } 
} catch 

... 그리고이 실제 코드

<updates at="1342481821" found="616" sorting="latest_updates" showing="Last 4D"> 
<show> 
    <id> 
     3039 
    </id> 
    <last> 
     -14508 
    </last> 
    <lastepisode> 
     -14508 
    </lastepisode> 
</show> 
<show> 
    <id> 
     30612 
    </id> 
    <last> 
     -13484 
    </last> 
    <lastepisode> 
     163275 
    </lastepisode> 
</show> 

등등 .... : 아래는 XML의 일례의 비트가

결과가 나타나지 않으며 eventType은 디버거에서 항상 0입니다. 예제 URL은 브라우저에서 정상적으로 작동합니다. 어떤 아이디어?

+1

친구 난 그의 혼합을 사용하여 만든이 http://xjaphx.wordpress.com/2011/10/16/android-xml-adventure-parsing-xml-data-with-xmlpullparser/ –

+0

시도 가이드와 Lars Vogel로부터의 하나. http://www.vogella.com/articles/AndroidXML/article.html – Crunch

답변

3

eventType == XmlPullParser.START_TAG 일 때 xpp.next() 만 실행합니다. xpp.next을 한 줄 아래로 이동하면 항상 실행됩니다.

while (eventType != XmlPullParser.END_DOCUMENT) { 
     if (eventType == XmlPullParser.START_DOCUMENT) { 
     } 
     else if (eventType == XmlPullParser.END_DOCUMENT) { 
     } 
     else if (eventType == XmlPullParser.START_TAG) { 
      if (xpp.getName().equalsIgnoreCase("id")) { 
       showIds.add(Integer.parseInt(xpp.nextText())); 
      } 
      else if (eventType == XmlPullParser.END_TAG) { 
      } 
      else if (eventType == XmlPullParser.TEXT) { 
      } 
      // eventType = xpp.next(); <-- remove it 
     } 
     eventType = xpp.next(); // <-- move here 
    } 
} catch 
+0

트릭을 해 주셔서 감사합니다. – Crunch