2011-05-25 59 views
0

完整的RSS提要內容下面是我的XML文件,我想取每Item標籤的Description標籤的內容。我嘗試使用SAX解析器,但它不起作用。我怎樣才能做到這一點?取從XML文件

<?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> 

回答

1

通過創建一個DocumentBuilderDocument,然後使用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 
      } 
     } 

    }