編輯:關於Android編程
在Android中極力推薦的xmlpull方式解析xml。xmlpull不僅僅可以使用在Android上,同樣也適用於javase,但在javase環境下,你需要自己去獲取xmlpull所依賴的類庫。
環境:
kxml2-2.3.0.jar
xmlpull_1_1_3_4c.jar
以上兩個jar分別從以下網站下載:
http://www.xmlpull.org/ xmlpull
http://kxml.sourceforge.net/ kxml
xml文件:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%2D%2D%3E--> <root> <student id="1" group="1"> <name>張三</name> <sex>男</sex> <age>18</age> <email>[email protected]</email> <birthday>1987-06-08</birthday> <memo>好學生</memo> </student> <student id="2" group="2"> <name>李四</name> <sex>女</sex> <age>18</age> <email>[email protected]</email> <birthday>1987-06-08</birthday> <memo>好學生</memo> </student> <student id="3" group="3"> <name>小王</name> <sex>男</sex> <age>18</age> <email>[email protected]</email> <birthday>1987-06-08</birthday> <memo>好學生</memo> </student> <student id="4" group="4"> <name>小張</name> <sex>男</sex> <age>18</age> <email>[email protected]</email> <birthday>1987-06-08</birthday> <memo>好學生</memo> </student> <student id="5" group="5"> <name>小明</name> <sex>男</sex> <age>18</age> <email>[email protected]</email> <birthday>1987-06-08</birthday> <memo>好學生</memo> </student> </root>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; } }
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 ListPullParseXML(){ List 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 (); 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 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()); } } }
注:若工程中缺少kxml2-2.3.0.jar文件則會報以下異常信息:
你應該添加kxml2-2.3.0.jar包到你的classpath下
本文實例為大家分享了Android中使用定時器的三種方法,供大家參考,具體內容如下圖示:因為都比較簡單,所以就直接貼代碼(慮去再次點擊停止的操作),有個全局的Handle
Android打包apk時,有時候需要打各種渠道包,例如 豌豆莢、華為手機市場、小米市場、360市場等,那麼每一種渠道打包時,都需要配置不同的數據元,如果渠道較多,那麼打
飛信,是中國移動推出的一款聊天工具,通過手機號注冊賬號然後跟通訊錄的朋友發短信。這個功能就是可以免費發短信,但是前提條件必須是加為了飛信好友。那麼小編就給大
PowerManagerService負責Android系統中電源管理方面的工作,為了簡便我們在下文中將其簡稱為PMS。我們先大致了解一下PMS在Android中的整體結