pullxml操作xml

package com.kk.android.test;



import java.io.File;

import java.io.FileWriter;

import java.io.InputStream;

import java.io.Writer;

import java.util.ArrayList;

import java.util.List;



import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlSerializer;



import android.test.AndroidTestCase;

import android.util.Log;

import android.util.Xml;



/**

 * 使用xmlpull库操作xml

 * @author MyPC

 *

 */

public class PullParserPersonTest extends AndroidTestCase {

    private static final String TAG="PullParserPersonTest";

    

    /**

     * 解析xml

     * @throws Exception

     */

    public void testRead() throws Exception {

        InputStream personFile=getClass().getClassLoader().getResourceAsStream("person.xml");

        List<Person> personList = null;

        Person person=null;

        XmlPullParser parser=Xml.newPullParser();

        parser.setInput(personFile, "utf-8");

        int eventType=parser.getEventType(); //产生第一个事件

        

        while(eventType!=XmlPullParser.END_DOCUMENT){

            switch (eventType){

            

            case XmlPullParser.START_DOCUMENT:

                personList=new ArrayList<Person>();

                break;

            case XmlPullParser.START_TAG:

                String tagName=parser.getName();//获取解析器当前指向的元素的名称

                if("person".equals(tagName)){

                    person=new Person();

                    person.setId(Integer.valueOf(parser.getAttributeValue(0)));

                }

                if(person!=null){

                    if(tagName.equals("name")){

                        person.setName(parser.nextText());

                    }

                    if(tagName.equals("age")){

                        person.setAge(Integer.valueOf(parser.nextText()));

                    }

                }

                break;

            case XmlPullParser.END_TAG:

                if("person".equals(parser.getName())){

                    personList.add(person);

                    person=null;

                }

                break;

            }

            eventType=parser.next();

        }

        

        for(Person p:personList){

            Log.i(TAG, p.toString());

        }

        

    }

    

    /**

     * 生成xml

     * @throws Exception

     */

    public void write()throws Exception{

        File file=new File(getContext().getFilesDir(),"message.xml");

        if(!file.exists())file.createNewFile();

        Writer writer=new FileWriter(file);

        XmlSerializer serializer=Xml.newSerializer();

        serializer.setOutput(writer);

        serializer.startDocument(null, true);

        serializer.startTag(null, "message");

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

            

            serializer.startTag(null,"name");

            serializer.attribute(null, "id", i+"");

            serializer.text("name"+i);

            serializer.endTag(null,"name");

            

            serializer.startTag(null, "age");

            serializer.text(i+"");

            serializer.endTag(null, "age");

        }

        serializer.endTag(null, "message");

        serializer.endDocument();

    }

    

    

    

    private class Person {

        private Integer id;

        private Integer age;

        private String name;



        public Integer getId() {

            return id;

        }



        public void setId(Integer id) {

            this.id = id;

        }



        public Integer getAge() {

            return age;

        }



        public void setAge(Integer age) {

            this.age = age;

        }



        public String getName() {

            return name;

        }



        public void setName(String name) {

            this.name = name;

        }



        @Override

        public String toString() {

            return "id:"+id+" name:"+name+" age:"+age;

        }

        

        

    }    



}

你可能感兴趣的:(pull)