Android中生成xml文件小demo
来源:互联网 发布:nba常规赛数据 编辑:程序博客网 时间:2024/06/05 12:39
第一步;
在eclipse中创建一个Android工程,先在布局文件中创建一个简单的类似学生登录系统的界面用来后面生成xml的实例,代码如下;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/studentname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入学生的姓名" /> <EditText android:id="@+id/studentnumber" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入学生的学号" /> <RadioGroup android:orientation="horizontal" android:id="@+id/radiogb" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/male" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/female" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="女" /> </RadioGroup> <Button android:onClick="save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存" /></LinearLayout>
预览的效果;
第二步;
在MainActivity中实现功能部分,代码如下;
package com.zdsoft.example;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import org.xmlpull.v1.XmlSerializer;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.util.Xml;import android.view.View;import android.widget.EditText;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity { private EditText mstudentname,mstudentnumber; private RadioGroup mrgp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mstudentname=(EditText) findViewById(R.id.studentname); mstudentnumber=(EditText) findViewById(R.id.studentnumber); mrgp=(RadioGroup) findViewById(R.id.radiogb); } public void save(View v){ //将得到的数据转换为String类型的字符串 String studentname=mstudentname.getText().toString().trim(); String studentnumber=mstudentnumber.getText().toString().trim(); //判断输入的学生姓名和学号不能为空,如果为空则提示,并且return返回 if(TextUtils.isEmpty(studentname)||TextUtils.isEmpty(studentnumber)){ Toast.makeText(this, "输入的学生的姓名和学号不能为空", 0).show(); return; } //获得学生头的性别 int id=mrgp.getCheckedRadioButtonId(); //定义学生的初始化性别为男 String sex="男"; if(id==R.id.male){ sex="男"; }else if(id==R.id.female){ sex="女"; } /*走到 这里, 学生的 信息 都 有了 .将学生的信息保存起来 * <?xml version="1.0" encoding="utf-8"?> <student> <name>张三</name> <number>123456</number> <sex>男</sex> </student> * */ try { File file = new File(getFilesDir(), studentname+".xml"); OutputStream out = new FileOutputStream(file); // 专门生成xml 文件的 序列化器 XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "UTF-8"); // <?xml version="1.0" encoding="utf-8" standalone?> serializer.startDocument("UTF-8", true); serializer.startTag(null, "student"); //设置 文本信息 -- 学生的姓名 serializer.startTag(null, "name"); serializer.text(studentname); serializer.endTag(null, "name"); serializer.startTag(null, "number"); serializer.text(studentnumber); serializer.endTag(null, "number"); serializer.startTag(null, "sex"); serializer.text(sex); serializer.endTag(null, "sex"); serializer.endTag(null, "student"); serializer.endDocument(); out.close(); //弹出保存成功的提示信息 Toast.makeText(this, "保存成功", 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "保存失败", 0).show(); } }}
最后我们可以在对应的目录下,找到所生成的xml文件。
0 0
- Android中生成xml文件小demo
- Android中生成xml文件
- Android 中xml文件的生成
- Android 生成 xml文件
- Android生成XML文件
- asp.net mvc中应用缓存依赖文件(xml)的一个小demo
- 关于DOM4J 解析和生成XML小Demo
- 在android中使用Pull解析器生成xml文件
- android中xml文件的生成与读取(Pull)
- Android笔记 解析xml文件demo
- 黑马Android:生成xml文件
- Android XmlSerializer生成XML文件
- Android中使用Pull解析器生成XML文件和读取xml文件
- 用jdom来解析xml文件小Demo
- Android Xml文件生成,Xml数据格式写入
- Android开发中禁止Eclipse中xml文件Run as的XSL Transformation生成out.xml
- android中XML解析生成
- android中 XML的生成
- linux内核分析笔记----上半部与下半部
- [UnityUI]UGUI背包
- Altium Designer 16 (AD16) 破解版下载 百度网盘分享 16.1.9 (Build 221) 更新
- java和c之间进行数据传递
- myBatis配置
- Android中生成xml文件小demo
- IIS6⇒IIS7网站注意点
- leetcode——Jump Game II
- hadoop2.x常用端口及定义方法
- STL源码剖析——stl_numeric.h
- 论飞机游戏在C语言程序的基本语句能完成功能的体会
- keil5
- 学生成绩管理系统及文件结构分析
- Mac安装cocoapods和使用