XML解析之PULL

发布时间:2020-06-29 05:52:46 作者:张立达
来源:网络 阅读:170

在Android中极力推荐的xmlpull方式解析xml。

为什么 STAX 解析方式 效率 好于 SAX ?

1、SAX 无选择性的,所有事件都会处理 解析方式,Stax 由用户控制需要处理事件类型

2、在使用Stax进行数据解析时,随时终止解析


        环境:        

    xmlpull_1_1_3_4c.jar

http://www.xmlpull.org/    xmlpull

XML文件:

XML解析之PULL

<?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>

XML解析之PULL

XML对应的实体文件:

XML解析之PULL

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;
    }
    
}

XML解析之PULL

Pull解析代码

XML解析之PULL

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());
        }
    }
    
}

XML解析之PULL

Event_Type事件返回的事件代码:

XML解析之PULL

public static final java.lang.String NO_NAMESPACE = "";  
  public static final int START_DOCUMENT = 0;  
  public static final int END_DOCUMENT = 1;  
  public static final int START_TAG = 2;  
  public static final int END_TAG = 3;  
  public static final int TEXT = 4;  
  public static final int CDSECT = 5;  
  public static final int ENTITY_REF = 6;  
  public static final int IGNORABLE_WHITESPACE = 7;  
  public static final int PROCESSING_INSTRUCTION = 8;  
  public static final int COMMENT = 9;  
  public static final int DOCDECL = 10;

XML解析之PULL

对XML数据通过PULL解析器进行CURD示意图:XML解析之PULL


推荐阅读:
  1. docker:更换docker的pull源,轻松pull
  2. AndroidManifest.xml解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

用户 android 选择性

上一篇:Angular 4 表单验证

下一篇:array_merge函数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》