xml的系列化和解析

public class xmlwork {

    //这是我们的编码格式

    private static String codeing="utf-8";

   //这是 我们的文件路径

    private static String pathS="data/data/com.kang.xml/kang.xml";

    //写入的方法
    public static boolean writexml() {

       try {

     //

        List<person> pList=getxml();

        // 生成工具的对象

            XmlSerializer serializer=Xml.newSerializer();

           //我们去生成一个文件
            File path=new File(pathS);
          // 得到一个输入流

            FileOutputStream outputStream=new FileOutputStream(path);

         / /  去设置这个 生成对象的输入流 以及编码

            serializer.setOutput(outputStream,codeing);

       // 这是我们的头标志    就是这个<?xml version='1.0' encoding='utf-8' standalone='yes' ?>

            serializer.startDocument(codeing, true);

           //这个是 开始的标志

        // 比如  <persons>

              //          <person id=1>

                //                   <name>"zhangsan"</name>

                  //                    <age>1</age>

                    //      </person>             

        // </persons>
            serializer.startTag(null, "persons");
            for (person p:pList) {
                serializer.startTag(null, "person");
                serializer.attribute(null, "id", String.valueOf(p.getId()));
                serializer.startTag(null, "name");
                serializer.text(p.getName());
                serializer.endTag(null, "name");
                serializer.startTag(null, "age");
                serializer.text(String.valueOf(p.getAge()));
                serializer.endTag(null, "age");
                serializer.endTag(null, "person");
                
            }
            
            serializer.endTag(null, "persons");
            serializer.endDocument();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
    
}
    private static List<person> getxml(){
        List<person> ppList=new ArrayList<person>();
        for (int i = 0; i < 30; i++) {
            ppList.add(new person("zheng"+i,i,18+i));
        }
        return ppList;
        

    }

    // 这是 xml文件的解析  这个就不用多说啦
    private static List<person> readxml() {
        File path=new File(pathS);
        try {
            FileInputStream inputStream=new FileInputStream(path);
            XmlPullParser parser=Xml.newPullParser();
            parser.setInput(inputStream, codeing);
            int emptype=parser.getEventType();
            if (emptype!=XmlPullParser.END_DOCUMENT) {
                List<person> pp=null;
                person ps=null;
                String tagname=parser.getName();
                switch (emptype) {
                case XmlPullParser.START_TAG:
                    if ("persons".equals(tagname)) {
                         pp=new ArrayList<person>();
                    }else if("person".equals(tagname)) {
                         ps=new person();
                        String id= parser.getAttributeValue(null,"id");
                        ps.setId(Integer.valueOf(id));
                        
                    }else if("name".equals(tagname)) {
                    
                        ps.setName(parser.nextText());
                    }else if ("age".equals(tagname)) {
                        
                        ps.setId(Integer.valueOf(parser.next()));
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("person".equals(tagname)) {

                        pp.add(ps);
                    
                    }    
                    break;
                default:
                    break;
                }
                emptype=parser.next();
                return pp;
            }
         
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return null;
        
    }

}

你可能感兴趣的:(xml,安卓,解析,序列化)