Android中XML文件的序列化生成与解析

学向勤中得,萤窗万卷书。这篇文章主要讲述Android中XML文件的序列化生成与解析相关的知识,希望能为你提供帮助。
首先,我把Person的实体类

package net.loonggg.test; public class Person { private int id; private String age; private String name; private String sex; private String address; public int getId() { return id; }public void setId(int id) { this.id = id; }public String getAge() { return age; }public void setAge(String age) { this.age = age; }public String getName() { return name; }public void setName(String name) { this.name = name; }public String getSex() { return sex; }public void setSex(String sex) { this.sex = sex; }public String getAddress() { return address; }public void setAddress(String address) { this.address = address; }@Override public String toString() { return " Person [id=" + id + " , age=" + age + " , name=" + name + " , sex=" + sex + " , address=" + address + " ]" ; }}

其次:生成xml文件的方法例如以下:
/** * 生成xml */ private void createXml() { XmlSerializer serializer = Xml.newSerializer(); // xml文件生成器 File file = new File(Environment.getExternalStorageDirectory(), " person.xml" ); FileOutputStream fos = null; try { fos = new FileOutputStream(file); serializer.setOutput(fos, " utf-8" ); // 为xml生成器设置输出流和字符编码 serializer.startDocument(" utf-8" , true); // 開始文档。參数分别为字符编码和是否保持独立 serializer.startTag(null, " persons" ); // 開始标签,參数分别为:命名空间和标签名 for (Person person : list) {serializer.startTag(null, " person" ); serializer.attribute(null, " id" , person.getId() + " " ); serializer.startTag(null, " name" ); // 開始标签 serializer.text(person.getName()); // 文本内容 serializer.endTag(null, " name" ); // 结束标签serializer.startTag(null, " sex" ); serializer.text(person.getSex()); serializer.endTag(null, " sex" ); serializer.startTag(null, " age" ); serializer.text(person.getAge()); serializer.endTag(null, " age" ); serializer.startTag(null, " address" ); serializer.text(person.getAddress()); serializer.endTag(null, " address" ); serializer.endTag(null, " person" ); } serializer.endTag(null, " persons" ); // 结束标签 serializer.endDocument(); // 结束xml文档 Toast.makeText(getApplicationContext(), " 生成成功。" , Toast.LENGTH_SHORT); } catch (Exception e) { Toast.makeText(getApplicationContext(), " 生成失败!
" , Toast.LENGTH_SHORT); e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } }}

然后是解析xml文件的方法:
/** * 解析xml文件 */ private List< Person> pullXml() { try { File file = new File(Environment.getExternalStorageDirectory(), " person.xml" ); FileInputStream fis = new FileInputStream(file); List< Person> persons = null; Person person = null; XmlPullParser parser = Xml.newPullParser(); // 获取xml解析器 parser.setInput(fis, " utf-8" ); // 參数分别为输入流和字符编码 int type = parser.getEventType(); while (type != XmlPullParser.END_DOCUMENT) {// 假设事件不等于文档结束事件就继续循环 switch (type) { case XmlPullParser.START_TAG: if (" persons" .equals(parser.getName())) { persons = new ArrayList< Person> (); } else if (" person" .equals(parser.getName())) { person = new Person(); String id = parser.getAttributeValue(0); person.setId(Integer.parseInt(id)); } else if (" name" .equals(parser.getName())) { person.setName(parser.nextText()); } else if (" sex" .equals(parser.getName())) { person.setSex(parser.nextText()); } else if (" address" .equals(parser.getName())) { person.setAddress(parser.nextText()); } else if (" age" .equals(parser.getName())) { person.setAge(parser.nextText()); } break; case XmlPullParser.END_TAG: if (" person" .equals(parser.getName())) { persons.add(person); person = null; } break; } type = parser.next(); // 继续下一个事件 } return persons; } catch (NumberFormatException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }

再次是MainActivity里调用的部分代码:
【Android中XML文件的序列化生成与解析】
public class MainActivity extends Activity { private Button btn_create = null; private Button btn_pull = null; private TextView tv_show; private List< Person> list = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_create = (Button) findViewById(R.id.createXml); btn_pull = (Button) findViewById(R.id.pullXml); tv_show = (TextView) findViewById(R.id.tvShow); list = new ArrayList< Person> (); for (int i = 0; i < 5; i++) { Person person = new Person(); person.setAge(i + 20 + " " ); person.setId(i); person.setName(" loonggg" + i); person.setSex(" 男" ); person.setAddress(" 中南海" + i + 1 + " 大街" ); list.add(person); } btn_create.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) { createXml(); } }); btn_pull.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) { tv_show.setText(pullXml().toString()); } }); } }













    推荐阅读