1. 程式人生 > >XStream工具類解析XML

XStream工具類解析XML

這裡使用XUtils進行網路請求XML,使用XStream進行解析XML,封裝成集合



----------

 - XML資料

<root><resultcode>200</resultcode><reason>success</reason><result><item><id>242</id><catalog>中國文學</catalog></item><item><id>252</id><catalog>人物傳記</catalog
>
</item><item><id>244</id><catalog>兒童文學</catalog></item><item><id>248</id><catalog>歷史</catalog></item><item><id>257</id><catalog>哲學</catalog></item><item><id>243</id><catalog
>
外國文學</catalog></item><item><id>247</id><catalog>小說</catalog></item><item><id>251</id><catalog>心靈雞湯</catalog></item><item><id>253</id><catalog>心理學</catalog></item><item><id>250</id
>
<catalog>成功勵志</catalog></item><item><id>249</id><catalog>教育</catalog></item><item><id>245</id><catalog>散文</catalog></item><item><id>256</id><catalog>理財</catalog></item><item><id>254</id><catalog>管理</catalog></item><item><id>246</id><catalog>經典名著</catalog></item><item><id>255</id><catalog>經濟</catalog></item><item><id>258</id><catalog>計算機</catalog></item></result><error_code>0</error_code></root>
  • xml資料對應的bean類(注意欄位要新增完整)
 - package com.example.bean;

import java.util.ArrayList;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

//添加註解
@XStreamAlias("root")
public class MyRoot {
      public String resultcode;
      public String reason;
      public String error_code;

    @XStreamAlias("result")
    public MyResult result;
    public static class MyResult
    {   
        @XStreamImplicit(itemFieldName="item")
        public ArrayList<MyItem> item;

        public static class MyItem
        {
            public String id;
            public String catalog;
        }
    }
}
  • 在Activity中
 -package com.example.myandroid_xmlstream;

import java.util.ArrayList;

import com.example.bean.MyRoot;
import com.example.bean.MyRoot.MyResult.MyItem;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.thoughtworks.xstream.XStream;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {



    //xml路徑
    private String url="http://apis.juhe.cn/goodbook/catalog?key=cdcde130a384bdf960030a563e7e1954&dtype=xml";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //請求網路
        getNetWork(url);
    }
    private void getNetWork(String path) {

        //建立請求物件
        HttpUtils httpUtils = new HttpUtils();

        //設定傳送的請求方式,路徑,返回的回撥方法
        httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {

            //請求失敗
            @Override
            public void onFailure(HttpException arg0, String arg1) {

            }
            //請求成功
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                //獲取xml字串
                String xmlString = arg0.result;
                //解析xml
                xStreamXml(xmlString);
            }

        });


    }
    //解析xml的方法
    private void xStreamXml(String xmlString) {
        //建立解析xml的工具類物件
        XStream xs = new XStream();
        //處理註解
        xs.processAnnotations(MyRoot.class);
        //根據xml資料進行反射成bean類
        MyRoot root = (MyRoot) xs.fromXML(xmlString);
        //建立對應的集合
        ArrayList<MyItem> ilist = new ArrayList<MyItem>();
        //為集合賦值
        ilist = root.result.item;
        //遍歷集合列印得出的資料
        for (MyItem myItem : ilist) {
        System.out.println("id="+myItem.id+"::catalog="+myItem.catalog);
    }


    }


}