[置顶] Android学习系列-Android中解析xml(7)

Android学习系列-Android中解析xml(7)
一,概述
1,一个是DOM,它是生成一个树,有了树以后你搜索、查找都可以做。
2,另一种是基于流的,就是解析器从头到尾解析一遍xml文件。
 基于流的目前有两种比较有名,
(1)是SAX,
 sax的原理是解析器解析过程中通过回调把tag/value值等传给你,你可以比较、操作。
(2)PULL
 而pull的原理是它只告诉你一个tag开始或者结束了,至于tag/value的值是什么需要你自己去向parser问,所以叫做pull,而sax看起来是push给你的。

sax/pull还有一点区别,你随便找个sax和pull的例子比较一下就可以发现,pull是一个while循环,随时可以跳出,而sax不是,sax是只要解析了,就必须解析完成。
Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码

被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型元素的值。
 
二,下面只要介绍xmlpull方式解析xml
1.在Android中应用推荐的xmlpull方式解析xml。
        环境:        
    kxml2-2.3.0.jar

xmlpull_1_1_3_4c.jar

以上两个jar分别从以下网站下载:

http://www.xmlpull.org/    xmlpull

http://kxml.sourceforge.net/    kxml

2.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <student id="1" group="1">
        <name>张三</name>
        <sex>男</sex>
        <age>18</age>
        <email>zhangsan@163.com</email>
        <birthday>1987-06-08</birthday>
        <memo>好学生</memo>
    </student>
    <student id="2" group="2">
        <name>李四</name>
        <sex>女</sex>
        <age>18</age>
        <email>lisi@163.com</email>
        <birthday>1987-06-08</birthday>
        <memo>好学生</memo>
    </student>
    <student id="3" group="3">
        <name>小王</name>
        <sex>男</sex>
        <age>18</age>
        <email>xiaowang@163.com</email>
        <birthday>1987-06-08</birthday>
        <memo>好学生</memo>
    </student>
    <student id="4" group="4">
        <name>小张</name>
        <sex>男</sex>
        <age>18</age>
        <email>xiaozhang@163.com</email>
        <birthday>1987-06-08</birthday>
        <memo>好学生</memo>
    </student>
    <student id="5" group="5">
        <name>小明</name>
        <sex>男</sex>
        <age>18</age>
        <email>xiaoming@163.com</email>
        <birthday>1987-06-08</birthday>
        <memo>好学生</memo>
    </student>
</root>

3.Xml对应的实体bean文件:

package com.parsexml.entity;

public class Student {
   
    private int id;
    private int group;
    private String name;
    private String sex;
    private int age;
    private String email;
    private String memo;
    private String birthday;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getGroup() {
        return group;
    }
    public void setGroup(int group) {
        this.group = group;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getMemo() {
        return memo;
    }
    public void setMemo(String memo) {
        this.memo = memo;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
   
}

4.Pull解析代码

package com.parsexml;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.parsexml.entity.Student;

public class PullParseXml {
   
   
    public List<Student> PullParseXML(){
       
        List<Student> list=null;
        Student student = null;
       
        //构建XmlPullParserFactory
        try {
            XmlPullParserFactory pullParserFactory=XmlPullParserFactory.newInstance();
            //获取XmlPullParser的实例
            XmlPullParser xmlPullParser=pullParserFactory.newPullParser();
            //设置输入流  xml文件
            xmlPullParser.setInput(Thread.currentThread().getContextClassLoader().getResourceAsStream("Student.xml"), "UTF-8");
           
            //开始
            int eventType=xmlPullParser.getEventType();
           
            try {
                while(eventType!=XmlPullParser.END_DOCUMENT){
                    String nodeName=xmlPullParser.getName();
                    switch (eventType) {
                    //文档开始
                    case XmlPullParser.START_DOCUMENT:
                        list=new ArrayList<Student>();
                        break;
                    //开始节点
                    case XmlPullParser.START_TAG:
                        //判断如果其实节点为student
                        if("student".equals(nodeName)){
                            //实例化student对象
                            student=new Student();
                            //设置Id属性
                            student.setId(Integer.parseInt(xmlPullParser.getAttributeValue(0)));
                            //设置Group属性
                            student.setGroup(Integer.parseInt(xmlPullParser.getAttributeValue(1)));
                        }else if("name".equals(nodeName)){
                            //设置name
                            student.setName(xmlPullParser.nextText());
                        }else if("sex".equals(nodeName)){
                            //设置sex
                            student.setSex(xmlPullParser.nextText());
                        }else if("age".equals(nodeName)){
                            //设置age
                            student.setAge(Integer.parseInt(xmlPullParser.nextText()));
                        }else if("email".equals(nodeName)){
                            //设置email
                            student.setEmail(xmlPullParser.nextText());
                        }else if("birthday".equals(nodeName)){
                            //设置birthday
                            student.setBirthday(xmlPullParser.nextText());
                        }else if("memo".equals(nodeName)){
                            //设置memo属性
                            student.setMemo(xmlPullParser.nextText());
                        }
                        break;
                    //结束节点
                    case XmlPullParser.END_TAG:
                        if("student".equals(nodeName)){
                            list.add(student);
                            student=null;
                        }
                        break;
                    default:
                        break;
                    }
                    eventType=xmlPullParser.next();
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

return list;
    }
   
    public static void main(String[] args) {
        List<Student> list=new PullParseXml().PullParseXML();
        for(Student student:list){
            System.out.println("id:"+student.getId()+"\tgroup:"+student.getGroup()+"\tname:"+student.getName()+"\tsex:"+student.getSex

()+"\tage:"+student.getAge()+"\temail:"+student.getEmail()+"\tbirthday:"+student.getBirthday()+"\tmemo:"+student.getMemo());
        }
    }
   
}

上一篇:【POJ】3630 Phone List


下一篇:面试官:你了解Webpack吗?