Android——PULL解析XML

时间:2023-03-09 20:10:49
Android——PULL解析XML

简介

Android中常常使用XML文件保存用户的APP设置信息。因此需要对XML文件的相关操作进行了解。本文将以《学生信息管理系统》为案例背景进行介绍相关的XML解析的介绍,以及其他相关知识的阐述。

需求:

  1. 在一个Activity中可以动态添加一个学生信息并保存到XML文件。
  2. 同时,还可以查看当前的所有学生信息。

相关技术:

  1. 线性布局
  2. 设置onClick()事件响应函数
  3. 添加一个TextView以显示添加的学生信息
  4. 清空所有的子元素(列表中所有的学生信息的TextView)
  5. PULL创建XML文件
  6. PULL解析XML文件

界面UI:

Android——PULL解析XML

实现:

  • 界面布局:

界面的布局,首先需要考虑的是:上图中间显示学生信息的栏位,如果学生信息条数过多,需要考虑是否会出现重合。

解决方法:

整个layout采用LinearLayout。

为各个模块设置权重(weight),其中,除显示学生信息的所有部分均不设置权重(即默认为0)并且学生信息显示部分weight=”1。这样既可达到将剩余的部分全部分配给这个部分,

代码:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <!-- 第一大板块 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学生信息管理系统"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:textColor="#0000FF"
/> <!-- 第二大板块 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!-- 姓名的输入框 -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
/>
<EditText
android:id="@+id/et_studentname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout> <!-- 性别的输入框 -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
/>
<EditText
android:id="@+id/et_studentgender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout> <!-- 年龄的输入框 -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄"
/>
<EditText
android:id="@+id/et_studentage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout> <!-- 权重不写的话,默认就是0 -->
<Button
android:id="@+id/bt_addstudent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加学生"
android:layout_gravity="bottom"
android:onClick="addStudent"
/>
</LinearLayout>
<!-- 第三大板块 -->
<ScrollView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
>
<LinearLayout
android:id="@+id/part3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- 用户每点击一次添加按钮,就在代码中new一个textView显示学生信息 -->
</LinearLayout>
</ScrollView>
<!-- 第四大板块 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/bt_savetoxml"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存信息"
android:onClick="savetoxml"
/>
<Button
android:id="@+id/bt_getfromxml"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="查看信息"
android:onClick="getfromxml"
/>
</LinearLayout> </LinearLayout>
  • onClick事件

在设置响应事件之前,首先需要考虑以下内容:

当点击“添加学生”按钮时,应该在下面的显示需要添加的学生信息;

当点击“保存信息”按钮时,需要将上面需要添加学生信息保存到XML文件中。然而,如何获取该页面中的学生信息呢?同时,在查看学生信息时,如果从XML文件中读取出学生信息,添加textview到这里,会不会重复呢?

解决方案:

1-在Activity的onCreate方法中初始化一个List<Student>,用于保存学生信息。
studentList=new ArrayList<Student>();
2-当添加学生信息时,将初始化一个学生对象,并添加到该List中。
 public void addStudent(View v){
String name = studentname.getText().toString();
String gender = studentgender.getText().toString();
String age = studentage.getText().toString(); TextView studentinfo = new TextView(this);
studentinfo.setText(name+" "+gender+" "+age);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.part3);
linearLayout.addView(studentinfo);
Student student=new Student(name, gender, age);
studentList.add(student);
}

上面的代码中,TextView studentinfo = new TextView(this);用于在显示学生信息栏创建一个TextView。并使用linearLayout.addView(studentinfo)将创建的TextView添加到显示栏。将该学生添加到List中——studentList.add(student)

3-在保存信息时,遍历整个List,将学生信息保存到XML文件。
public void savetoxml(View v){
XmlSerializer xs = Xml.newSerializer();
FileOutputStream fos=null;
fos=new FileOutputStream(new File(getFilesDir(), "students.xml"));
xs.setOutput(fos, "utf-8");
xs.startDocument("utf-8", true);
xs.startTag(null, "students");
for(Student student: studentList){
xs.startTag(null, "student");
xs.startTag(null, "name");
xs.text(student.getName());
xs.endTag(null, "name");
//---------------------------------
xs.startTag(null, "gender");
xs.text(student.getGender());
xs.endTag(null, "gender");
//---------------------------------
xs.startTag(null, "age");
xs.text(student.getAge());
xs.endTag(null, "age");
xs.endTag(null, "student");
}
xs.endTag(null, "students");
xs.endDocument();
}
4-在查看学生信息时,首先清空之前页面中的学生信息,将Listclear,并显示已保存的学生信息保存到List中,同时将学生信息添加到显示栏
public void getfromxml(View v){
//1-如果当前页面中有学生信息列出,首先删除列表信息
studentList.clear();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.part3);
linearLayout.removeAllViews();
//2-从XML文件中读取学生信息,新建textview,并将学生信息添加到其中
parse();
}
public void parse(){
XmlPullParser parser= Xml.newPullParser();
Student student=null;
TextView studentinfo =null;
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.part3);
InputStream in=new FileInputStream(new File(getFilesDir(), "students.xml"));
parser.setInput(in, "UTF-8");
int evenType=parser.getEventType();
while(evenType!=XmlPullParser.END_DOCUMENT){
if(evenType==XmlPullParser.START_TAG){
if("student".equals(parser.getName())){
student=new Student();
studentinfo = new TextView(this);
}
if("name".equals(parser.getName())){
student.setName(parser.nextText());
}
if("gender".equals(parser.getName())){
student.setGender(parser.nextText());
}
if("age".equals(parser.getName())){
student.setAge(parser.nextText());
}
}
if(evenType==XmlPullParser.END_TAG&& "student".equals(parser.getName()))
{
Log.i("student", student.toString());
studentList.add(student);
linearLayout.addView(studentinfo);
studentinfo.setText(student.getName()+"\t"+student.getGender()+"\t"+student.getAge());
student=null;
}
evenType=parser.next();
}
}