程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-從XML文件中獲取完整的 RSS 內容提要

android-從XML文件中獲取完整的 RSS 內容提要

編輯:編程綜合問答
從XML文件中獲取完整的 RSS 內容提要

以下是XML 文件,我想從每個Item tag中取出 Description tag 的內容。我使用一個SAX解析器,但是不正確,如何實現呢?

<?xml version="1.0" encoding="utf-8"?>
<!-- generator="FeedCreator 1.8.0-dev ([email protected])" -->
<rss version="2.0"  xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Latest Happenings</title>
        <description></description>
        <link>http://www.bjp.org/</link>
        <lastBuildDate>Wed, 25 May 2011 11:28:15 GMT</lastBuildDate>
        <generator>FeedCreator 1.8.0-dev ([email protected])</generator>
        <atom:link href="http://www.bjp.org/index.php?option=com_ninjarsssyndicator&amp;feed_id=12&amp;format=raw" rel="self" type="application/rss+xml" />        <item>
            <title>Meeting of BJP State Presidents, Pradesh Prabharies/Sah-prabharis  and State General Secretaries (Org.) at Lucknow</title>
        <description>

<image>http://www.bjp.org/images/stories/ne_eng_may_05_2011.jpg</image>

        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Sun, 05 Jun 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title>BJP National Executive Meeting at Lucknow</title>
        <description>

<image>http://www.bjp.org/images/stories/meeting3062011.jpg</image>

        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Sat, 04 Jun 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title>BJP National Executive Meeting at Lucknow</title>
        <description>

<image>http://www.bjp.org/images/stories/meeting3062011.jpg</image>

        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Fri, 03 Jun 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title>National Conference on RTI Training by RTI Cell</title>
        <description>
            <text><![CDATA[<img width="900" src="http://www.bjp.org/images/stories/invitation card new.jpg" />]]></text>
        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Thu, 26 May 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title>BJYM against corruption – Hai Dum To Badhao Kadam, Chhattisgarh</title>
        <description>
<text><![CDATA[<p>]]></text>
<link>http://www.bjym.org/hai-dum-to-badhao-kadam</link>
<text><![CDATA[</p>
<p>]]></text>
<image>http://www.bjp.org/images/stories/bjym_1.jpg</image>

        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Mon, 23 May 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title>National Convention on Corruption &amp;amp; Recovery of Black Money by CA Cell</title>
        <description>

<image>http://www.bjp.org/images/stories/ca1.jpg</image>
<text><![CDATA[
<div><br />
<div>]]></text>
<image>http://www.bjp.org/images/stories/ca1.jpg</image>
<text><![CDATA[
</div>]]></text>
        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Tue, 17 May 2011 07:20:52 GMT</pubDate>
        </item>
        <item>
            <title>All India Lawyer Conference at Lucknow </title>
        <description>
<text><![CDATA[<p style="text-align: center;">]]></text>
<image>http://www.bjp.org/images/stories/kamala_2.png</image>
<text><![CDATA[
<p style="text-align: center;"><span style="color:#ff6600;">
<span style="font-size: 24pt;"><strong>Bharatiya Janata Party</strong><strong><em><br /><br /></em></strong></span></span></p>
<span style="color: #008000;"><span style="font-size: 18pt;"><span style="font-family: arial black,avant garde;">&nbsp;</span></span></span>
<p style="text-align: center;"><span style="color:#008000;"><span style="font-size: 18pt;"><strong><span style="font-family: 'arial black', 'avant garde';">All India Lawyer Conference </span></strong></span></span></p>
<p style="text-align: center;"><span style="color:#008000;"><span style="font-size: 18pt;"><strong><span style="font-family: 'arial black', 'avant garde';">at Lucknow (Uttar Pradesh)</span></strong></span></span></p>
<p style="text-align: center;"><span style="font-family: 'arial black', 'avant garde'; color:#008000; font-size: large;"><strong><br /></strong></span></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><strong><span style="font-family: andale mono,times;"><em><span style="font-size: 14pt;"><span style="color:#ff0000;">of</span></span></em></span></strong></p>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><span style="font-size: 14pt;"><span style="color: #000080;"><strong>BJP President Shri Nitin Gadkari</strong></span></span></span></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><span style="font-size: 12pt;"><span style="color:#ff0000;"><strong>on<br /></strong></span></span></p>
<p style="text-align: center;"><strong><span style="font-size: 12pt;"><span style="color: #ff0000;">May 15, 2011 (Sunday)</span></span><br /></strong></p>]]></text>
        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Sun, 15 May 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title>Border Darshan: Commemorating 110th Birth Anniversary of Dr. Syama Prasad Mookerjee</title>
        <description>
<text><![CDATA[<p>]]></text>
<link>http://borderdarshan.com/</link>
<text><![CDATA[</p>
<p><img width="900" src="http://www.bjp.org/images/stories/border_darshan_delhi-without pic-shri tarun vijaybalaptenew.jpg" /></p>
<p><img src="http://www.bjp.org/images/stories/border_darshan_delhi-without pic-shri tarun vijaybalaptenew.jpg" /></p>]]></text>
        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Sat, 14 May 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title>BJP SC Morcha National Executive Meeting at Andhra Pradesh</title>
        <description>
<text><![CDATA[<p style="text-align: center;">]]></text>
<image>http://www.bjp.org/images/stories/kamala_2.png</image>
<text><![CDATA[
<p style="text-align: center;"><span style="color:#ff6600;"><span style="font-size: 24pt;"><strong>Bharatiya Janata Party</strong><strong><em><br /><br /></em></strong></span></span></p>
<span style="color: #008000;"><span style="font-size: 18pt;"><span style="font-family: arial black,avant garde;">&nbsp;</span></span></span><span style="font-family: arial black,avant garde;"><span style="font-size: 18pt;"><span style="color: #008000;"> </span></span></span>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><span style="font-size: 18pt;"><span style="color: #008000;">BJP SC Morcha National Executive Meeting </span></span></span></p>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><span style="font-size: 18pt;"><span style="color: #008000;">at </span></span></span></p>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><span style="font-size: 18pt;"><span style="color: #008000;">Andhra Pradesh</span></span></span></p>
<p style="text-align: center;"><span style="font-family: 'arial black', 'avant garde'; color:#008000; font-size: large;"><strong><br /></strong></span></p>
<p style="text-align: center;"><strong><span style="font-family: andale mono,times;"><em><span style="font-size: 14pt;"><span style="color:#ff0000;">of</span></span></em></span></strong></p>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><span style="font-size: 14pt;"><span style="color: #000080;"><strong>ST Morcha National President<br />Shri Faggan Singh Kulasthe</strong></span></span></span></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><span style="font-size: 12pt;"><span style="color:#ff0000;"><strong>on<br /></strong></span></span></p>
<p style="text-align: center;"><strong><span style="font-size: 12pt;"><span style="color: #ff0000;">May 12, 2011 (Thursday</span></span></strong><strong><span style="font-size: 12pt;"><span style="color:#ff0000;">)</span></span><br /></strong></p>]]></text>
        </description>
            <author> [email protected] (Anurag Pathak)</author>
            <pubDate>Thu, 12 May 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title>Mukhyamantri Sammelan at  Constitution Club, New Delhi</title>
        <description>
<text><![CDATA[<p style="text-align: center;">]]></text>
<image>http://www.bjp.org/images/stories/kamala_2.png</image>
<text><![CDATA[
<p style="text-align: center;"><span style="color:#ff6600;"><span style="font-size: 24pt;"><strong>Bharatiya Janata Party</strong><strong><em><br /><br /></em></strong></span></span></p>
<span style="color: #008000;"><span style="font-size: 18pt;"><span style="font-family: arial black,avant garde;">&nbsp;</span></span></span>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><strong><span style="color:#008000;"><span style="font-size: 18pt;">Mukhyamantri Sammelan </span></span></strong></span></p>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><strong><span style="color:#008000;"><span style="font-size: 18pt;">at&nbsp; </span></span></strong></span></p>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><strong><span style="color:#008000;"><span style="font-size: 18pt;">Constitution Club, New Delhi</span></span></strong></span></p>
<span style="color: #008000;"><span style="font-size: 18pt;"><strong><span style="font-family: 'arial black', 'avant garde';"><br /></span></strong></span></span>
<p style="text-align: center;"><strong><span style="font-family: andale mono,times;"><em><span style="font-size: 14pt;"><span style="color:#ff0000;">of</span></span></em></span></strong></p>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><span style="font-size: 14pt;"><span style="color: #000080;"><strong>BJP President Shri Nitin Gadkari</strong></span></span></span></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><span style="font-size: 12pt;"><span style="color:#ff0000;"><strong>on<br /></strong></span></span></p>
<p style="text-align: center;"><strong><span style="font-size: 12pt;"><span style="color: #ff0000;">May 9-10, 2011 (Monday)</span></span><br /></strong></p>]]></text>
        </description>
            <author> [email protected] (Pritam Sharma)</author>
            <pubDate>Mon, 09 May 2011 00:00:00 GMT</pubDate>
        </item>
    </channel>
</rss>

最佳回答:


通過 DocumentBuilder 創建一個 Document,然後使用 DOM 方法來獲取你所需要的。

   InputStream responseStream = ... // create an InputStream from your string or something else
    Document doc = null;

    try {
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = docBuilder.parse(new InputSource(new InputStreamReader(responseStream)));
    } catch (ParserConfigurationException e) {
        throw new IOException(e.getMessage());
    } catch(SAXException e) {
        throw new IOException(e.getMessage());
    }

    if(doc != null) {

        NodeList items = doc.getElementsByTagName("item");
        int len = items.getLength();
        for(int i = 0; i < len; i++) {
            Element item = (Element)items.get(i);
            NodeList descItems = item.getElementsByTagName("description");

            if(descItems.getLength() > 0) {
                Element descrElement = (Element)descItems.get(0); // get first
                // do anything you want with it
            }
        }

    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved