2012-04-26 104 views
0

這是我從中提取數據的RSS XML文件。我用SAX解析器完成了所有工作,所有工作都很好。但是我解析了整個RSS文件並獲取了每個單元素數據。Rss解析問題(XML)

但我的問題是我得到整個文件元素的數據,但我只想要只在元素。

<rss version="2.0"> 
<channel> 
<title>RSS Feed</title> 
<link>http://www.xyz.com</link> 
<description>Calendar RSS Feeds</description> 
<language>en-us</language> 
<ttl>60</ttl> 
<item> 
<title> 
title 1 
</title> 
<description>description 1</description> 
<link> 
http://www.xyz.com 
</link> 
<guid isPermaLink="false">[email protected]://www.xyz.com</guid> 
</item> 
<item> 
<title> 
title 2 
</title> 
<description>description 2</description> 
<link> 
http://www.xyz.com 
</link> 
<guid isPermaLink="false">[email protected]://www.xyz.com</guid> 
</item> 
<item> 
</channel> 
</rss> 

我已經嘗試了所有的組合來設置此我會告訴我的代碼在這裏我提出的startElement和 的endElement方法: -

public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 
     // TODO Auto-generated method stub 

     current = true; 

     if (localName.equals("channel")) 
     { 
      itemList = new ItemList(); 
     } 
    } 

    @Override 
    public void endElement(String uri, String localName, String qName) 
      throws SAXException { 
     // TODO Auto-generated method stub 
     current = false; 

     if(localName.equals("title")) 
     { 
      itemList.setTitle(currentValue); 
     } 

    } 

    @Override 
    public void characters(char[] ch, int start, int length) 
      throws SAXException { 
     // TODO Auto-generated method stub 

     if(current) 
     { 
      currentValue = new String(ch, start, length); 
      current=false; 
     } 
    } 

任何一個請建議我如何只能retrive的僅在元素下的元素。

我也試過,但這個給了我錯誤

public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 
     // TODO Auto-generated method stub 

     current = true; 

     if (localName.equals("channel")) 
     { 
      if (localName.equals("item")) 
      { 
      itemList = new ItemList(); 
      } 
     } 
    } 

在此先感謝。

回答

0

試試這個,希望它西港島線幫助....

url = new URL("http://www.abcd.com/rss.xml");//....... 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

    if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){ 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc; 
      doc = db.parse(url.openStream()); 
      doc.getDocumentElement().normalize(); 
      NodeList itemLst = doc.getElementsByTagName("item"); 



      Array.Description = new String[itemLst.getLength()];//........ 
      Array.Title = new String[itemLst.getLength()]; 
      Array.image = new String[itemLst.getLength()]; 
      Array.Date = new String[itemLst.getLength()]; 

      for(int i=0; i < itemLst.getLength(); i++){ 

       Node item = itemLst.item(i); 
       if(item.getNodeType() == Node.ELEMENT_NODE){ 
         Element ielem = (Element)item; 
         NodeList title = ielem.getElementsByTagName("title"); 
         NodeList date = ielem.getElementsByTagName("pubDate"); 

         NodeList description = ielem.getElementsByTagName("description"); 

        ////......... 
         Array.Title[i] = title.item(0).getChildNodes().item(0).getNodeValue(); 
         Array.Description[i] = description.item(0).getChildNodes().item(0).getNodeValue(); 
         Array.Date[i] = date.item(0).getChildNodes().item(0).getNodeValue(); 
+0

我已經嘗試過這樣的DocumentBuilder我得到這個結果,但在這我不知道如何實現StringBuffer的,因爲我不在DocumentBuider中獲取完整的數據。你可以建議我如何使用DocumentBuilder實現StringBuffer.Thanks ... – user755278 2012-04-26 10:17:04

+0

k ...我已經構建了一個RSS閱讀器。我將發佈我用於解析的整個文件。只需檢查它... – 2012-04-26 10:19:49

0
public class headlinesparser { 


public static void parse(){ 
URL url; 
try { 
    url = new URL("http://www.abcd.com/taxonomy/term/19/rss.xml");//....... 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

    if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){ 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc; 
      doc = db.parse(url.openStream()); 
      doc.getDocumentElement().normalize(); 
      NodeList itemLst = doc.getElementsByTagName("item"); 



      Headlines.Description = new String[itemLst.getLength()];//........ 
      Headlines.Title = new String[itemLst.getLength()]; 
      Headlines.image = new String[itemLst.getLength()]; 
      Headlines.Date = new String[itemLst.getLength()]; 

      for(int i=0; i < itemLst.getLength(); i++){ 

       Node item = itemLst.item(i); 
       if(item.getNodeType() == Node.ELEMENT_NODE){ 
         Element ielem = (Element)item; 
         NodeList title = ielem.getElementsByTagName("title"); 
         NodeList date = ielem.getElementsByTagName("pubDate"); 

         NodeList description = ielem.getElementsByTagName("description"); 

        ////......... 
         Headlines.Title[i] = title.item(0).getChildNodes().item(0).getNodeValue(); 
         Headlines.Description[i] = description.item(0).getChildNodes().item(0).getNodeValue(); 
         Headlines.Date[i] = date.item(0).getChildNodes().item(0).getNodeValue(); 

        if (Headlines.Description[i].contains("<img ")){ 

        String img = Headlines.Description[i].substring(Headlines.Description[i].indexOf("<img ")); 
        String cleanUp = img.substring(0, img.indexOf(">")+1); 
        img = img.substring(img.indexOf("src=") + 5); 
        int indexOf = img.indexOf("'"); 
        if (indexOf==-1){ 
         indexOf = img.indexOf("\""); 
        } 
        img = img.substring(0, indexOf); 

        //setImgLink(img); 

        Headlines.image[i]=img; 

         } 




       } 

      } 

    } 

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

} 


}