使XML序列化器生成XML文件

核心代码

package com.example.createxml;

import android.app.Activity;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Xmlserializer extends Activity {

    private List<Message> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xmlserializer);


    }

    public void click(View v){
        list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Message msg = new Message("苗戴" + i, System.currentTimeMillis() + "", 1 + "", "我是body" + i);
            list.add(msg);
        }


        //1.得到序列化对象
        XmlSerializer xs = Xml.newSerializer();
        //存放到SD卡中注意添加可读权限  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        //sdcard/message.xml
        File file = new File(Environment.getExternalStorageDirectory(), "message.xml");

        try {
            FileOutputStream fos = new FileOutputStream(file);
            //2.对序列化对象进行初始化
            xs.setOutput(fos, "utf-8");
            //3.生成文件,生成头结点
            xs.startDocument("utf-8", true);
            xs.startTag(null, "messages");
            for(Message msg : list){
                xs.startTag(null,"message");

                xs.startTag(null,"address");
                xs.text(msg.getAddress());
                xs.endTag(null, "address");

                xs.startTag(null,"date");
                xs.text(msg.getDate());
                xs.endTag(null, "date");

                xs.startTag(null,"type");
                xs.text(msg.getType());
                xs.endTag(null,"type");

                xs.startTag(null,"body");
                xs.text(msg.getBody());
                xs.endTag(null,"body");

                xs.endTag(null,"message");
            }

            xs.endTag(null,"messages");
            xs.endDocument();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}

布局文件

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.createxml.Xmlserializer">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/createxml"
        android:onClick="click"/>

</RelativeLayout>

Message类

package com.example.createxml;

/**
 * Created by joy on 2015/12/20.
 */
public class Message {



    public Message(String address, String date, String type, String body) {
        this.address = address;
        this.date = date;
        this.type = type;
        this.body = body;
    }

    private String address;
    private String date;
    private String type;
    private String body;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "Message{" +
                "address='" + address + '\'' +
                ", date='" + date + '\'' +
                ", type='" + type + '\'' +
                ", body='" + body + '\'' +
                '}';
    }
}

生成的XML文件

<messages>
<message>
<address>苗戴0</address>
<date>1451094799432</date>
<type>1</type>
<body>我是body0</body>
</message>
<message>
<address>苗戴1</address>
<date>1451094799433</date>
<type>1</type>
<body>我是body1</body>
</message>
<message>
<address>苗戴2</address>
<date>1451094799433</date>
<type>1</type>
<body>我是body2</body>
</message>
<message>
<address>苗戴3</address>
<date>1451094799433</date>
<type>1</type>
<body>我是body3</body>
</message>
<message>
<address>苗戴4</address>
<date>1451094799433</date>
<type>1</type>
<body>我是body4</body>
</message>
<message>
<address>苗戴5</address>
<date>1451094799433</date>
<type>1</type>
<body>我是body5</body>
</message>
<message>
<address>苗戴6</address>
<date>1451094799433</date>
<type>1</type>
<body>我是body6</body>
</message>
<message>
<address>苗戴7</address>
<date>1451094799433</date>
<type>1</type>
<body>我是body7</body>
</message>
<message>
<address>苗戴8</address>
<date>1451094799434</date>
<type>1</type>
<body>我是body8</body>
</message>
<message>
<address>苗戴9</address>
<date>1451094799434</date>
<type>1</type>
<body>我是body9</body>
</message>
</messages>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值