java - RSS 阅读器未获取某些标签

标签 java android rss

这是我上一个问题的继续问题:RSS Reader NullPointerException

在我的应用程序中,在我的列表中,有时我看不到 RSS 的标题,有时看不到描述(和图像)。最奇怪的是我对所有链接都没有问题。例如,如果我解析原始教程的链接( http://www.mobilenations.com/rss/mb.xml ),一切正常。但是当我使用其他链接时,我遇到了上述问题...

这是我的 DOMParser 类:

package com.td.rssreader.parser;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class DOMParser {

    private RSSFeed _feed = new RSSFeed();

    public RSSFeed parseXml(String xml) {

        // _feed.clearList();

        URL url = null;
        try {
            url = new URL(xml);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        try {
            // Create required instances
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            // Parse the xml
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            // Get all <item> tags.
            NodeList nl = doc.getElementsByTagName("item");
            int length = nl.getLength();

            for (int i = 0; i < length; i++) {
                Node currentNode = nl.item(i);
                RSSItem _item = new RSSItem();

                NodeList nchild = currentNode.getChildNodes();
                int clength = nchild.getLength();

                // Get the required elements from each Item
                for (int j = 1; j < clength; j = j + 2) {

                    Node thisNode = nchild.item(j);
                    String theString = null;

                      if (thisNode != null && thisNode.getFirstChild() != null) {
                            theString = thisNode.getFirstChild().getNodeValue();
                        }


                    if (theString != null) {

                        String nodeName = thisNode.getNodeName();

                        if ("title".equals(nodeName)) {
                            // Node name is equals to 'title' so set the Node
                            // value to the Title in the RSSItem.
                            _item.setTitle(theString);
                        }

                        else if ("description".equals(nodeName)) {
                            _item.setDescription(theString);

                            // Parse the html description to get the image url
                            String html = theString;
                            org.jsoup.nodes.Document docHtml = Jsoup
                                    .parse(html);
                            Elements imgEle = docHtml.select("img");
                            _item.setImage(imgEle.attr("src"));
                        }
//description
                        else if ("pubDate".equals(nodeName)) {

                            // We replace the plus and zero's in the date with
                            // empty string
                            String formatedDate = theString.replace(" +0000",
                                    "");
                            _item.setDate(formatedDate);
                        }


                        if ("link".equals(nodeName)) {
                            // Node name is equals to 'title' so set the Node
                            // value to the Title in the RSSItem.
                            _item.setLink(theString);
                        }
                    }
                }

                // add item to the list
                _feed.addItem(_item);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        // Return the final feed once all the Items are added to the RSSFeed
        // Object(_feed).
        return _feed;
    }

}

最佳答案

一旦您循环遍历项目节点,您就会有另一个循环尝试遍历子元素(以设置标题、描述等)。

但是循环从索引 1 开始并增加 2:

// Get the required elements from each Item
            for (int j = 1; j < clength; j = j + 2) {

这意味着它只检查位置 1、3、5 等

查看发布的 xml,这显示了为什么每个项目都会获得不同的数据。将循环设置为索引为 0 并仅增加 1。

关于java - RSS 阅读器未获取某些标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867104/

相关文章:

ruby - 如何使用 RSpec 测试 XML 文件?

java - Java 中动态和静态类型分配之间的区别

java - 最终引用的优化如何在 Java 中工作?

java - 在 OnClick android 上启动后台服务

java - Android 版本 4.3 中的异常 android java.util.Objects

android - 未找到 Google Play 服务资源

.net - .Net 的 RSS/Atom 解析库

java - 如何获得给定日期范围内的天数?

Java:如何在池中维护 SSLSocket session 以防止对每条消息进行新的握手

php - 使用 Phonegap 应用程序执行 ajax 请求时出现问题