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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM