Как я могу прочитать тег XML без StartTag или Endtag, используя XmlPullParser Android - PullRequest
0 голосов
/ 21 сентября 2018

Вот XML, который я пытаюсь прочитать в Android Java с помощью XmlPullParser, но я не могу найти способ прочитать тег <link/>, так как это закрытый тег

<id>
    tag:google.com,2013:googlealerts/feed:10407958590599670710
</id>

<title type="html">
    Uhuru&#39;s order on fare control has no legal backing
</title>

<link href="https://www.google.com/url?rct=j&sa=t&url=https://www.nation.co.ke/business/Uhuru-s-order-on-fare-control-has-no-legal-backing/996-4768072-coxlk6z/index.html&ct=ga&cd=CAIyHDI1YTNhOGJmZjY3ZmQ4NTk6Y29tOmVuOktFOlI&usg=AFQjCNG10EpkC5Gogga5T4Hkys8pg3TCHw"/>

<published>2018-09-19T18:11:15Z</published>

<updated>2018-09-19T18:11:15Z</updated>

<content type="html">
    Lawyers, matatu operators and 
    <b>NTSA</b> sources said that the transport ... transport sector — said 
    <b>NTSA</b> has no legal mandate to set fares, adding that&nbsp;...
</content>

<author>
    <name/>
</author>

Это мой фрагмент кода Java, который пытается заархивировать чтение через xml

while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {
        if (xpp.getName().equalsIgnoreCase("entry")) {
            insideItem = true;
        } else if (xpp.getName().equalsIgnoreCase("title")) {
            if (insideItem) {
                titles.add(xpp.nextText());
            }
        } else if (xpp.getName().equalsIgnoreCase("link")) {
            if (insideItem) {
                links.add(xpp.nextText());
            }
        } else if (xpp.getName().equalsIgnoreCase("content")) {
            if (insideItem) {
                content.add(xpp.nextText());
            }
        } else if (xpp.getName().equalsIgnoreCase("published")) {
            if (insideItem) {
                published.add(xpp.nextText());
            }
        }
    } else if (eventType == XmlPullParser.END_TAG && 
               xpp.getName().equalsIgnoreCase("entry")) {

               insideItem = false;
    }

    eventType = xpp.next();
}

1 Ответ

0 голосов
/ 21 сентября 2018

Я наконец смог прочитать самозакрытый тег, используя этот фрагмент кода:

protected Exception doInBackground(Integer... integers) {
            try {

                url = new URL(RSS_URL_2);

                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(false);

                XmlPullParser xpp = factory.newPullParser();

                xpp.setInput(getInputStream(url), "UTF_8");

                boolean insideItem = false;

                int eventType = xpp.getEventType();

                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if (xpp.getName().equalsIgnoreCase("entry")) {
                            insideItem = true;
                        } else if (xpp.getName().equalsIgnoreCase("title")) {
                            if (insideItem) {
                                titles.add(xpp.nextText());
                            }
                        } else if (xpp.getName().equals("link")) {
                            if (insideItem) {
                                links.add(xpp.getAttributeValue(null, "href"));
                            }
                        } else if (xpp.getName().equalsIgnoreCase("content")) {
                            if (insideItem) {
                                content.add(xpp.nextText());
                            }
                        } else if (xpp.getName().equalsIgnoreCase("published")) {
                            if (insideItem) {
                                published.add(xpp.nextText());
                            }
                        }
                    } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("entry")) {
                        insideItem = false;
                    }

                    eventType = xpp.next();
                }

            } catch (MalformedURLException me) {
                exception = me;
            } catch (XmlPullParserException xp) {
                exception = xp;
            } catch (IOException ie) {
                exception = ie;
            }

            return exception;
        }
...