2017-08-23 135 views
0

我正在使用enter link description here如何解析嵌套的RSS提要?

我能夠從URL獲取所有需要的數據。但我想獲取<item>...</item>屬性作爲List。我可以逐個獲取所有屬性。我想要完整的item屬性列表。 他們的任何其他選項是否相同?

以下是rss解析的代碼。

public class HandleXML { 

    public volatile boolean parsingComplete = true; 
    private String title = "title"; 
    private String link = "link"; 
    private String description = "description"; 
    private String image = "image"; 
    private String imageTitle = "title"; 
    private String imageDescription = "description"; 
    private String imageLink = "link"; 
    private String guid = "guid"; 
    private String pubDate = "pubDate"; 
    private String media_content = "media:content"; 
    private String urlString = null; 
    private XmlPullParserFactory xmlFactoryObject; 
    private boolean isImage = false; 

    public boolean isParsingComplete() { 
     return parsingComplete; 
    } 

    public void setParsingComplete(boolean parsingComplete) { 
     this.parsingComplete = parsingComplete; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public void setLink(String link) { 
     this.link = link; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getImage() { 
     return image; 
    } 

    public void setImage(String image) { 
     this.image = image; 
    } 

    public String getImageTitle() { 
     return imageTitle; 
    } 

    public void setImageTitle(String imageTitle) { 
     this.imageTitle = imageTitle; 
    } 

    public String getImageDescription() { 
     return imageDescription; 
    } 

    public void setImageDescription(String imageDescription) { 
     this.imageDescription = imageDescription; 
    } 

    public String getImageLink() { 
     return imageLink; 
    } 

    public void setImageLink(String imageLink) { 
     this.imageLink = imageLink; 
    } 

    public String getGuid() { 
     return guid; 
    } 

    public void setGuid(String guid) { 
     this.guid = guid; 
    } 

    public String getPubDate() { 
     return pubDate; 
    } 

    public void setPubDate(String pubDate) { 
     this.pubDate = pubDate; 
    } 

    public String getMedia_content() { 
     return media_content; 
    } 

    public void setMedia_content(String media_content) { 
     this.media_content = media_content; 
    } 

    public String getUrlString() { 
     return urlString; 
    } 

    public void setUrlString(String urlString) { 
     this.urlString = urlString; 
    } 

    public HandleXML(String url) { 
     this.urlString = url; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public String getLink() { 
     return link; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void parseXMLAndStoreIt(XmlPullParser myParser) { 
     int event; 
     String text = null; 

     try { 
      event = myParser.getEventType(); 

      while (event != XmlPullParser.END_DOCUMENT) { 
       String name = myParser.getName(); 

       switch (event) { 
        case XmlPullParser.START_TAG: 
         break; 

        case XmlPullParser.TEXT: 
         text = myParser.getText(); 
         break; 

        case XmlPullParser.END_TAG: 

         if (!isImage && name.equals("title")) { 
          title = text; 
          Log.e("Input text : ",text); 
         } 
         else if (!isImage && name.equals("link")) { 
          link = text; 
          Log.e("Input link : ",text); 
         } 
         else if (!isImage && name.equals("description")) { 
          description = text; 
          Log.e("Input description : ",text); 
         } 
         else if (name.equals("image")) { 
          image = text; 
          isImage = true; 
          Log.e("Input image : ",text); 
         } 
         else if (isImage && name.equals("title")) { 
          imageTitle = text; 
          Log.e("Input image title : ",text); 
         } 
         else if (isImage && name.equals("description")) { 
          imageDescription = text; 
          Log.e("Input image descr : ",text); 
         } 
         else if (isImage && name.equals("link")) { 
          imageLink = text; 
          Log.e("Input link : ",text); 
         } 
         else if (isImage && name.equals("guid")) { 
          guid = text; 
          Log.e("Input guid : ",text); 
         } 
         else if (isImage && name.equals("pubDate")) { 
          pubDate = text; 
          Log.e("Input pubdate : ",text); 
         } 
         else if (isImage && name.equals("media:content")) { 
          media_content = text; 
//       isImage = false; 
          Log.e("Input media:content : ",text); 
         } 
         break; 
       } 
       event = myParser.next(); 
      } 
      parsingComplete = false; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void fetchXML() { 
     Thread thread = new Thread(new Runnable() { 
      @Override 
      public void run() { 

       try { 
        URL url = new URL(urlString); 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

        conn.setReadTimeout(10000 /* milliseconds */); 
        conn.setConnectTimeout(15000 /* milliseconds */); 
        conn.setRequestMethod("GET"); 
        conn.setDoInput(true); 
        // Starts the query 
        conn.connect(); 
        InputStream stream = conn.getInputStream(); 

        xmlFactoryObject = XmlPullParserFactory.newInstance(); 
        XmlPullParser myparser = xmlFactoryObject.newPullParser(); 
        myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
        myparser.setInput(stream, null); 
        parseXMLAndStoreIt(myparser); 
        stream.close(); 
       } catch (Exception e) { 
       } 
      } 
     }); 
     thread.start(); 
    } 
} 

如何獲取物品清單?請幫忙。

回答

1

您應該 1)將Item類從HandleXML分離爲模型。 2)在通道標籤的開頭,初始化一個項目列表。 3)在item標籤的開頭,創建一個新的item對象。 4)相應地填充Item對象。 5)在item標籤的末尾添加Item對象到列表中。

您應該注意isImage變量。 init應該是true,一旦你創建了一個item對象,設置爲false。 該片段是如下的內容:

public class HandleXML { 

    public volatile boolean parsingComplete = true; 
    private String image = "image"; 
    private String imageTitle = "title"; 
    private String imageDescription = "description"; 
    private String imageLink = "link"; 
    private String urlString = null; 
    private XmlPullParserFactory xmlFactoryObject; 
    private boolean isImage = true; // isImage init to be true 

    private List<Item> itemList = null; 

    // separate the Item class with HandleXML 
    class Item { 
     private String title = "title"; 
     private String link = "link"; 
     private String description = "description"; 
     private String guid = "guid"; 
     private String pubDate = "pubDate"; 
     private String media_content = "media:content"; 

     public void setTitle(String title) { 
      this.title = title; 
     } 

     public void setLink(String link) { 
      this.link = link; 
     } 

     public void setDescription(String description) { 
      this.description = description; 
     } 

     public String getGuid() { 
      return guid; 
     } 

     public void setGuid(String guid) { 
      this.guid = guid; 
     } 

     public String getPubDate() { 
      return pubDate; 
     } 

     public void setPubDate(String pubDate) { 
      this.pubDate = pubDate; 
     } 

     public String getMedia_content() { 
      return media_content; 
     } 

     public void setMedia_content(String media_content) { 
      this.media_content = media_content; 
     } 

     public String getTitle() { 
      return title; 
     } 

     public String getLink() { 
      return link; 
     } 

     public String getDescription() { 
      return description; 
     } 
    } 

    public boolean isParsingComplete() { 
     return parsingComplete; 
    } 

    public void setParsingComplete(boolean parsingComplete) { 
     this.parsingComplete = parsingComplete; 
    } 


    public String getImage() { 
     return image; 
    } 

    public void setImage(String image) { 
     this.image = image; 
    } 

    public String getImageTitle() { 
     return imageTitle; 
    } 

    public void setImageTitle(String imageTitle) { 
     this.imageTitle = imageTitle; 
    } 

    public String getImageDescription() { 
     return imageDescription; 
    } 

    public void setImageDescription(String imageDescription) { 
     this.imageDescription = imageDescription; 
    } 

    public String getImageLink() { 
     return imageLink; 
    } 

    public void setImageLink(String imageLink) { 
     this.imageLink = imageLink; 
    } 

    public String getUrlString() { 
     return urlString; 
    } 

    public void setUrlString(String urlString) { 
     this.urlString = urlString; 
    } 

    public HandleXML(String url) { 
     this.urlString = url; 
    } 


    public void parseXMLAndStoreIt(XmlPullParser myParser) { 
     int event; 
     String text = null; 
     Item newItem = null; 

     try { 
      event = myParser.getEventType(); 

      while (event != XmlPullParser.END_DOCUMENT) { 
       String name = myParser.getName(); 

       switch (event) { 
        case XmlPullParser.START_TAG: 
         if (name.equalsIgnoreCase("channel")) { 
          // init the list for item. 
          itemList = new ArrayList<Item>(); 
         } 
         if (name.equalsIgnoreCase("item")) { 
          // create a new item object. 
          newItem = new Item(); 
          isImage = false; // isImage to false 
         } 
         break; 

        case XmlPullParser.TEXT: 
         text = myParser.getText(); 
         break; 

        case XmlPullParser.END_TAG: 

         if (!isImage && name.equals("title")) { 
          newItem.setTitle(text); 
          Log.e("Input text : ",text); 
         } 
         else if (!isImage && name.equals("link")) { 
          newItem.setLink(text); 
          Log.e("Input link : ",text); 
         } 
         else if (!isImage && name.equals("description")) { 
          newItem.setDescription(text); 
          Log.e("Input description : ",text); 
         } 
         else if (name.equals("image")) { 
          image = text; 
          isImage = true; 
          Log.e("Input image : ",text); 
         } 
         else if (isImage && name.equals("title")) { 
          imageTitle = text; 
          Log.e("Input image title : ",text); 
         } 
         else if (isImage && name.equals("description")) { 
          imageDescription = text; 
          Log.e("Input image descr : ",text); 
         } 
         else if (isImage && name.equals("link")) { 
          imageLink = text; 
          Log.e("Input link : ",text); 
         } 
         else if (!isImage && name.equals("guid")) { 
          newItem.setGuid(text); 
          Log.e("Input guid : ",text); 
         } 
         else if (!isImage && name.equals("pubDate")) { 
          newItem.setPubDate(text); 
          Log.e("Input pubdate : ",text); 
         } 
         else if (!isImage && name.equals("media:content")) { 
          newItem.setMedia_content(text); 
//       isImage = false; 
          Log.e("Input media:content : ",text); 
         } 
         else if (name.equalsIgnoreCase("item")) { 
          // add the item to list in the end of item tag 
          itemList.add(newItem); 
          Log.e("itemList", "Add one"); 
         } 
         break; 
       } 
       event = myParser.next(); 
      } 
      parsingComplete = false; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void fetchXML() { 
     Thread thread = new Thread(new Runnable() { 
      @Override 
      public void run() { 

       try { 
        URL url = new URL(urlString); 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

        conn.setReadTimeout(10000 /* milliseconds */); 
        conn.setConnectTimeout(15000 /* milliseconds */); 
        conn.setRequestMethod("GET"); 
        conn.setDoInput(true); 
        // Starts the query 
        conn.connect(); 
        InputStream stream = conn.getInputStream(); 

        xmlFactoryObject = XmlPullParserFactory.newInstance(); 
        XmlPullParser myparser = xmlFactoryObject.newPullParser(); 
        myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
        myparser.setInput(stream, "UTF_8"); 
        parseXMLAndStoreIt(myparser); 
        stream.close(); 
       } catch (Exception e) { 
       } 
      } 
     }); 
     thread.start(); 
    } 
} 
+0

這表明ITEMLIST大小總是0項永遠不會在列表中添加。 –

+0

請再次查看代碼段。我測試的項目將被添加到列表中。 – chancyWu