Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用XmlPullParser解析RSS提要?_Android_Rss_Xmlpullparser - Fatal编程技术网

Android 如何使用XmlPullParser解析RSS提要?

Android 如何使用XmlPullParser解析RSS提要?,android,rss,xmlpullparser,Android,Rss,Xmlpullparser,我想解析一个RSS提要。我的问题是如何解析和标记之间的所有标记 考虑到这一非常简单的XML: <?xml version="1.0" ?> <rss version="2.0"> <channel> <title>MyRSSPage</title> <link>http://www.example.com</link> <item> <link>www.example.co

我想解析一个RSS提要。我的问题是如何解析
标记之间的所有标记

考虑到这一非常简单的XML:

<?xml version="1.0" ?>
<rss version="2.0">
<channel>
  <title>MyRSSPage</title>
  <link>http://www.example.com</link>
  <item>
  <link>www.example.com/example1</link>
  <title>Example title 1</title>
  </item>
  <item>
  <link>www.example.com/example2</link>
  <title>Example title 2</title>
  </item>
</channel>
</rss>
试试下面的方法

try {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(url.openConnection().getInputStream(), "UTF_8"); 
    //xpp.setInput(getInputStream(url), "UTF-8");

    boolean insideItem = false;

    // Returns the type of current event: START_TAG, END_TAG, etc..
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {

            if (xpp.getName().equalsIgnoreCase("item")) {
                insideItem = true;
            } 
            else if(xpp.getName().equalsIgnoreCase("title")) 
            {

            }
        }
        eventType = xpp.next(); //move to next element
    }

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (XmlPullParserException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
编辑:

输出

www.example.com/example1
Example title 1
www.example.com/example2
Example title 2

代码怎么了?没问题。除了我不知道如何解析介于
之间的标签外,没有什么问题。请看我在上面文章中的编辑。我的问题是如何解析
标记之间的元素。请尝试编辑。它应该会起作用@Raghunandan你能给我发一个
标签之间的lin吗example@Raghunandan你能再解释一点例子吗this@NagarjunaReddy你在哪里需要解释。您还可以将xml复制到assests fodler,并使用op在问题中使用的xml
try {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(url.openConnection().getInputStream(), "UTF_8"); 
    //xpp.setInput(getInputStream(url), "UTF-8");

    boolean insideItem = false;

    // Returns the type of current event: START_TAG, END_TAG, etc..
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {

            if (xpp.getName().equalsIgnoreCase("item")) {
                insideItem = true;
            } 
            else if(xpp.getName().equalsIgnoreCase("title")) 
            {

            }
        }
        eventType = xpp.next(); //move to next element
    }

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (XmlPullParserException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(open,null);
// xpp.setInput(getInputStream(url), "UTF-8");

boolean insideItem = false;

// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {

        if (xpp.getName().equalsIgnoreCase("item")) {
            insideItem = true;
        } else if (xpp.getName().equalsIgnoreCase("title")) {
            if (insideItem)
                Log.i("....",xpp.nextText()); // extract the headline
        } else if (xpp.getName().equalsIgnoreCase("link")) {
            if (insideItem)
                Log.i("....",xpp.nextText());  // extract the link of article
        }
    } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
        insideItem = false;
    }

    eventType = xpp.next(); // move to next element
}
www.example.com/example1
Example title 1
www.example.com/example2
Example title 2