Android XML Parser跳过标签

时间:2017-06-27 04:32:46

标签: android xml rss

我正在制作一本能阅读RSS Feed的读者。起初我想使用一个库,但意识到他们没有加载一些数据,所以我决定自己创建一个读者。但这是问题所在。并不总是我的解析器返回一个图像,这取决于像这个RSS The image is in the content:data的网站,而对于像BBC那样的RSS,在内容中找到了图像:我无法获得的数据第一次迭代所以我有下一个项目获取前一项的图像,下面是我的代码String title =“”;

    String description = "";
    String link = "";
    String encodedDesciption = "";
    String imgSrc = "image";
    boolean isItem = false;
    try {
        XmlPullParser xmlPullParser = Xml.newPullParser();
        xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        xmlPullParser.setInput(inputStream, null);

       // xmlPullParser.nextTag();
        while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT) {

            int eventType = xmlPullParser.getEventType();

            String name = xmlPullParser.getName();
            if(name == null)
                continue;

            if(eventType == XmlPullParser.END_TAG) {
                if(name.equalsIgnoreCase("item")) {
                    isItem = false;
                }
                continue;
            }

            if (eventType == XmlPullParser.START_TAG) {
                if(name.equalsIgnoreCase("item")) {
                    isItem = true;
                    continue;
                }
            }

            String result = "";
            if (xmlPullParser.next() == XmlPullParser.TEXT) {
                result = xmlPullParser.getText();
                xmlPullParser.nextTag();
            }

            if (name.equalsIgnoreCase(TITLE)) {
                title = result;
            } else if (name.equalsIgnoreCase(LINK)) {
                link = result;
            } else if (name.equalsIgnoreCase(DESCRIPTION)) {
                description = result;
            }else if (name.equalsIgnoreCase(DESCRIPTION_ENCODED)) {
                encodedDesciption = result;

            }


            if (title != null && link != null && description != null && imgSrc != null && encodedDesciption != null) {
                if(isItem) {
                    Document document = Jsoup.parse(encodedDesciption);
                    Element img = document.select("img").first();
                    if (img != null) {
                        imgSrc = img.attr("src");
                        Log.d(TAG, "Image found "+imgSrc);
                        img.remove();
                    }else  Log.d(TAG, "Image not found");
                    FeedItem feedItem = new FeedItem(title, description, encodedDesciption, imgSrc, link );
                    feeds.add(feedItem);
                    Log.d(TAG, feedItem.toString());
                }

                title = null;
                link = null;
                description = null;
                isItem = false;
            }


        }


    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    } finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

如何在正确的索引处获得正确的content:data?以及如何获取网址

0 个答案:

没有答案