【问题标题】:How do I Read an XML tag without StartTag or Endtag using XmlPullParser Android如何使用 XmlPullParser Android 读取没有 StartTag 或 Endtag 的 XML 标记
【发布时间】:2018-09-20 23:59:45
【问题描述】:

这是我尝试使用 XmlPullParser 在 Android Java 中读取的 XML,但我找不到读取 <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 代码 sn-p 试图通过 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();
}

【问题讨论】:

    标签: android xml xml-parsing xmlpullparser


    【解决方案1】:

    我终于可以使用这段代码读取自闭标签了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-02
      • 2023-03-07
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2013-01-15
      相关资源
      最近更新 更多