1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| XmlSerializer serializer = Xml.newSerializer(); try { File file = new File(Environment.getExternalStorageDirectory(), "person.xml"); FileOutputStream fos = new FileOutputStream(file);
serializer.setOutput(fos, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "persons"); for(Person person : persons) { 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(String.valueOf(person.getAge())); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); } catch(Exception e) { e.printStackTrace(); }
|