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

时间:2023-12-12 09:29:20

前言:对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、运行结果

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

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