您现在的位置是:首页 > 文章详情

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

日期:2017-05-15点击:367
前言:对xstream不理解的请看:

android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件

android XMl 解析神奇xstream 二: 把对象转换成xml

android XMl 解析神奇xstream 三: 把复杂对象转换成 xml

android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象

 

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 ; /** * 把xml字符串写入SD卡文件 * @param context * @param str xml字符串 */ public void writeToXml(Context context, String str ){ //获取文件路径 String SDPATH = Environment.getExternalStorageDirectory() + "/myfile1.xml/" ; //创建文件 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 ); //将对象转化为xml字符串 XStream xstream = new XStream() ; //设置别名 xstream.alias( "blog" , Product.class ) ; String string = xstream.toXML( product ) ; XstreamUtil xUtil = 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.writeToXml( this , string ); } }

 

4、运行结果

 

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

 

原文链接:https://yq.aliyun.com/articles/304065
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章