星空网 > 软件开发 > 操作系统

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、运行结果

android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件images/loading.gif' data-original="http://images0.cnblogs.com/blog2015/605655/201506/151118387795115.png" />

 

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

 




原标题:android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

LinkShare:https://www.ikjzd.com/w/1573202233781903362
临沂综试区:https://www.ikjzd.com/w/1573202236743081985
亚马逊礼品卡:https://www.ikjzd.com/w/1573202237388660738
理士知识产权:https://www.ikjzd.com/w/1573202238206550017
丽水综试区:https://www.ikjzd.com/w/1573202238869594113
Listing Eagle:https://www.ikjzd.com/w/1573202240496640002
在英国有分公司的中国公司:https://www.xlkjsw.com/tag/30029.html
英国成立:https://www.xlkjsw.com/tag/30031.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流