1、下载工具 xstream
下载最新版本地址: https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/
下载完成后 把jar包导入到自己的android项目中
2、asset 文件夹 下的 aa.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<product>
<name>jike</name>
<age>30</age>
</product>
3、实体
package com.android10; public class Product { private String name ; private String age ; 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; } }
4、实现代码
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.xml" ; //文件名字 try { InputStream in = getResources().getAssets().open( fileName ) ; XStream xstream = new XStream() ; xstream.alias( "product" , Product.class ); Product product = (Product) xstream.fromXML( in ); System.out.println("sss " + product.getName() + product.getAge() ); } catch (IOException e) { e.printStackTrace(); } } }
5、运行结果
sss jike30
6、项目下载地址
http://download.csdn.net/detail/yanzi2015/8797641
时间: 2024-12-26 18:51:29