简介

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

需求:

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

相关技术:

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

界面UI:

实现:

  • 界面布局:

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

解决方法:

整个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();
}
}

Android——PULL解析XML的更多相关文章

  1. Android pull解析xml文件

    本文介绍android中使用pull来解析xml文件 先自己写一个xml文件,存一些天气信息 <?xml version="1.0" encoding="UTF-8 ...

  2. Android Pull解析XML

    在上文中介绍了使用sax方式解析xml,这里介绍下在Android中极力推荐的xmlpull方式解析xml.xmlpull不仅仅可以使用在Android上,同样也适用于javase,但在javase环 ...

  3. Android之Pull解析XML

    一.Pull解析方法介绍 除了可以使用SAX和DOM解析XML文件,也可以使用Android内置的Pull解析器解析XML文件.Pull解析器的运行方式与SAX解析器相似.它也是事件触发的.Pull解 ...

  4. Android系列--DOM、SAX、Pull解析XML

    您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...

  5. Android SAX、DOM、Pull解析xml文件剖析与案例讲解

    XML介绍 XML(Extensible Markup Language) 即可扩展标记语言,与HTML一样,都是SGML(Standard Generalized Markup Language,标 ...

  6. Android中pull解析XML文件的简单使用

    首先,android中解析XML文件有三种方式,dom,sax,pull 这里先讲pull,稍候会说SAX和DOM pull是一种事件驱动的xml解析方式,不需要解析整个文档,返回的值是数值型,是推荐 ...

  7. Android -- 创建XML文件对象及其序列化, pull解析XML文件

    1. 创建XML文件对象及其序列化 示例代码:(模拟以xml格式备份短信到SD卡) SmsInfo.java, bean对象 /** * 短信的业务bean * @author Administrat ...

  8. PULL解析XML的运行机制详解

    PULL解析简单易上手,基本上看一遍,基本上就会解析啦,但总是感觉对PULL解析的运行机制不是很了解,就总结了以下事件驱动到底是怎么执行的.. PULL: Android内置了PULL解析器.PULL ...

  9. [置顶] Android学习系列-Android中解析xml(7)

    Android学习系列-Android中解析xml(7) 一,概述 1,一个是DOM,它是生成一个树,有了树以后你搜索.查找都可以做. 2,另一种是基于流的,就是解析器从头到尾解析一遍xml文件.   ...

随机推荐

  1. postgresql 函数返回结果集(zz)

    pgsql function 系列之一:返回结果集--------------------------------------------------------------------------- ...

  2. WPFProgressBarAndSlider随位置显示Value

    先来一发图,有图有真相. 核心代码如下 ProgressBar添加一个textBlock 绑定Value并且位置绑定进度条的实际宽度 <Canvas Height="10" ...

  3. mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理

    1.mvc5+ef6+Bootstrap 项目心得--创立之初 2.mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理 3.mvc5+ef6+Bootstrap 项目心得--WebG ...

  4. java并发:线程池、饱和策略、定制、扩展

    一.序言 当我们需要使用线程的时候,我们可以新建一个线程,然后显式调用线程的start()方法,这样实现起来非常简便,但在某些场景下存在缺陷:如果需要同时执行多个任务(即并发的线程数量很多),频繁地创 ...

  5. HTML5之CSS3 3D transform 剖析式学习之一

    最近坐地铁发现“亚洲动物基金”在地铁上做了很多公益广告,比较吸引人的是一个月熊的广告.做的很可爱.回去就搜了一下,发现这个网站是HTML5做的,非常炫. 所以想学习一下,方法就是传统的学习办法,模仿. ...

  6. Android开发之ViewPager做新手引导界面

    先看一下我们要开发的界面(三张图片,滑到最后一个会出现开始体验的Button,下面的小红点会跟着一起滑动): 首先看一下布局文件: <?xml version="1.0" e ...

  7. 给菜单加个优雅的unselect事件

    先上图,说场景 假设默认选中的是item1,我现在选中item3了,有时候需要对item1做一些别的处理.常见的做法是,切换选中状态前找到当前选中(item1),或者每次选中后选中的项记录在中间变量. ...

  8. Android M 控件:Snackbar、Toolbar、TabLayout、NavigationView

    Snackbar Snackbar提供了一个介于Toast和AlertDialog之间轻量级控件,它可以很方便的提供消息的提示和动作反馈.Snackbar的使用与Toast的使用基本相同: Snack ...

  9. java-二维码编写zxing

    zxing 这个是google的 下载地址 http://code.google.com/p/zxing/downloads/list 二维码源码案例 package com.utils; impor ...

  10. mysql查看数据库

    进入MySQL Command line client下查看当前使用的数据库: mysql>select database(); mysql>status; mysql>show t ...