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

[操作系统]android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象


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

android

android

android

 

1、文件准备

把一个预先写好的

文件内容为:

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

 

 

2、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;  }}


3、主要方法

package com.android10;import java.io.IOException;import java.io.InputStream;import android.app.Activity;import android.os.Bundle;import com.thoughtworks.xstream.XStream;public class MainActivity extends Activity {  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView( R.layout.activity_main );    //得到资源中的数据流     String fileName = "aa.//文件名字      try {      InputStream in = getResources().getAssets().open( fileName ) ;      XStream xstream = new XStream() ;        //这个blog标识一定要和       xstream.alias( "blog" , Product.class );       Product product = (Product) xstream.from"sss " + product.getName() + product.getAge() );    } catch (IOException e) {      e.printStackTrace();    }   }}


4、运行结果

 product jhon30 person  saliy36