使用xml从rss feed获取数据,然后存储在ArrayList中

时间:2011-09-21 07:43:16

标签: android rss xsd

我正在创建一个Android应用程序来从rss feed https://spreadsheets.google.com/feeds/list/0AgSdFM6D0vX4dE5iQ0FfMGVWWGpieDZJY2lHX3I1S3c/od6/private/full

获取数据

任何人都可以建议从xml中获取值gsx:brand并将其存储在Arraylist中

1 个答案:

答案 0 :(得分:2)

这是我所做的一个例子,你可以看看这个并相应地工作,

<强> MainRSSDemo.java

public class MainRSSDemo extends Activity {

    private List<Message> messages;
    static String feedUrl = "http://rss.news.yahoo.com/rss/terrorism";
    static final String RSS = "rss";
    static final String CHANNEL = "channel";
    static final  String ITEM = "item";
    static final String PUB_DATE = "pubDate";
    static final  String DESCRIPTION = "description";
    static final  String LINK = "link";
    static final  String TITLE = "title";
    static final  String URL = "url";
    static final  String MEDIA = "content";
    private InputStream is;
    private URL feed_Url = null;
    private ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv =(ListView)findViewById(R.id.ListView01);

        try {
                feed_Url = new URL(feedUrl);
                is =  feed_Url.openConnection().getInputStream();
                messages = parse();
                is.close();

        } catch (Exception e) {
            Log.i("err", e.toString());
            e.printStackTrace();
        }

        final List<String> titles = new ArrayList<String>(messages.size());
        for (Message msg : messages){
            titles.add(msg.getTitle());
        }
        lv.setAdapter(new ArrayAdapter<String>(this,R.layout.row,titles));
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {

                Toast.makeText(getBaseContext(),titles.get(position), Toast.LENGTH_LONG).show();

        }
        });
    }

    public List<Message> parse() {
        List<Message> messages = null;
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(is, null);
            int eventType = parser.getEventType();
            Message currentMessage = null;
            boolean done = false;
            while (eventType != XmlPullParser.END_DOCUMENT && !done){
                String name = null;
                switch (eventType){
                    case XmlPullParser.START_DOCUMENT:
                        messages = new ArrayList<Message>();
                        break;
                    case XmlPullParser.START_TAG:
                        name = parser.getName();
                    if (name.equalsIgnoreCase(ITEM)) {
                        currentMessage = new Message();
                    } else if (currentMessage != null) {
                        if (name.equalsIgnoreCase(LINK)) {
                            currentMessage.setLink(parser.nextText());
                        } else if (name.equalsIgnoreCase(DESCRIPTION)) {
                            String parse = parser.nextText().toString();
                            if (parse.contains("</a>")) {
                                parse = parse.substring(parse.indexOf("</a>") + 4, parse.lastIndexOf("</p>"));
                                currentMessage.setDescription(parse.toString());
                            } else {
                                currentMessage.setDescription(parse);
                            }
                        } else if (name.equalsIgnoreCase(PUB_DATE)) {
                            currentMessage.setDate(parser.nextText());
                        } else if (name.equalsIgnoreCase(TITLE)) {
                            currentMessage.setTitle(parser.nextText());
                        } else if (name.equalsIgnoreCase(MEDIA)) {
                            currentMessage.setImageUrl(parser.getAttributeValue(0));
                            parser.nextText();
                        }
                    }
                        break;
                    case XmlPullParser.END_TAG:
                        name = parser.getName();
                        if (name.equalsIgnoreCase(ITEM) && currentMessage != null){
                            messages.add(currentMessage);
                        } else if (name.equalsIgnoreCase(CHANNEL)){
                            done = true;
                        }
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {

            Log.e("AndroidNews::PullFeedParser", e.getMessage(), e);
            throw new RuntimeException(e);
        }
        return messages;
    }
}

<强> Message.java

public class Message implements Comparable<Message> {
    static SimpleDateFormat FORMATTER = new SimpleDateFormat(
            "EEE, dd MMM yyyy HH:mm:ss Z");
    private String title;
    private URL link;
    private String description;
    private Date date;
    private String image_url;

    public void setImageUrl(String ImageUrl) {
        image_url = ImageUrl;
    }

    public String getImageUrl() {
        return image_url;
    }

    public String getTitle() {
        return title;
    }

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

    // getters and setters omitted for brevity
    public URL getLink() {
        return link;
    }

    public void setLink(String link) {
        try {
            this.link = new URL(link);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public String getDescription() {
        return description;
    }

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

    public String getDate() {
        return FORMATTER.format(this.date);
    }

    public void setDate(String date) {
        // pad the date if necessary
        while (!date.endsWith("00")) {
            date += "0";
        }
        try {
            this.date = FORMATTER.parse(date.trim());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public Message copy() {
        Message copy = new Message();
        copy.title = title;
        copy.link = link;
        copy.description = description;
        copy.date = date;

        return copy;
    }

    public int compareTo(Message another) {
        if (another == null)
            return 1;
        // sort descending, most recent first
        return another.date.compareTo(date);
    }
}