android下通过xstream解析xml格式信息

==========推荐============

 实例教程-----会员贡献索引贴

http://www.eoeandroid.com/thread-1987-1-1.html

 

android 图像处理滤镜系列合集

http://www.eoeandroid.com/thread-178656-1-1.html

分享45个android实例源码

http://www.eoeandroid.com/thread-185986-1-1.html

==========帖子正文==========

可以通过json格式向android
http客户端传输数据,见:android下支持json的远程访问,也可以用xml格式。
  下面是一个xml文件的格式示例。

<product>  
    <name>NetGear 614v9无线路由器</name>  
    <createTime>2009-10-27 00:00:00.0 CST</createTime>  
</product>

下载或者访问该xml文件:[Download not found]
  如果解析上面的xml文件呢?这里选用了xstream,网址:

http://xstream.codehaus.org/

xstream可以自动解析文件,并且根据xml数据实例化javabean。如果不这样,需要手工编写SAX
API代码解析。
  首先编写了一个对应的Product的javabean:

package com.easymorse;  
  
import java.util.Date;  
  
public class Product { 
    @Override
    public String toString() { 
        return “Product [createTime=" + createTime.toLocaleString() + ", name=" + name + "]“; 
    }  
  
    private String name;  
  
    public String getName() { 
        return name; 
    }  
  
    public void setName(String name) { 
        this.name = name; 
    }  
  
    public Date getCreateTime() { 
        return createTime; 
    }  
  
    public void setCreateTime(Date createTime) { 
        this.createTime = createTime; 
    }  
  
    private Date createTime; 
}

然后,需要类似这样调用xstream的代码(代码还是改自实现android activity之间的跳转):

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 extends Activity { 
    private TextView textView; 
    @Override
    protected void 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(new InputStreamReader( 
                    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的截图:

从服务器端返回的中文内容能够正确解码。不过,如果通过eclipse插件中的ddms日志,看到的是乱码,估计和日志或者eclipse插件默认字符集有关 。

另外,想要使用xstream需要引入xstream包。具体方法见:在eclipse的android项目中引入第三方包。在这里xstream又依赖xpp3用于对xml解析。xpp3的网址:

http://www.extreme.indiana.edu/xgws/xsoap/xpp/

可以在这里下载到最新的xpp3分发包:

 http://www.extreme.indiana.edu/dist/java-repository/xpp3/distributions/

然后解压缩,将其中的xpp3_min-*.jar导入项目即可。
  或者也可以选择不依赖xpp3包,这样可以节省24K左右的空间。需要实例化XStream时:

XStream xstream = new XStream(new DomDriver());

另外,日期格式用:

2012-07-23 00:00:00.0 CST

是为了直接转型方面,如果比较复杂,需要实现xstream的转型接口做定制实现:

 http://xstream.codehaus.org/converter-tutorial.html

 

 

posted on 2012-07-26 14:27  vus520  阅读(4685)  评论(4编辑  收藏  举报

导航