XML解析之PULL
在Android中极力推荐的xmlpull方式解析xml。
为什么 STAX 解析方式 效率 好于 SAX ?
1、SAX 无选择性的,所有事件都会处理 解析方式,Stax 由用户控制需要处理事件类型
2、在使用Stax进行数据解析时,随时终止解析
环境:
xmlpull_1_1_3_4c.jar
http://www.xmlpull.org/ xmlpull
XML文件:
<?xmlversion="1.0"encoding="UTF-8"?><root><studentid="1"group="1"><name>张三</name><sex>男</sex><age>18</age><email>zhangsan@163.com</email><birthday>1987-06-08</birthday><memo>好学生</memo></student><studentid="2"group="2"><name>李四</name><sex>女</sex><age>18</age><email>lisi@163.com</email><birthday>1987-06-08</birthday><memo>好学生</memo></student><studentid="3"group="3"><name>小王</name><sex>男</sex><age>18</age><email>xiaowang@163.com</email><birthday>1987-06-08</birthday><memo>好学生</memo></student><studentid="4"group="4"><name>小张</name><sex>男</sex><age>18</age><email>xiaozhang@163.com</email><birthday>1987-06-08</birthday><memo>好学生</memo></student><studentid="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对应的实体文件:
publicclassStudent{privateintid;privateintgroup;privateStringname;privateStringsex;privateintage;privateStringemail;privateStringmemo;privateStringbirthday;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicintgetGroup(){returngroup;}publicvoidsetGroup(intgroup){this.group=group;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetSex(){returnsex;}publicvoidsetSex(Stringsex){this.sex=sex;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetEmail(){returnemail;}publicvoidsetEmail(Stringemail){this.email=email;}publicStringgetMemo(){returnmemo;}publicvoidsetMemo(Stringmemo){this.memo=memo;}publicStringgetBirthday(){returnbirthday;}publicvoidsetBirthday(Stringbirthday){this.birthday=birthday;}}
Pull解析代码
publicclassPullParseXml{publicList<Student>PullParseXML(){List<Student>list=null;Studentstudent=null;//构建XmlPullParserFactorytry{XmlPullParserFactorypullParserFactory=XmlPullParserFactory.newInstance();//获取XmlPullParser的实例XmlPullParserxmlPullParser=pullParserFactory.newPullParser();//设置输入流xml文件xmlPullParser.setInput(Thread.currentThread().getContextClassLoader().getResourceAsStream("Student.xml"),"UTF-8");//开始inteventType=xmlPullParser.getEventType();try{while(eventType!=XmlPullParser.END_DOCUMENT){StringnodeName=xmlPullParser.getName();switch(eventType){//文档开始caseXmlPullParser.START_DOCUMENT:list=newArrayList<Student>();break;//开始节点caseXmlPullParser.START_TAG://判断如果其实节点为studentif("student".equals(nodeName)){//实例化student对象student=newStudent();//设置Id属性student.setId(Integer.parseInt(xmlPullParser.getAttributeValue(0)));//设置Group属性student.setGroup(Integer.parseInt(xmlPullParser.getAttributeValue(1)));}elseif("name".equals(nodeName)){//设置namestudent.setName(xmlPullParser.nextText());}elseif("sex".equals(nodeName)){//设置sexstudent.setSex(xmlPullParser.nextText());}elseif("age".equals(nodeName)){//设置agestudent.setAge(Integer.parseInt(xmlPullParser.nextText()));}elseif("email".equals(nodeName)){//设置emailstudent.setEmail(xmlPullParser.nextText());}elseif("birthday".equals(nodeName)){//设置birthdaystudent.setBirthday(xmlPullParser.nextText());}elseif("memo".equals(nodeName)){//设置memo属性student.setMemo(xmlPullParser.nextText());}break;//结束节点caseXmlPullParser.END_TAG:if("student".equals(nodeName)){list.add(student);student=null;}break;default:break;}eventType=xmlPullParser.next();}}catch(NumberFormatExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}catch(XmlPullParserExceptione){e.printStackTrace();}returnlist;}publicstaticvoidmain(String[]args){List<Student>list=newPullParseXml().PullParseXML();for(Studentstudent: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());}}}
Event_Type事件返回的事件代码:
publicstaticfinaljava.lang.StringNO_NAMESPACE="";publicstaticfinalintSTART_DOCUMENT=0;publicstaticfinalintEND_DOCUMENT=1;publicstaticfinalintSTART_TAG=2;publicstaticfinalintEND_TAG=3;publicstaticfinalintTEXT=4;publicstaticfinalintCDSECT=5;publicstaticfinalintENTITY_REF=6;publicstaticfinalintIGNORABLE_WHITESPACE=7;publicstaticfinalintPROCESSING_INSTRUCTION=8;publicstaticfinalintCOMMENT=9;publicstaticfinalintDOCDECL=10;
对XML数据通过PULL解析器进行CURD示意图:
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。