关闭

XStream解析xml数据

124人阅读 评论(0) 收藏 举报

javaBean 这里需要注意注解 大概意思应该就是标识

package com.example.bean;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("root")
public class Book {

    private String resultcode;
    private String reason;
    private Res result;
    private String error_code;
    public String getResultcode() {
        return resultcode;
    }
    public void setResultcode(String resultcode) {
        this.resultcode = resultcode;
    }
    public String getReason() {
        return reason;
    }
    public void setReason(String reason) {
        this.reason = reason;
    }
    public Res getResult() {
        return result;
    }
    public void setResult(Res result) {
        this.result = result;
    }
    public String getError_code() {
        return error_code;
    }
    public void setError_code(String error_code) {
        this.error_code = error_code;
    }
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Book(String resultcode, String reason, Res result, String error_code) {
        super();
        this.resultcode = resultcode;
        this.reason = reason;
        this.result = result;
        this.error_code = error_code;
    }
    @Override
    public String toString() {
        return "Book [resultcode=" + resultcode + ", reason=" + reason
                + ", result=" + result + ", error_code=" + error_code + "]";
    }

}
package com.example.bean;

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("result")
public class Res {
    @XStreamImplicit(itemFieldName="item")
    private List<TExt> item;

    public List<TExt> getItem() {
        return item;
    }

    public void setItem(List<TExt> item) {
        this.item = item;
    }

    public Res() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Res(List<TExt> item) {
        super();
        this.item = item;
    }

    @Override
    public String toString() {
        return "Res [item=" + item + "]";
    }

}
package com.example.bean;


public class TExt {

    private String id;
    private String catalog;
    @Override
    public String toString() {
        return "TExt [id=" + id + ", catalog=" + catalog + "]";
    }
    public TExt(String id, String catalog) {
        super();
        this.id = id;
        this.catalog = catalog;
    }
    public TExt() {
        super();
        // TODO Auto-generated constructor stub
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getCatalog() {
        return catalog;
    }
    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }
}

这里面需要导入XStream包

package com.example.xstreamdemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.bean.Book;
import com.example.bean.Res;
import com.example.bean.TExt;
import com.thoughtworks.xstream.XStream;


public class MainActivity extends Activity {

    private String url="http://apis.juhe.cn/goodbook/catalog?key=eb487dc80b175790e914b7f62e94c478&dtype=xml";
    private HttpURLConnection connection;
    private String json;
    private Handler handler=new Handler(){

        public void handleMessage(Message msg) {
            XStream tream=new XStream();
            tream.processAnnotations(Book.class);
            Book bb = (Book) tream.fromXML(json);
            Res result = bb.getResult();
            List<TExt> item = result.getItem();
            Log.i("item", item.toString());
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(){

            public void run() {
                json = getjiexi(url);
                Message msg=new Message();
                msg.obj=
                handler.sendMessage(msg);
            };
        }.start();

    }
    protected String getjiexi(String url2) {
        String ss=null;
           try {
               URL url=new URL(url2);
               connection = (HttpURLConnection) url.openConnection();
               connection.setRequestMethod("GET");
               connection.setConnectTimeout(8000);
               connection.setReadTimeout(5000);
               InputStream stream = connection.getInputStream();
               BufferedReader reader=new BufferedReader(new InputStreamReader(stream));
               final StringBuilder builder=new StringBuilder();
               String line;
            while((line=reader.readLine())!=null){
                   builder.append(line);
                    ss = builder.toString();

               }
            Log.i("ss", ss);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ss;
    }




}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2322次
    • 积分:107
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档