2 clcurie 11 clcurie_11 于 2013.03.11 13:08 提问

从XML文件中获取完整的 RSS 内容提要

以下是XML 文件,我想从每个Item tag中取出 Description tag 的内容。我使用一个SAX解析器,但是不正确,如何实现呢?

<?xml version="1.0" encoding="utf-8"?>
<!-- generator="FeedCreator 1.8.0-dev (info@mypapit.net)" -->
<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 (info@mypapit.net)</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> anurag.pathak@bjp.org (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> anurag.pathak@bjp.org (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> anurag.pathak@bjp.org (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> anurag.pathak@bjp.org (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> anurag.pathak@bjp.org (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> anurag.pathak@bjp.org (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> anurag.pathak@bjp.org (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> anurag.pathak@bjp.org (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> anurag.pathak@bjp.org (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> pritam.sharma@bjp.org (Pritam Sharma)</author>
            <pubDate>Mon, 09 May 2011 00:00:00 GMT</pubDate>
        </item>
    </channel>
</rss>

2个回答

curie_87
curie_87   2013.03.15 11:15
已采纳

通过 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
            }
        }

    }
pby_bob
pby_bob   2013.03.11 14:45

这种你使用pull解析会更简单方便,获取到description节点,然后就可以继续解析XmlPullParser,从parser中根据节点名称取出你想要的东西。

Csdn user default icon
上传中...
上传图片
插入图片