public class MainActivity extends Activity { private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取资源文件的输入流对象
//InputStream inputstram=this.getClass().getClassLoader().getResourceAsStream("person.xml");
try
{
persons=new ArrayList<Person>();
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"a.xml");
Person p=new Person(25, "ggg", "21", "men", "1234562");
persons.add(p);
FileOutputStream stram=new FileOutputStream(file);
savexml(persons,stram);
System.out.println("保存成功");
}
catch (Exception e) {
e.printStackTrace();
}
} public void savexml(List<Person> person,OutputStream out)
{
/**
* 类似于这样格式的文件
* <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<persons>
<person id="25">
<name>ggg</name>
<sex>men</sex>
<age>21</age>
<phone>1234562</phone>
</person>
</persons>
*/
try
{
//xml解析器
XmlSerializer xml=Xml.newSerializer();
//设置xml文件的输出方向
xml.setOutput(out, "UTF-8");
//设置xml的开始文档内容及编码格式
xml.startDocument("UTF-8", true);
//设置xml的开始节点
xml.startTag(null, "persons"); for(Person p:person)
{
xml.startTag(null, "person"); String id=String.valueOf(p.getId());
xml.attribute(null, "id",id);
//设置开始name节点
xml.startTag(null, "name");
//设置name的值
xml.text(p.getName());
xml.endTag(null, "name");
//设置name的结束节点
xml.startTag(null, "sex");
xml.text(p.getSex());
xml.endTag(null, "sex"); xml.startTag(null, "age");
xml.text(p.getAge());
xml.endTag(null, "age"); xml.startTag(null, "phone");
xml.text(p.getPhone());
xml.endTag(null, "phone");
//结束节点
xml.endTag(null, "person");
}
//xml的结束节点
xml.endTag(null, "persons");
//结束文档
xml.endDocument();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
out.flush();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
person类
public class Person
{
private int id;
private String name;
private String age;
private String sex;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Person(int id, String name, String age, String sex, String phone) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Person(){}
}
保存的内容结果是:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<persons>
<person id="25">
<name>ggg</name>
<sex>men</sex>
<age>21</age>
<phone>1234562</phone>
</person>
</persons>