在Android中極力推薦的xmlpull方式解析xml。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、金塔網(wǎng)站維護(hù)、網(wǎng)站推廣。為什么 STAX 解析方式 效率 好于 SAX ?
1、SAX 無選擇性的,所有事件都會處理 解析方式,Stax 由用戶控制需要處理事件類型
2、在使用Stax進(jìn)行數(shù)據(jù)解析時,隨時終止解析
環(huán)境:
xmlpull_1_1_3_4c.jar
http://www.xmlpull.org/ xmlpull
XML文件:
張三 男 18 zhangsan@163.com 1987-06-08 好學(xué)生 李四 女 18 lisi@163.com 1987-06-08 好學(xué)生 小王 男 18 xiaowang@163.com 1987-06-08 好學(xué)生 小張 男 18 xiaozhang@163.com 1987-06-08 好學(xué)生 小明 男 18 xiaoming@163.com 1987-06-08 好學(xué)生
XML對應(yīng)的實(shí)體文件:
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; } }
Pull解析代碼
public class PullParseXml { public ListPullParseXML(){ List list=null; Student student = null; //構(gòu)建XmlPullParserFactory try { XmlPullParserFactory pullParserFactory=XmlPullParserFactory.newInstance(); //獲取XmlPullParser的實(shí)例 XmlPullParser xmlPullParser=pullParserFactory.newPullParser(); //設(shè)置輸入流 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 (); break; //開始節(jié)點(diǎn) case XmlPullParser.START_TAG: //判斷如果其實(shí)節(jié)點(diǎn)為student if("student".equals(nodeName)){ //實(shí)例化student對象 student=new Student(); //設(shè)置Id屬性 student.setId(Integer.parseInt(xmlPullParser.getAttributeValue(0))); //設(shè)置Group屬性 student.setGroup(Integer.parseInt(xmlPullParser.getAttributeValue(1))); }else if("name".equals(nodeName)){ //設(shè)置name student.setName(xmlPullParser.nextText()); }else if("sex".equals(nodeName)){ //設(shè)置sex student.setSex(xmlPullParser.nextText()); }else if("age".equals(nodeName)){ //設(shè)置age student.setAge(Integer.parseInt(xmlPullParser.nextText())); }else if("email".equals(nodeName)){ //設(shè)置email student.setEmail(xmlPullParser.nextText()); }else if("birthday".equals(nodeName)){ //設(shè)置birthday student.setBirthday(xmlPullParser.nextText()); }else if("memo".equals(nodeName)){ //設(shè)置memo屬性 student.setMemo(xmlPullParser.nextText()); } break; //結(jié)束節(jié)點(diǎn) 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 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()); } } }
Event_Type事件返回的事件代碼:
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數(shù)據(jù)通過PULL解析器進(jìn)行CURD示意圖:
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。