你的位置:首页 > 操作系统

[操作系统]android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件


前言:对xstream不理解的请看:

android

android

android

android

 

1、建立JavaBeen

package com.android10;public class Person {  String pName ;  String pAge ;    public String getpName() {    return pName;  }  public void setpName(String pName) {    this.pName = pName;  }  public String getpAge() {    return pAge;  }  public void setpAge(String pAge) {    this.pAge = pAge;  } }

 

package com.android10;public class Product {  private String name ;  private String age ;  private Person person ;  public String getName() {    return name;  }  public void setName(String name) {    this.name = name;  }  public String getAge() {    return age;  }  public void setAge(String age) {    this.age = age;  }  public Person getPerson() {    return person;  }  public void setPerson(Person person) {    this.person = person;  }}


2、工具类代码

package com.android10;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import android.content.Context;import android.os.Environment;public class XstreamUtil {  XcallBack xcallBack ;  /**   * 把@param context   * @param str    xml字符串   */  public void writeTo//获取文件路径    String SDPATH = Environment.getExternalStorageDirectory() + "/myfile1. ;    //创建文件    File file = new File( SDPATH ) ;    if( !file.exists() ){      try {        file.createNewFile() ;      } catch (IOException e) {        e.printStackTrace();      }     }    //写入数据    try {      FileOutputStream out = new FileOutputStream( file ) ;      OutputStreamWriter outw = new OutputStreamWriter(out);       try {         outw.write(str);         outw.close();         out.close();         xcallBack.success();       } catch (IOException e) {         xcallBack.fail();       }     } catch (FileNotFoundException e1) {      e1.printStackTrace();      xcallBack.fail();     }  }   void setXStreamLister( XcallBack xcallBack ){    this.xcallBack = xcallBack ;  }}interface XcallBack{  /**   * 写入成功   */  void success() ;   /**   * 写入失败   */  void fail() ;   }


3、主要方法

package com.android10;import android.app.Activity;import android.os.Bundle;import android.widget.Toast;import com.thoughtworks.xstream.XStream;public class MainActivity extends Activity {  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView( R.layout.activity_main );    Person person = new Person() ;    person.setpName( "saliy" ) ;    person.setpAge( "36" );    Product product = new Product() ;    product.setName( "jhon" ) ;    product.setAge( "30" );    product.setPerson( person );    //将对象转化为    XStream xstream = new XStream() ;    //设置别名    xstream.alias( "blog" , Product.class ) ;     String string = xstream.to= new XstreamUtil() ;    xUtil.setXStreamLister( new XcallBack() {      @Override      public void success() {        Toast.makeText( MainActivity.this , "成功了 ", Toast.LENGTH_SHORT ).show();       }      @Override      public void fail() {        Toast.makeText( MainActivity.this , "失败了 ", Toast.LENGTH_SHORT ).show();       }    });    xUtil.writeTothis , string );  }}

 

4、运行结果

 

<blog>
  <age>30</age>
  <name>jhon</name>
  <person>
    <pAge>36</pAge>
    <pName>saliy</pName>
  </person>
</blog>