xml也是存储Android中存储信息的一种方法,以下介绍一个动态存储信息到xml并读取xml的实例。
首先将信息存储到 student.xml中(默认存储到assets文件夹中)
private void xmlSave(Student student) {
students.add(student);
Write("students.xml", writeXml(students));
send(student.getName());//在通知栏发送通知
}
public void Write(String path, String str) {
try {
OutputStream os = openFileOutput(path, MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(str);
osw.close();
os.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}<pre class="java" name="code">private String writeXml(List<Student> students) {//使用Pull解析器生成XML文件
XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
serializer.setOutput(writer);
serializer.startDocument("UTF - 8", true);
serializer.startTag("", "students");
for (Student student : students) {
serializer.startTag("", "student");
serializer.attribute("", "id", String.valueOf(student.getID()));
serializer.attribute("", "name", student.getName());
serializer.attribute("", "math", String.valueOf(student.getMath()));
serializer.attribute("", "geometry", String.valueOf(student.getGeometry()));
serializer.attribute("", "cProgram", String.valueOf(student.getCprogram()));
serializer.endTag("", "student");
}
serializer.endTag("", "students");
serializer.endDocument();
return writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
在DDMS中(data/data/包名/files/students.xml)提取到的新目录文件内容格式如下
使用DOM解析器对students.xml进行解析:
private void btnXML() {
Bundle data = new Bundle();
data.putSerializable("data", toList(ReadXmlUser()));
Intent intent = new Intent(InputActivity.this, PrintActivity.class);
intent.putExtras(data);
startActivity(intent);
}
private List<Student> ReadXmlUser() {
DocumentBuilderFactory documentBuilderFactory;
DocumentBuilder documentBuilder;
Document document;
documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
//xml文件放到assets目录下
document = documentBuilder.parse(this.openFileInput("students.xml"));
Element root = document.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("student");
for (int i = 0; i < nodeList.getLength(); i++) {
Student student = new Student();
//得到一个student节点
Element studentNode = (Element) nodeList.item(i);
//获取student节点的属性值
student.setID(new Integer(studentNode.getAttribute("id")));
student.setName(new String(studentNode.getAttribute("name")));
student.setMath(new Float(studentNode.getAttribute("math")));
student.setGeometry(new Float(studentNode.getAttribute("geometry")));
student.setCprogram(new Float(studentNode.getAttribute("cProgram")));
students.add(student);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
Student类如下:
package com.zhang.android;
public class Student{ //学生信息类
private int ID; //学号
private String name ; //学生姓名
private float math; //工数成绩
private float geometry; //代几成绩
private float cprogram; //C语言成绩
public Student(){ //无参构造方法
}
public Student(int iD,String name,float math,float geometry,float cprogram){ //有参构造方法
this.setID(iD);
this.setName(name);
this.setMath(math);
this.setGeometry(geometry);
this.setCprogram(cprogram);
}
//运用setter和getter进行封装
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 float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getGeometry() {
return geometry;
}
public void setGeometry(float geometry) {
this.geometry = geometry;
}
public float getCprogram() {
return cprogram;
}
public void setCprogram(float cprogram) {
this.cprogram = cprogram;
}
}