关闭

xml的一中解析方法pull

85人阅读 评论(0) 收藏 举报
private void ToXML(String result){
        try {
            XmlPullParser pull=Xml.newPullParser();
            ByteArrayInputStream in=new ByteArrayInputStream(result.getBytes());
            pull.setInput(in,null);
            int type=pull.getEventType();
            while (type!=XmlPullParser.END_DOCUMENT){
                String name=pull.getName();
                switch (type){
                    case XmlPullParser.START_DOCUMENT:
                    break;
                    case XmlPullParser.START_TAG:
                        if (name.equals("item")){
                            item=new ItemNews();
                        }else if (name.equals("title")){
                            String title=pull.nextText();
                            item.setTitle(title);
                            System.out.println("---------------"+title);
                        }else if (name.equals("link")){
                            String link=pull.nextText();
                            item.setLink(link);
                        }else if (name.equals("copyright")){
                            String copyright=pull.nextText();
                            item.setCopyright(copyright);
                        }else if (name.equals("pubDate")){
                            String pubDate=pull.nextText();
                            item.setPubDate(pubDate);
                        }
                    break;
                    case XmlPullParser.END_TAG:
                        if (name.equals("item")){
                            list.add(item);
                        }
                    break;
                }
                type=pull.next();
            }
            in.close();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3979次
    • 积分:112
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:23篇
    • 译文:0篇
    • 评论:0条