android读写XML 收藏.doc
上传人:qw****27 上传时间:2024-09-12 格式:DOC 页数:12 大小:63KB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

android读写XML 收藏.doc

android读写XML收藏.doc

预览

免费试读已结束,剩余 2 页请下载文档后查看

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

android读写XML收藏input.xml文件存放学生信息viewplaincopytoclipboardprint?<?xmlversion="1.0"encoding="UTF-8"standalone="no"?>-<学生花名册>-<学生性别="男"><姓名>吴凤龙</姓名><年龄>30</年龄><电话>68888888</电话></学生>-<学生性别="男"><姓名>张井满</姓名><年龄>32</年龄><电话>88888888</电话></学生></学生花名册><?xmlversion="1.0"encoding="UTF-8"standalone="no"?>-<学生花名册>-<学生性别="男"><姓名>吴凤龙</姓名><年龄>30</年龄><电话>68888888</电话></学生>-<学生性别="男"><姓名>张井满</姓名><年龄>32</年龄><电话>88888888</电话></学生></学生花名册>读取XML的代码:viewplaincopytoclipboardprint?privateArrayList<SMSUnit>readSMSXMLFile(Stringpath,Stringfilename){FileinFile=newFile(path,filename);try{inFile.createNewFile();}catch(IOExceptione){e.printStackTrace();}//为解析XML作准备,创建DocumentBuilderFactory实例,指定DocumentBuilderDocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();DocumentBuilderdb=null;try{db=dbf.newDocumentBuilder();}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);System.exit(1);}//下面是解析XML的全过程,比较简单,先取根元素"学生花名册"Elementroot=doc.getDocumentElement();//取"学生"元素列表NodeListstudents=root.getElementsByTagName("学生");for(inti=0;i<students.getLength();i++){//依次取每个"学生"元素Elementstudent=(Element)students.item(i);//创建一个学生的Bean实例StudentBeanstudentBean=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());}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){Elemente=(Element)phones.item(0);Textt=(Te