2017-05-05 28 views
0

我無法訪問內部標記,以獲取圖像「url」 在這裏我的標記名稱是「enclosure」,它包含另一個名爲「url」,這就是我想要得到的... here一整節課,我創建如何獲取包含XML標籤Android Studio的內部圖像url?

**public class ParseApplications { 
    private static final String TAG = "ParseApplications"; 

    private ArrayList<NewsFeeds> application; 

    public ParseApplications() { 
     this.application = new ArrayList<>(); 

    } 

    public ArrayList<NewsFeeds> getApplication() { 
     return application; 
    } 

    public boolean Parse(String xmlData) { 
     boolean status = true; 
     NewsFeeds currentNews = null; 
     boolean InEntry = false; 
     String textValue = ""; 
     boolean gotImage = false; 

     try { 
      // XmlPullParserFactory This class is used to create implementations of XML Pull Parser defined in XMPULL 
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      //coming line mean that the xml parse i will handle it by my code 
      /* 
      Specifies that the parser produced by this factory will provide support for XML namespaces. 
      By default the value of this is set to false. 
      Parameters 
      awareness 
      boolean: true if the parser produced by this code will provide support for XML namespaces; false otherwise. 
      */ 
      factory.setNamespaceAware(true); 
      //XML Pull Parser is an interface that defines parsing functionality provided in XMLPULL V1 API 
      //newPullParser is Creates a new instance of a XML Pull Parser using the currently configured factory features. 
      XmlPullParser xxp = factory.newPullParser(); 

      xxp.setInput(new StringReader(xmlData)); 
      //getEventType Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.). return int 
      int eventType = xxp.getEventType(); 
      while (eventType != XmlPullParser.END_DOCUMENT) { 
       String tagName = xxp.getName(); 
       switch (eventType) { 
        case XmlPullParser.START_TAG: 
         Log.d(TAG, "Parse: Starting tag for " + tagName); 
         if ("item".equalsIgnoreCase(tagName)) { 
          InEntry = true; 
          currentNews = new NewsFeeds(); 
         } 

         break; 
        case XmlPullParser.TEXT: 
         textValue = xxp.getText(); 
         break; 
        case XmlPullParser.END_TAG: 

         if (InEntry) { 
          if ("item".equalsIgnoreCase(tagName)) { 
           application.add(currentNews); 

          } else if ("title".equalsIgnoreCase(tagName)) { 
           currentNews.setName(textValue); 
          } else if ("pubdate".equalsIgnoreCase(tagName)) { 
           currentNews.setTheDate(textValue); 
          } else if ("description".equalsIgnoreCase(tagName)) { 
           currentNews.setSummry(textValue); 
          } else if ("link".equalsIgnoreCase(tagName)) { 
           currentNews.setTitle(textValue); 
          } else if ("enclosure".equalsIgnoreCase(tagName)) { 
           currentNews.setImageUrl(textValue); 
          } 
         } 
         break; 
        default: 
         //nothing to do 

       } 
       eventType = xxp.next(); 


      } 

      for (NewsFeeds app : application) { 
       Log.d(TAG, "*********************"); 
       Log.d(TAG, app.toString()); 
      } 
     } catch (Exception e) { 
      status = false; 
     } 
     return status; 
    } 
}** 

我無法訪問內標籤來獲取圖像「URL」我在這裏的標記名稱是「圈地」,它包含另一個一個名爲「URL」,這就是我想要得到...在這裏我創建了一個全班

+0

您可以發佈XML響應也或格式? – curiousMind

回答

0

我建議打印原始數據並查看標籤的層次結構。只是做一個日誌而不分析數據並自己閱讀。它可能會丟失,或者您在獲取之前需要訪問父標籤。

這也許看起來很基本,但你確定你輸入正確嗎? (大寫字母,空格等)

其他標籤怎麼樣?我想你正確地得到它們。

+0

我輸入正確.. –

+0

你得到什麼迴應(原始數據)? –

0

解決 只有兩個LINES

else if ("enclosure".equalsIgnoreCase(tagName)) { 
           String url = xxp.getAttributeValue(0); 
           currentNews.setImageUrl(url); 
          } 
相關問題