2011-05-31 42 views
0

這是RSS饋送的項目元素沒有得到使用了一個節點的節點的Html值RSS訂閱

- <item> 
      <guid isPermaLink="false">40</guid> 
      <category>Parking</category> 
      <category>Paris - Charles de Gaulle</category> 
      <title>Quick drop-off</title> 
      <description><description>&lt;span style="color: #00437f;"&gt;&lt;strong&gt;Trains &lt;br /&gt;&#xD; 
SNCF mainline trains&lt;br /&gt;&#xD; 
&lt;/strong&gt;The Paris-Charles de Gaulle SNCF station is situated between terminals 2C-2D and 2E-2F - The station is called "Aéroport Charles de Gaulle 2 - TGV".&#xD; 
&lt;p&gt;Getting to or leaving Paris-Charles de Gaulle :&lt;br /&gt;&#xD; 
- Terminals ABCDEF - accessible via the travelator&lt;br /&gt;&#xD; 
-From Paris - Opéra to Paris-Charles de Gaulle&lt;br /&gt;&#xD; 
Stops at the airport :&lt;br /&gt;&#xD; 
- Terminals 2A, 2C, 2E-2F, 2D, 2B, 3 and 1&lt;br /&gt;&#xD; 
- Terminal 2G is linked to terminal 2C (entrance 4) by the free N2 shuttle &lt;br /&gt;&#xD; 
(Average travel time : 15 minutes)&lt;/p&gt;&#xD; 
- Terminal 2G is linked to terminal 2C (entrance 4) by the free N2 shuttle &lt;br /&gt;&#xD; 
(Average travel time : 15 minutes)&lt;/p&gt;&#xD; 
    Fare* : 15 € (Pass Navigo not valid)&#xD; 
&lt;p&gt; &lt;/p&gt;&#xD; 
&lt;p&gt;&lt;strong&gt;&lt;br /&gt;&#xD; 
&lt;p&gt; &lt;/p&gt;&#xD; 
6 bus lines between Tremblay en France, Goussainville, Sarcelles, Villiers le Bel, Othis, Villeparisis and Paris-Charles de Gaulle airport.&lt;br /&gt;&#xD; 
Fares* : Ticket t+, between 1,14 € and 2,28 €&lt;/p&gt;&#xD; 
&lt;div class="ll" style="border-top: #dadada 1px solid; ParaRoi=0&amp;amp;ParaProfiling=0&amp;amp;ParaCompte=Adp_mobile" /&gt; &#xD; 
</description> 
      <pubDate>Tue, 01 Dec 2009 00:00:00 +0100</pubDate> 
      <enclosure url="http://services.aeroportsdeparis.fr/iPhoneRessources/pict_DpMinute.png" type="image/png" /> 
      <nbArticles>8</nbArticles> 
      <idSite>0</idSite> 
      </item> 

我可以使用DOM解析器除了描述數據該父下訪問其他節點的數據。

使用下面的函數

public class Rss_Data_Retriever 
{ 

    Document myDoc; 
    private String[][] data=null; 

    public Rss_Data_Retriever(String Url) 
    { 
     InputStream in; 
     in=getInputStream(Url); 
     myDoc=getDocument(in); 

    } 
    public String getSampleString() 
    { 
     String s=getNthNodeValue("category", this.myDoc,0); 
     FeastingOfData(this.myDoc, "item"); 
     return s; 
    } 

    public InputStream getInputStream(String urlString) 
    { 
     int responseCode=0; 
     HttpURLConnection httpurlConnection = null; 

     try 
     { 
      URL url=new URL(urlString); 
      URLConnection connection; 


      connection=url.openConnection(); 

      httpurlConnection=(HttpURLConnection)connection; 

      responseCode=httpurlConnection.getResponseCode(); 

     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     if(responseCode==HttpURLConnection.HTTP_OK) 
     { 

      Log.d("Message", "httpOk"); 
      InputStream input_fn=null; 
      try 
      { 
       input_fn = httpurlConnection.getInputStream(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return input_fn; 

     } 
     else 
      Log.d("Message", "httpNotOk"); 

     return null; 

    } 

    public Document getDocument(InputStream inputStream) 
    { 
     DocumentBuilder builder; 
     NodeList docTitle = null; 
     Document Doc=null; 


      try 
      { 
       builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
       Doc= builder.parse(inputStream); 
      } 
      catch (ParserConfigurationException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (FactoryConfigurationError e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SAXException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     return Doc; 
    } 

    public String getNthNodeValue(String Tag,Document doc,int index) 
    { 
     DocumentBuilder builder; 
     NodeList docTitle = null; 
     Document Doc=null; 

     docTitle=doc.getElementsByTagName(Tag); 


     builder=null; 
     Doc=null; 
     Element value=(Element) docTitle.item(index); 
     return value.getFirstChild().getNodeValue(); 

    } 
    public String getNthNodeValue(String Tag,Element element,int index,boolean isValue) 
    { 

     NodeList nodeList = null; 
     try   
     { 


      nodeList=element.getElementsByTagName(Tag); 
     } 
     catch (FactoryConfigurationError e) 
     { 

     } 

     Element value=(Element) nodeList.item(index); 
     if(isValue) 
      return value.getFirstChild().getNodeValue(); 
     else 
      return value.getAttribute("url"); 
    } 

    public String[][] getData() 
    { 
     return data; 
    } 

    public void FeastingOfData(Document doc,String TagName) 
    { 
     DocumentBuilder builder; 
     NodeList nodeList = null; 




       nodeList=doc.getElementsByTagName(TagName); 
       Log.d("Message", "Length:"+nodeList.getLength()); 

       if(data==null) 
        data=new String [nodeList.getLength()][5]; 

       int i=0; 

       for(i=0;i<nodeList.getLength();i++) 
       { 
        Element node=(Element) nodeList.item(i); 
        String maincategory=getNthNodeValue("category", node, 0,true); 
        String category=getNthNodeValue("category", node, 1,true); 
        String title=getNthNodeValue("title", node, 0,true); 
        String description=getNthNodeValue("description", node, 0,true); 
        String enclosureUrl=getNthNodeValue("enclosure", node, 0,false); 


        data[i][0]=maincategory; 
        data[i][1]=category; 
        data[i][2]=title; 
        data[i][3]=description; 
        data[i][4]=enclosureUrl; 



        Log.d("DataChecked", "maincategory:"+maincategory); 
        Log.d("DataChecked", "category:"+category); 
        Log.d("DataChecked", "title"+title); 
        Log.d("DataChecked", "enclosureUrl:"+enclosureUrl); 
        Log.d("DataChecked", "Description:"+description); 
       } 

    } 
} 

我試圖打印描述的節點值然後我得到了「<」簡單隻在同一時間我可以打印 類別,標題等

什麼都可以問題

問候, Kariyachan

+0

可否請您解析後您的整個代碼? – 2011-05-31 12:51:59

+0

我更新了我的問題 – DroidBot 2011-06-01 03:31:07

+0

夥計們請幫助我,謝謝 – DroidBot 2011-06-02 04:48:36

回答

1

這是我如何解決Html內容是在描述標籤,所以我改變了代碼如下。現在我可以 加載字符串返回到webview。

希望有人會發現這個有用的。

感謝

public String getNthNodeValue(String Tag, Element element, int index, 
      boolean isValue) { 

     NodeList nodeList = null; 
     try { 

      nodeList = element.getElementsByTagName(Tag); 
     } catch (FactoryConfigurationError e) { 

     } 

     Element value = (Element) nodeList.item(index); 
     if (Tag.equals("description")) { 
      String str = ""; 
      value.normalize(); 
      str = value.getFirstChild().getNodeValue(); 
      return str; 
     } 

     if (isValue) 
      return value.getFirstChild().getNodeValue(); 
     else 
      return value.getAttribute("url"); 
    } 
1

你描述C進一步添加xml/html標記,但未包含CDATA聲明。我的猜測是這導致解析器將內容解釋爲進一步標記而不是節點值。你可以找到CDATA here的基本解釋。

+0

感謝您的回覆。我更新了我的問題,並從該RSS源站點的查看源獲得了描述數據。請看看謝謝 – DroidBot 2011-06-01 03:23:09

+0

您看過logcat輸出嗎?當你擊中異常時,你只是打印堆棧跟蹤,所以我想知道是否有任何一個被擊中。你能否檢查描述節點是否有孩子? – 2011-06-01 03:35:20

+0

我看着logcat輸出我得到** <**簽名作爲輸出和描述沒有子節點。 – DroidBot 2011-06-01 03:51:33

相關問題