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

时间:2017-05-05 16:31:29

标签: android xml-parsing rss

我无法访问内部代码以获取图片“url” 这里我的标签名称是“enclosure”,它包含另一个名为“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”这里我的标记名称是“enclosure”,它包含另一个名为“url”的文件,这就是我想要的...这里是一个整体我创建的课程

2 个答案:

答案 0 :(得分:0)

我建议打印原始数据并查看标签的层次结构。只需做一个日志而不解析数据并自己阅读。它可能会丢失,或者您需要在获取之前访问父标记。

这也许看起来很基本,但你确定你输入正确吗? (大写字母,空格等)

其他标签怎么样?我猜你是正确的。

答案 1 :(得分:0)

解决 只有两条线

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