2014-04-25 1 views

답변

0

당신은 ..., 많은 감사를이 예

try { 
     URL url = new URL("http://news.yahoo.com/rss/entertainment"); 

     XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
     factory.setNamespaceAware(false); 
     XmlPullParser xpp = factory.newPullParser(); 

      // We will get the XML from an input stream 
     xpp.setInput(getInputStream(url), "UTF_8"); 

      /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag. 
      * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag. 
      * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...." 
      * so we should skip the "<title>" tag which is a child of "<channel>" tag, 
      * and take in consideration only "<title>" tag which is a child of "<item>" 
      * 
      * In order to achieve this, we will make use of a boolean variable. 
      */ 
     boolean insideItem = false; 

      // Returns the type of current event: START_TAG, END_TAG, etc.. 
     int eventType = xpp.getEventType(); 
     while (eventType != XmlPullParser.END_DOCUMENT) { 
      if (eventType == XmlPullParser.START_TAG) { 

       if (xpp.getName().equalsIgnoreCase("item")) { 
        insideItem = true; 
       } else if (xpp.getName().equalsIgnoreCase("title")) { 
        if (insideItem) 
         headlines.add(xpp.nextText()); //extract the headline 
       } else if (xpp.getName().equalsIgnoreCase("link")) { 
        if (insideItem) 
         links.add(xpp.nextText()); //extract the link of article 
       } 
      }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){ 
       insideItem=false; 
      } 

      eventType = xpp.next(); //move to next element 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    public InputStream getInputStream(URL url) { 
     try { 
      return url.openConnection().getInputStream(); 
     } catch (IOException e) { 
      return null; 
     } 
    } 
+0

안녕을 시도 XML을 RSS 피드 분석을 xmlpullparser 또는 색소폰 파서 또는 DOM 파서를 사용한다! 그것은 작동합니다. 모든 데이터 항목을 추출 할 수 있습니다. 그러나 나는 또한 뉴스에 대한 그림을 보여주기 위해 "media : content"속성을 "url"로 추출해야한다. 조금 더 plz 도움이 ... :) – Rider

+0

당신은 미디어 콘텐츠 URL 태그를 시도 해 봤나? – skyshine