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
原创粉丝点击