android Xml 解析之 XStream 解析
XStream解析
XStream 是一个开源项目,一套简单实用的类库,用于序列化对象与 XML 对象之间的相互转换,将 XML 文件内容解析为一个对象或将一个对象序列化为 XML 文件.
1. 下载路径
http://x-stream.github.io/download.html
2. 常用方法API
(1)fromXML()方法,返回值是Object
xStream.fromXML( File file) ;
xStream.fromXML( InputStream in) ;
xStream.fromXML( Reader reader ) ;
xStream.fromXML( String xml ) ;
xStream.fromXML( URL url ) ;
xStream.fromXML( File file , Object root ) ;
xStream.fromXML( InputStream in , Object root ) ;
xStream.fromXML( Reader reader , Object root ) ;
xStream.fromXML( String xml , Object root ) ;
xStream.fromXML( URL url , Object root ) ;
(2)toXML()方法
xStream.toXML( Object object ) ; //返回值String
xStream.toXML( Object object , OutputStream out ); //返回值void
xStream.toXML( Object object , Writer writer ); //返回值void
(3)设置别名
根节点设置别名:参数一:别名 ; 参数二:当前的类名
xstream.alias( “blog” , Product.class ) ;
XStream 解析android项目 asset 文件夹 xml 文件
1. packageupdate.xml
<?xml version="1.0" encoding="utf-8"?>
<result>
<version>1.0</version>
<upgradeMode>1</upgradeMode>
<packages>
<versionName>1.00.01</versionName>
<url> <![CDATA[http://www.baidu.com]]></url>
<level>1</level>
<size>5550672</size>
<md5>A48F51DB26DE9EA8492AC5031647E8A2</md5>
<time>2016-06-27</time>
<desc> <![CDATA[ 1、修正了有线连接显示问题 ]]> </desc>
</packages>
</result>
2. Java 实体类
Result.java
package com.sky.xsteamtoparserxml;
/**
* Created by JerryLiu on 2016/7/19.
*/
public class Result {
private String version;
private int upgradeMode;
private Packages packages;
public int getUpgradeMode() {
return upgradeMode;
}
public void setUpgradeMode(int upgradeMode) {
this.upgradeMode = upgradeMode;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Packages getPackages() {
return packages;
}
public void setPackages(Packages packages) {
this.packages = packages;
}
}
package.java
package com.sky.xsteamtoparserxml;
/**
* Created by JerryLiu on 2016/7/19.
*/
public class Packages {
private String versionName;
private String url;
private int level;
private int size;
private String md5;
private String time;
private String desc;
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
3. 实现代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String fileName = "packageupdate.xml";
InputStream in = null;
try {
in = getResources().getAssets().open(fileName);
XStream xStream = new XStream();
xStream.alias("result", Result.class);
Result result = (Result) xStream.fromXML(in);
LogUtils.d("version = " + result.getVersion() + "\nupgradeMode = " + result.getUpgradeMode());
LogUtils.d(result.getPackages());
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. 运行结果
D/LogUtils-/MainActivity.onCreate(MainActivity.java:30): version = 1.0;
upgradeMode = 1;
Packages {desc = " 1、修正了有线连接显示问题 ",
versionName = "1.00.01",
md5 = "A48F51DB26DE9EA8492AC5031647E8A2",
url = " http://www.baidu.com",
time = "2016-06-27",
size = 5550672,
level = 1}