关闭

XML解析--Pull

标签: xmlAndroid
215人阅读 评论(0) 收藏 举报
分类:

1:简单点的: 参照信息 :http://apis.juhe.cn/goodbook/querykey=9d6ef8c31647a206e05fcaff70527182&catalog_id=244&pn=10&rn=20&dtype=xml

   /**
     * 解析xml
     * @param xml
     */
    private void pullMessage(String xml) {
        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
        //实例化pull对象
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(bais,"utf-8");
            //获取第一个产生事件
            int eventType = parser.getEventType();
            //判断事件的类型
            while(eventType!=XmlPullParser.END_DOCUMENT){
                //获取节点名称
                String name = parser.getName();
                switch (eventType){
                    case XmlPullParser.START_DOCUMENT:
                        //实例化集合
                        booksList = new ArrayList<BooksMessage>();
                        break;
                    case XmlPullParser.START_TAG:
                        if("item".equals(name)){//实例化对象
                            book = new BooksMessage();
                        }
                        if("title".equals(name)){
                            book.title = parser.nextText();
                        }else  if("sub2".equals(name)){
                            book.sub2 = parser.nextText();
                        }else  if("img".equals(name)){
                            book.img = parser.nextText();
                        }else  if("reading".equals(name)){
                            book.reading = parser.nextText();
                        }else  if("bytime".equals(name)){
                            book.bytime = parser.nextText();
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if("item".equals(name)){//把获取的信息保存到集合
                           booksList.add(book);
                            book = null;
                        }
                        break;
                }
                //重新为事件赋值
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        booksAll.addAll(booksList);
        //创建适配器,展示数据
        if(adapter == null){
            adapter = new BookAdapter(MainActivity.this,booksAll);
            lv.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

2:复杂点的(直接把信息放到 assets目录下)

<?xml version="1.0" encoding="UTF-8"?>  
<books xmlns:android="http://schemas.android.com/apk/res/android" >  
    <book id="1">  
        <name>thinking in java</name>  
        <price>85.5</price>  
        <type>电子书籍</type>
        <student>
            <age>19</age>
            <name>zhangsan</name>
        </student>
    </book>  
    <book id="2">  
        <name>Spring in Action</name>  
        <price>39.0</price>  
    <type>电子书籍</type>
            <student>
            <age>19</age>
            <name>zhangsan</name>
        </student>
    </book> 
    <book id="3">  
        <name>Android疯狂讲义</name>  
        <price>79.0</price>  
    <type>电子书籍</type>
            <student>
            <age>19</age>
            <name>zhangsan</name>
        </student>
    </book> 
</books> 

解析

//BooksBean
public class BooksBean {
    public String id;
    public String name;
    public String price;
    public String type;

    public StudentBean student;
    public class StudentBean{
        public String age;
        public String name;
        @Override
        public String toString() {
            return "StudentBean [age=" + age + ", name=" + name + "]";
        }
    }
    @Override
    public String toString() {
        return "BooksBean [id=" + id + ", name=" + name + ", price=" + price
                + ", type=" + type + ", student=" + student + "]";
    }
}
//解析代码
    private void StreamXml() {

        System.out.println("开始解析");
        try {
            // 创建pull对象
            XmlPullParser parser = Xml.newPullParser();
//从网上获取需要转成字节流   ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
            //获取assets目录下xml文件,设置字符编码格式
            parser.setInput(getAssets().open("books.xml"), "utf-8");
            // 获取第一个产生的事件
            int eventType = parser.getEventType();
            // 判断是不是xml的结束标签
            while (eventType != XmlPullParser.END_DOCUMENT) {
                System.out.println("进入循环");
                String name = parser.getName();
                switch (eventType) {
                // xml声明标签
                case XmlPullParser.START_DOCUMENT:
                    System.out.println("START_DOCUMENT");
                    //初始化集合
                    books = new ArrayList<BooksBean>();
                    break;
                    //文档开始标签
                case XmlPullParser.START_TAG:
                    System.out.println("START_TAG");
                    if("book".equals(name)){
                        //初始化对象
                        book = new BooksBean();
                        book.id = parser.getAttributeValue(0);
                    }
                    if("name".equals(name)){
                        book.name = parser.nextText();
                    }else if("price".equals(name)){
                        book.price = parser.nextText();
                    }else if("type".equals(name)){
                        book.type = parser.nextText();
                    }else if("student".equals(name)){
                        if(book!=null && "age".equals(name)){
                            book.student.age = parser.nextText();
                        }else if(book!=null && "name".equals(name)){
                            book.student.name = parser.nextText();
                        }
                    }
                    break;
                    //文档结束标签
                case XmlPullParser.END_TAG:
                    System.out.println("END_TAG");
                    if("book".equals(name)){
                        //把信息保存到集合
                        books.add(book);
                        //把对象重新赋值为空
                        book = null;
                    }
                    break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3788次
    • 积分:269
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:2篇
    • 译文:3篇
    • 评论:1条
    文章分类
    最新评论