首页 > 其他 > 详细

XML写入

时间:2015-10-23 18:51:23      阅读:109      评论:0      收藏:0      [点我收藏+]
    private void createXml() throws IOException {
        ArrayList<People> arrayList = new ArrayList<People>();
        for (int i = 0; i < 10; i++) {
            People p = new People("jim" + i, i);
            arrayList.add(p);
        }
        XmlSerializer xml = Xml.newSerializer();
        OutputStream os = openFileOutput("peoples.xml", Context.MODE_PRIVATE);
        xml.setOutput(os, "utf-8");
        xml.startDocument("utf-8", true);
        xml.startTag(null, "peoples");
        for (People people : arrayList) {
            xml.startTag(null, "people");
            xml.startTag(null, "name");
            xml.attribute(null, "id", "1000");
            xml.text(people.getName());
            xml.endTag(null, "name");

            xml.startTag(null, "age");
            xml.text(String.valueOf(people.getAge()));
            xml.endTag(null, "age");
            xml.endTag(null, "people");
        }
        xml.endTag(null, "peoples");
        xml.endDocument();
    }

 

    class People {
        String name;
        int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public People(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public People() {
        }
    }

 

XML写入

原文:http://www.cnblogs.com/linson0116/p/4905129.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!