«

实现XMl文件的序列化与解析

时间:2024-3-2 17:07     作者:韩俊     分类: Html+Css


<persons>
    <person id="1">
        <name>wang1</name>
        <age>10></age>
    </person>

    <person id="2">
        <name>wang2</name>
        <age>11></age>
    </person>

    <person id="3">
        <name>wang3</name>
        <age>12></age>
    </person>

    <person id="4">
        <name>wang4</name>
        <age>13></age>
    </person>
</persons>



将上面的xml文件队形实例化与解析。


直接写代码:


public class TestCase extends AndroidTestCase {
    public void test() {
//      writeToLocal();
        List<Person> persons = parserXmlFromLocal();

        for (Person person : persons) {
            Log.i("testcase", person.toString());
        }
    }

    private void writeToLocal() {
        List<Person> personlist = getPersonList();

        XmlSerializer serializer = Xml.newSerializer();

        try {

            File path = new File(Environment.getExternalStorageDirectory(),
                    "persons.xml");
            FileOutputStream fos = new FileOutputStream(path);

            serializer.setOutput(fos, "utf-8");
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "persons");// <persons>

            for (Person person : personlist) {
                // 开始写人
                serializer.startTag(null, "person");
                serializer
                        .attribute(null, "id", String.valueOf(person.getId()));
                // 写名字
                serializer.startTag(null, "name");
                serializer.text(person.getName());
                serializer.endTag(null, "name");

                // 写年龄
                serializer.startTag(null, "age");
                serializer.text(person.getAge() + "");
                serializer.endTag(null, "age");

                serializer.endTag(null, "person");
            }
            serializer.endTag(null, "persons");
            serializer.endDocument();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private List<Person> getPersonList() {
        List<Person> persons = new ArrayList<Person>();
        for (int i = 0; i < 20; i++) {
            persons.add(new Person(i, "wang" + i, 10 + i));
        }
        return persons;
    }

    private List<Person> parserXmlFromLocal (){

        try {
            File path = new File(Environment.getExternalStorageDirectory(),"persons.xml");
            FileInputStream fis = new FileInputStream(path);

            XmlPullParser parser = Xml.newPullParser();

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

            int eventType = parser.getEventType();

            List<Person> personlist = null;
            Person person = null;
            String id;

            while (eventType != XmlPullParser.END_DOCUMENT){
                String tagName = parser.getName();

                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if("persons".equals(tagName)){
                        personlist = new ArrayList<Person>();

                    }else if ("person".equals(tagName)){
                        person = new Person();
                        id = parser.getAttributeValue(null,"id");
                        person.setId(Integer.valueOf(id));
                    }else if ("name".equals(tagName)){
                        person.setName(parser.nextText());
                    }else if ("age".equals(tagName)){
                        person.setAge(Integer.valueOf(parser.nextText()));
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("person".equals(tagName)){
                        personlist.add(person);
                    }
                    break;
                default:
                    break;
                }
                eventType = parser.next();
            }
            return personlist;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}


标签: javascript html css

热门推荐