-<学生花名册>"> -<学生花名册>" />
欢迎来到天天文库
浏览记录
ID:39801988
大小:63.50 KB
页数:12页
时间:2019-07-11
《android读写XML 收藏》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、android读写XML收藏input.xml文件存放学生信息viewplaincopytoclipboardprint?-<学生花名册>-<学生性别="男"><姓名>吴凤龙姓名><年龄>30年龄><电话>68888888电话>学生>-<学生性别="男"><姓名>张井满姓名><年龄>32年龄><电话>88888888电话>学生>学生花名册>
2、"UTF-8"standalone="no"?>-<学生花名册>-<学生性别="男"><姓名>吴凤龙姓名><年龄>30年龄><电话>68888888电话>学生>-<学生性别="男"><姓名>张井满姓名><年龄>32年龄><电话>88888888电话>学生>学生花名册>读取XML的代码:viewplaincopytoclipboardprint?privateArrayListreadSMSXMLFile(Stringpath,Stringfilename){FileinFil
3、e=newFile(path,filename);try{inFile.createNewFile();}catch(IOExceptione){e.printStackTrace();}//为解析XML作准备,创建DocumentBuilderFactory实例,指定DocumentBuilderDocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();DocumentBuilderdb=null;try{db=dbf.newDocumentBuilder()
4、;}catch(ParserConfigurationExceptionpce){System.err.println(pce);//出异常时输出异常信息,然后退出,下同System.exit(1);}Documentdoc=null;try{doc=db.parse(inFile);}catch(DOMExceptiondom){System.err.println(dom.getMessage());System.exit(1);}catch(IOExceptionioe){System.err.println(ioe);S
5、ystem.exit(1);}//下面是解析XML的全过程,比较简单,先取根元素"学生花名册"Elementroot=doc.getDocumentElement();//取"学生"元素列表NodeListstudents=root.getElementsByTagName("学生");for(inti=0;i6、entBean=newStudentBean();//取学生的性别属性studentBean.setSex(student.getAttribute("性别"));//取"姓名"元素,下面类同NodeListnames=student.getElementsByTagName("姓名");if(names.getLength()==1){Elemente=(Element)names.item(0);Textt=(Text)e.getFirstChild();studentBean.setName(t.getNodeValue(7、));}NodeListages=student.getElementsByTagName("年龄");if(ages.getLength()==1){Elemente=(Element)ages.item(0);Textt=(Text)e.getFirstChild();studentBean.setAge(Integer.parseInt(t.getNodeValue()));}NodeListphones=student.getElementsByTagName("电话");if(phones.getLength()==18、){Elemente=(Element)phones.item(0);Textt=(Text)e.getFirstChild();studentBean.setPhone(t.getNodeValue());}student_Vector.add(student
6、entBean=newStudentBean();//取学生的性别属性studentBean.setSex(student.getAttribute("性别"));//取"姓名"元素,下面类同NodeListnames=student.getElementsByTagName("姓名");if(names.getLength()==1){Elemente=(Element)names.item(0);Textt=(Text)e.getFirstChild();studentBean.setName(t.getNodeValue(
7、));}NodeListages=student.getElementsByTagName("年龄");if(ages.getLength()==1){Elemente=(Element)ages.item(0);Textt=(Text)e.getFirstChild();studentBean.setAge(Integer.parseInt(t.getNodeValue()));}NodeListphones=student.getElementsByTagName("电话");if(phones.getLength()==1
8、){Elemente=(Element)phones.item(0);Textt=(Text)e.getFirstChild();studentBean.setPhone(t.getNodeValue());}student_Vector.add(student
此文档下载收益归作者所有