Pull生成XML文件

点击button按钮生成xml文件
Pull生成XML文件_第1张图片
Pull生成XML文件_第2张图片

MainActivity.java

package com.example.pullxmls;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;

public class MainActivity extends Activity {

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

    public void clickView(View v) throws IllegalArgumentException,
            IllegalStateException, IOException {

        // 生成一个xml文件
        // 1.得到xmlserializer的对象
        XmlSerializer xmlSerializer = Xml.newSerializer();

        // java流处理
        //StringWriter stringWriter = new StringWriter();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                this.openFileOutput("csdn.xml", Context.MODE_PRIVATE), "UTF-8"));

        // 2.输出
        xmlSerializer.setOutput(writer);
        // 开始文档
        xmlSerializer.startDocument("UTF-8", true);

        // 开始标签
        xmlSerializer.startTag(null, "orders");

        xmlSerializer.startTag(null, "order");
        xmlSerializer.startTag(null, "iprice");
        xmlSerializer.text("36");
        // 结束标签
        xmlSerializer.endTag(null, "iprice");

        // 开始标签
        xmlSerializer.startTag(null, "status");
        xmlSerializer.text("11");
        // 结束标签
        xmlSerializer.endTag(null, "status");

        // 结束标签
        xmlSerializer.endTag(null, "order");
        xmlSerializer.endTag(null, "orders");

        // 结束文档
        xmlSerializer.endDocument();

        writer.close();

    }
}

运行该项目将会生成csdn.xml文件
这里写图片描述
Pull生成XML文件_第3张图片

你可能感兴趣的:(pull)