简介

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. CGI、FastCGI和PHP-FPM关系图解

    CGI.FastCGI和PHP-FPM关系图解   webapp即是php解析器等 当Web Server收到 index.php 这个请求后,会启动对应的 CGI 程序,这里就是PHP的解析器.接下 ...

  2. CUDA2.1-原理之索引与warp

    本小节来自<大规模并行处理器编程实战>第四节,该书是很好的从内部原理结构上来讲述了CUDA的,对于理解CUDA很有帮助,借以博客的形式去繁取间,肯定会加入自己个人理解,所以有错误之处还望指 ...

  3. SQL 批量修改表结构

    项目中发现一批语言表的某个字段设的值太小了需要增大,因为涉及到很多张表,所以采用游标一张张的处理. 代码很简单 ) ) DECLARE LangTable CURSOR FOR SELECT name ...

  4. 大新闻!HoloLens即将入华商用

    昨天微软搞了大新闻,Terry和Alexi到了深圳,在WinHEC大会上宣布了2017上半年HoloLens正式入华商用. 关于HoloLens的技术原理和细节官方文档和报道已经披露很多了,他是一款真 ...

  5. sdk墙内更新方法

    因为GFW有“保护”,我们能“安全”的遨游在中华互联局域网内.如何快速地更新sdk,一直是Android开发者的心病.网上流传着五花八门的方法,在这我记录一些我用过的切实可行的方法供给有需要的人.同时 ...

  6. 记一次在Linux上面启动部署在tomcat服务器的程序

    前提:Linux系统已安装好jre环境 1.文件结构: 文件说明: 部署文件包含以下文件:1.apache-tomcat-7  程序运行的应用服务器tomcat包含: war包:apache-tomc ...

  7. chgrp 简明笔记

    改变与文件相关联的组 chgrp [options] group file-list 参数 group 为新组的名称或者数值ID,file-list 为要改变其相关联组的文件路径名列表 选项 -c   ...

  8. parse date receiving from mvc jsonresult

    if we received data like this: ,"Date":"\/Date(1410969600000)\/", we can parse i ...

  9. Bete冲刺第六阶段

    Bete冲刺第六阶段 github:https://github.com/RadioGroup/JourneyHelper 今日工作: web: 陈灿:新增了用户信息更新接口,优化了部分接口逻辑,更新 ...

  10. Bete冲刺第七阶段

    Bete冲刺第七阶段 今日工作: web: 新增通知处理接口 ios: 重写登录逻辑,添加创建行程填写.注册 POP界面 目前所遇问题: web: web目前进展顺利,暂时还没有遇到编码的问题. iO ...