2017-08-23 13 views
0

私は使用していますenter link description hereネストされたRSSフィードを解析するには?

URLから必要なすべてのデータを取得できます。しかし、私はリストとして<item>...</item>属性を取得したい。すべての属性を1つずつ取得できます。アイテム属性の完全なリストが必要です。 他のオプションは同じですか?

ここには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)チャンネルタグの先頭で、1つの項目リストを初期化します。 3)アイテムタグの冒頭に新しいアイテムオブジェクトを作成します。 4)Itemオブジェクトをそれに応じて設定します。 5)itemタグの最後にItemオブジェクトを追加します。

変数isImageに注意する必要があります。 initがtrueである必要があります。一度アイテムオブジェクトを作成すると、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

関連する問題