对象转成xml(并且序列化xml)在android中

前端之家收集整理的这篇文章主要介绍了对象转成xml(并且序列化xml)在android中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 
第一种
在本地生成AppuserManager.xml文件,并且把对象转成了xml格式
package com.example.hasee.myxml;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Xml;
import android.view.View;
import android.widget.TextView;

import org.xmlpull.v1.XmlSerializer;

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

public class MainActivity extends Activity {

    List<AppuserManager> list;
    private TextView tv_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_text = (TextView)findViewById(R.id.tv_text);
        list = new ArrayList<AppuserManager>();
       
            AppuserManager ap = new AppuserManager("appuser","*","ID=1","updatetime desc",0);
            list.add(ap);
    }


    public void click(View v){
        //使用序列化器生成xml文件
        //1、得到xml序列化器对象
        XmlSerializer xs = Xml.newSerializer();
        //2、设置序列化器的输出流
        File file = new File(Environment.getExternalStorageDirectory(),"AppuserManager.xml");
        FileOutputStream fos;


        try {
            fos = new FileOutputStream(file);

            xs.setOutput(fos,"utf-8");
            //3、开始生成xml文件
            xs.startDocument("utf-8",true);
            xs.startTag(null,"Root");
//            for (int i = 0; i < list.size(); i++) {
                xs.startTag(null,"SubModelNode");
                xs.attribute("","ModelName","AppUser_mod");


                xs.startTag(null,"TableName");

                xs.text(list.get(0).getTableName());
                xs.endTag(null,"TableName");

                xs.startTag(null,"Fields");
                xs.attribute("","DataType","String");
                xs.text(list.get(0).getFields());
                xs.endTag(null,"Fields");

                xs.startTag(null,"Where");
                xs.attribute("","String");
                xs.endTag(null,"Where");

                xs.startTag(null,"OrderBy");
                xs.attribute("","String");
                xs.text(list.get(0).getOrderBy());
                xs.endTag(null,"OrderBy");

                xs.startTag(null,"PageIndex");
                xs.attribute("","Int32");// xs.attribute 一定要在xs.text 的上面否则就报错
                xs.text(list.get(0).getPageIndex()+"");
                xs.endTag(null,"PageIndex");

                xs.startTag(null,"PageSize");
            xs.attribute("","Int32");
            xs.text(list.get(0).getPageSize()+"");
                xs.endTag(null,"PageSize");

                xs.endTag(null,"SubModelNode");
//            }/////////////////////

            xs.endTag(null,"Root");
            xs.endDocument();
            tv_text.setText(fos.toString());
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

写进去,在读出来,参考http://www.tuicool.com/articles/A7BNz2i

第二种:只是把对象转成了 xml格式,并不写在文件

package com.example.hasee.myxml;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.thoughtworks.xstream.XStream;

/**
 * 创建日期:${date} ${time}
 * 项目名称:${project_name}
 *
 * @author Jiang Shan Shan
 * @version 1.0
 * @since JDK 1.7.0_79
 * 文件名称: ${file_name}
 * 类说明:
 */
public class MainActivity2 extends Activity{

    private TextView tv_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_text = (TextView)findViewById(R.id.tv_text);

        AppuserManager ap = new AppuserManager("appuser",0);

        /*//把对象转化为 XML
        XStream xstream1 = new XStream();
        String string = xstream1.toXML( ap ) ;
        System.out.println( "sss"+ string );
        tv_text.setText(string);*/

        //把对象转化为 XML,并且设置别名
        XStream xstream2 = new XStream();
        xstream2.alias( "Root",AppuserManager.class) ;   //修改别名
        String string2 = xstream2.toXML( ap ) ;//把最外层 标签com.example.hasee.myxml 改成Root
        System.out.println(  string2 );
        tv_text.setText(string2);

    }
}

第二种方法基于工具类,网上可以下载 xstream-1.4.7.jar 文件。。。。

第二种方法的效率特别的高,而且所需写代码很少,优先选择第二种

猜你在找的XML相关文章