XmlPullParser跳过一些标记

时间:2017-06-27 19:32:03

标签: android rss

我正在尝试用Android中的XmlPullParser pull解析器解析RSS,但它在第一次迭代中不断跳过content:encoded标记,并且只在第二次迭代中抓取它,尽管它抓取了之前的标记数据,这里是我的代码

ArrayList<FeedItem> feeds = new ArrayList<>();

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

我正在解析的RSS link

0 个答案:

没有答案