Pull生成XML文件

前端之家收集整理的这篇文章主要介绍了Pull生成XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

点击button按钮生成xml文件

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文件

猜你在找的XML相关文章