【问题标题】:Parsing Yahoo Rss Feeds for android为 android 解析 Yahoo Rss 提要
【发布时间】:2014-04-25 12:08:04
【问题描述】:

谁能帮我解析来自 URL Yahoo Feed 的 Yahoo RSS 源

我可以调用和获取 URL。我有字符串的响应。我想解析它并在列表中显示提要。请帮忙?

【问题讨论】:

    标签: android xml-parsing rss yahoo


    【解决方案1】:

    您应该使用 xmlpullparser 或 sax 解析器或 dom 解析器进行 xml rss 提要解析 试试这个例子

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

    【讨论】:

    • 嘿,非常感谢...!!!有用。我能够提取所有数据项。但我还需要将“media:content”的属性提取为“url”以显示新闻图片。多一点帮助请... :)
    猜你喜欢
    • 2014-08-26
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多