2011-03-25 61 views
1

我正在使用以下代碼來解析RSS提要。如果引號出現,它不起作用。請幫忙。Android報價未得到解析

public static ArrayList<RssItem> getRssItems(String feedUrl) { 

     ArrayList<RssItem> rssItems = new ArrayList<RssItem>(); 

     try { 
      //open an URL connection make GET to the server and 
      //take xml RSS data 
      URL url = new URL(feedUrl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

      if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       InputStream is = conn.getInputStream(); 

       //DocumentBuilderFactory, DocumentBuilder are used for 
       //xml parsing 
       DocumentBuilderFactory dbf = DocumentBuilderFactory 
         .newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 

       //using db (Document Builder) parse xml data and assign 
       //it to Element 
       Document document = db.parse(is); 
       Element element = document.getDocumentElement(); 

       //take rss nodes to NodeList 
       NodeList nodeList = element.getElementsByTagName("item"); 

       if (nodeList.getLength() > 0) { 
        for (int i = 0; i < nodeList.getLength(); i++) { 

         //take each entry (corresponds to <item></item> tags in 
         //xml data 

         Element entry = (Element) nodeList.item(i); 

         Element _titleE = (Element) entry.getElementsByTagName(
           "title").item(0); 
         Element _descriptionE = (Element) entry 
           .getElementsByTagName("description").item(0); 
         Element _pubDateE = (Element) entry 
           .getElementsByTagName("pubDate").item(0); 
         Element _linkE = (Element) entry.getElementsByTagName(
           "link").item(0); 

         String _title = _titleE.getFirstChild().getNodeValue(); 
         String _description = _descriptionE.getFirstChild().getNodeValue(); 
         Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue()); 
         String _link = _linkE.getFirstChild().getNodeValue(); 

         //create RssItemObject and add it to the ArrayList 
         RssItem rssItem = new RssItem(_title, _description, 
           _pubDate, _link); 

         rssItems.add(rssItem); 
        } 
       } 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return rssItems; 
    } 

回答

0

您假設元素中只有一個文本節點(getFirstChild().getNodeValue())。這是一個錯誤的假設。您需要迭代元素的所有子節點併合並文本節點。

+0

潔具。你能舉一個例子代碼嗎?請。我是android新手。 – Droidme 2011-03-25 13:50:01