总结篇,android sd卡数据的读取,
权限的问题我在这里就不赘述了,写入和读取都需要权限,需要在配置文件里进行配置,这里不再配置
只展示主代码:
package com.example.xmlser; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import android.os.Environment; import android.util.Log; import android.util.Xml; import com.example.xmlser.domain.Person; public class xmlSerier { //本列是想android内存卡写入xml文件,需要用到xml序列化类 //xmlXmlSerializer,这个类是单列设计,只能通过Xml得到 //创建数据源 public List<Person> getPersonList(){ List<Person> list=new ArrayList(); for(int i=0;i<30;i++){ Person per=new Person(); per.setName(""+i); per.setAge(i); if(i%2==0){ per.setSex("男"); }else{ per.setSex("女"); } list.add(per); } return list; }; //写入方法 public void peosonToXml(List<Person> personList){ //获得序列化对象 XmlSerializer xmlSerializer=Xml.newSerializer(); try { //指定序列化地址和编码 File path=new File(Environment.getExternalStorageDirectory(),"person.xml"); Log.i("err2",path.toString()); FileOutputStream out= new FileOutputStream(path.toString()); //设置字节输出流,指定字符集 xmlSerializer.setOutput(out,"utf-8"); //设置doucument,开始写入 xmlSerializer.startDocument("utf-8",true); //指定根节点 xmlSerializer.startTag(null,"persons"); //循环加入person节点 for(Person per:personList){ xmlSerializer.startTag(null,"person"); //设置名字 xmlSerializer.attribute(null,"id",per.getName()); //设置年龄 xmlSerializer.startTag(null,"age"); xmlSerializer.text(String.valueOf(per.getAge())); xmlSerializer.endTag(null,"age"); //设置sex xmlSerializer.startTag(null,"sex"); xmlSerializer.text(String.valueOf(per.getSex())); xmlSerializer.endTag(null,"sex"); xmlSerializer.endTag(null,"person"); } //结束根节点 xmlSerializer.endTag(null,"persons"); //结束写入 xmlSerializer.endDocument(); out.close(); Log.i("drr","正确了"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("err","c成功"+e.toString()); } } public List<Person> readPersonList(){ /*<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <persons> <person id="0"> <age>0</age> <sex>男</sex> </person> </persons>*/ //指定想xml文件路径 File path=new File(Environment.getExternalStorageDirectory(),"person.xml"); //获取解析器 XmlPullParser read=Xml.newPullParser(); try { //设置解析器读取路径及编码 read.setInput(new FileInputStream(path),"utf-8"); //声明List<person>集合和Person对象,方便下面使用 //利于节省内存 List<Person> personList=null; Person person=null; //获取事件类型 int eventType=read.getEventType(); while(eventType!=XmlPullParser.END_DOCUMENT){ //获取当前节点的名称 String eleName=read.getName(); switch (eventType) { case XmlPullParser.START_TAG:{ if(eleName.equals("persons")){//persons //遇到更节点创建List集合 personList=new ArrayList<Person>(); }else if(eleName.equals("person")){//person //遇到person节点,创建person对象 person=new Person(); //设置id属性 int id=Integer.parseInt(read.getAttributeValue(null,"id")); person.setId(id); }else if(eleName.equals("age")){//age //获取age节点内容,并设置给person person.setAge(Integer.parseInt(read.nextText())); }else if(eleName.equals("sex")){//sex ////获取sex节点内容,并设置给person person.setSex(read.nextText()); } } break; case XmlPullParser.END_TAG:{ //当一个person节点读取结束时,将person对象加入集合中 if(eleName.equals("person")){ personList.add(person); }else if(eleName.equals("persons")){ return personList; } } break; } //获取下一个事件 eventType=read.next(); } Log.i("err","循环结束"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("err",e.toString()); } return null; } }