您的位置:首页 > 其它

XML写入

2015-10-23 17:13 246 查看
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() {
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: