中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

本溪市| 惠安县| 册亨县| 久治县| 开化县| 崇左市| 泽普县| 渑池县| 凤冈县| 屏南县| 安康市| 汤阴县| 琼中| 文成县| 仁寿县| 蚌埠市| 万安县| 望奎县| 疏勒县| 聂拉木县| 丽水市| 财经| 梁河县| 郴州市| 白朗县| 长春市| 绥化市| 宣汉县| 瑞安市| 潼南县| 志丹县| 仙桃市| 高尔夫| 射阳县| 锡林郭勒盟| 岑巩县| 健康| 噶尔县| 迁安市| 分宜县| 永泰县|