android菜鸟学习笔记19----Android数据存储(三)XML文件的解析及序列化

Android内置了PULL解析器的XPP3实现,以及SAX解析器等,可以直接使用PULL或SAX解析XML,直接把JAVA中进行PULL或SAX解析的代码直接拿来用,遗忘的话,可以参考java拾遗1,2,3关于XML的解析:

如有如下XmlUtils类实现PULL方式解析XML到List和序列化List到XML:

Student实体类代码:

 package cn.csc.bean;

 public class Student {

       private String id;

       private String name;

       private String gender;

       private int age;

       public String getId() {

            return id;

       }

       public void setId(String id) {

            this.id = id;

       }

       public String getName() {

            return name;

       }

       public void setName(String name) {

            this.name = name;

       }

       public String getGender() {

            return gender;

       }

       public void setGender(String gender) {

            this.gender = gender;

       }

       public int getAge() {

            return age;

       }

       public void setAge(int age) {

            this.age = age;

       }

       public Student() {

            super();

       }

       public Student(String id, String name, String gender, int age) {

            super();

            this.id = id;

            this.name = name;

            this.gender = gender;

            this.age = age;

       }

       @Override

       public String toString() {

            return "["+id+","+name+","+gender+","+age+"]";

       }

 }

XmlUtils类的代码:

 public class XmlUtils {

       public static void serialize(Context context, String filename, List<Student> list){

            try {

                  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

                  XmlSerializer serializer = factory.newSerializer();

                  serializer.setOutput(context.openFileOutput(filename, Context.MODE_PRIVATE), "utf-8");

                  serializer.startDocument("utf-8", true);

                  serializer.startTag(null, "students");

                  for(Student s:list){

                       serializer.startTag(null, "student");

                       serializer.attribute(null, "id", s.getId());

                       serializer.startTag(null, "name");

                       serializer.text(s.getName());

                       serializer.endTag(null, "name");

                       serializer.startTag(null, "gender");

                       serializer.text(s.getGender());

                       serializer.endTag(null, "gender");

                       serializer.startTag(null, "age");

                       serializer.text(s.getAge()+"");

                       serializer.endTag(null, "age");

                       serializer.endTag(null, "student");

                  }

                  serializer.endTag(null, "students");

                  serializer.endDocument();

            } catch (Exception e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

            }

       }

       public static List<Student> parse(Context context, String filename){

            List<Student> list = new ArrayList<Student>();

            try {

                  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

                  XmlPullParser parser = factory.newPullParser();

                  parser.setInput(context.openFileInput(filename), "utf-8");

                  Student s = null;

                  int eventType = parser.getEventType();

                  while(eventType != XmlPullParser.END_DOCUMENT){

                       if(eventType == XmlPullParser.START_TAG){

                             String tag = parser.getName();

                             if(tag.equals("student")){

                                   s = new Student();

                                   s.setId(parser.getAttributeValue(0));

                             }else if(tag.equals("name")){

                                   s.setName(parser.nextText());

                             }else if(tag.equals("gender")){

                                   s.setGender(parser.nextText());

                             }else if(tag.equals("age")){

                                   s.setAge(Integer.parseInt(parser.nextText()));

                             }

                       }else if(eventType == XmlPullParser.END_TAG && parser.getName().equals("student")){

                             list.add(s);

                       }

                       eventType = parser.next();

                  }

                  for(Student stu : list){

                       Log.i("XML_PARSER",stu.toString());

                  }

            } catch (Exception e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

                  return null;

            }

            return list;

       }

 }

使用Android Juit分别测试这两个方法:

 public class XmlTest extends AndroidTestCase {

       public void testSerializer(){

            List<Student> list = new ArrayList<Student>();

            for(int i=0; i<20; i++){

                  list.add(new Student(i+"","dqrcsc","male",25));

            }

            XmlUtils.serialize(getContext(), "students.xml", list);

       }

       public void testParser(){

            XmlUtils.parse(getContext(), "students.xml");

       }

 }

运行结果:

android菜鸟学习笔记19----Android数据存储(三)XML文件的解析及序列化

android菜鸟学习笔记19----Android数据存储(三)XML文件的解析及序列化

以上代码,除了获取Context上下文对象外,与Java项目中对XML的PULL解析是完全一样的。

实际上,Andoid在android.util包中还提供了一个Xml类,实现了对XML解析、序列化操作的封装,在使用时,可以相对简单一点:

android菜鸟学习笔记19----Android数据存储(三)XML文件的解析及序列化

这五个静态方法分别简化了PULL解析器、PULL序列化器、SAX解析的操作:

之前通过工厂类实例获取PULL序列化器的操作,可以从:

 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

 XmlSerializer serializer = factory.newSerializer();

简化为:

XmlSerializer serializer = Xml.newSerializer();

通过工厂类实例获取PULL解析器的操作,也可以从:

 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

 XmlPullParser parser = factory.newPullParser();

简化为:

XmlPullParser parser = Xml.newPullParser();

同样的,SAX解析也简化为直接调用Xml的parse()静态方法,这里就不说明了。

上一篇:如何快速把 Vue 项目升级到 webpack3


下一篇:Android学习笔记之数据的Sdcard存储方法及操作sdcard的工具类