如何获取包含XML标签Android Studio的内部图像url?

问题描述:

我无法访问内部标记,以获取图像“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

你得到什么回应(原始数据)? –

解决 只有两个LINES

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