android下通过xstream解析XML

来源:互联网 发布:蜂窝数据无法登陆应用 编辑:程序博客网 时间:2024/06/06 09:14
可以通过json格式向android http客户端传输数据,见:android下支持json的远程访问,也可以用xml格式。
  下面是一个xml文件的格式示例。
?
代码片段,双击复制
01
02
03
04
<product> 
    <name>NetGear 614v9无线路由器</name> 
    <createTime>2009-10-2700:00:00.0CST</createTime> 
</product>

  下载或者访问该xml文件:[Download not found]
  如果解析上面的xml文件呢?这里选用了xstream,网址:
  
?
代码片段,双击复制
01
http://xstream.codehaus.org/

  xstream可以自动解析文件,并且根据xml数据实例化javabean。如果不这样,需要手工编写SAX API代码解析。
  首先编写了一个对应的Product的javabean:
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.easymorse; 
  
import java.util.Date; 
  
public class Product {
    @Override
    publicString toString() {
        return“Product [createTime=" + createTime.toLocaleString() + ", name=" + name + "]“;
   
  
    privateString name; 
  
    publicString getName() {
        returnname;
   
  
    publicvoid setName(String name) {
        this.name = name;
   
  
    publicDate getCreateTime() {
        returncreateTime;
   
  
    publicvoid setCreateTime(Date createTime) {
        this.createTime = createTime;
   
  
    privateDate createTime;
}

  然后,需要类似这样调用xstream的代码(代码还是改自实现android activity之间的跳转):
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.easymorse; 
  
import java.io.BufferedReader;
import java.io.InputStreamReader; 
  
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; 
  
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView; 
  
import com.thoughtworks.xstream.XStream; 
  
public class NextActivity extendsActivity {
    privateTextView textView;
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.next_activity);
        this.textView=(TextView)this.findViewById(R.id.TextView01); 
  
        HttpClient client =new DefaultHttpClient();
        StringBuilder builder =new StringBuilder(); 
  
        HttpGet get =new HttpGet(
               “http://marshal.easymorse.com/wp-content/uploads/2009/10/product2.xml”);
        try{
            HttpResponse response = client.execute(get);
            BufferedReader reader =new BufferedReader(newInputStreamReader(
                    response.getEntity().getContent()));
            for(String s = reader.readLine(); s != null; s = reader.readLine()) {
               builder.append(s);
            }
            Log.v(“response”,”product:”+builder.toString());
            XStream xstream =new XStream();
            xstream.alias(“product”, Product.class);
            Product product=(Product) xstream.fromXML(builder.toString());
            this.textView.setText(product.toString());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  执行NextActivity的截图:
123.jpg
2012-7-24 12:00 上传
下载附件(29.44 KB)

  从服务器端返回的中文内容能够正确解码。不过,如果通过eclipse插件中的ddms日志,看到的是乱码,估计和日志或者eclipse插件默认字符集有关 。
321.jpg
2012-7-24 12:00 上传
下载附件(35.43 KB)

  另外,想要使用xstream需要引入xstream包。具体方法见:在eclipse的android项目中引入第三方包。在这里xstream又依赖xpp3用于对xml解析。xpp3的网址:
  
?
代码片段,双击复制
01
http://www.extreme.indiana.edu/xgws/xsoap/xpp/

  可以在这里下载到最新的xpp3分发包:
 
?
代码片段,双击复制
01
 http://www.extreme.indiana.edu/dist/java-repository/xpp3/distributions/

  然后解压缩,将其中的xpp3_min-*.jar导入项目即可。
  或者也可以选择不依赖xpp3包,这样可以节省24K左右的空间。需要实例化XStream时:
  
?
代码片段,双击复制
01
XStream xstream = new XStream(newDomDriver());

  另外,日期格式用:
  
?
代码片段,双击复制
01
2012-07-2300:00:00.0CST

  是为了直接转型方面,如果比较复杂,需要实现xstream的转型接口做定制实现:
 
?
代码片段,双击复制
01
 http://xstream.codehaus.org/converter-tutorial.html