Xstream解析XML

复制代码
<oschina>
   <catalog>1</catalog>
   <newsCount>0</newsCount>
   <pagesize>20</pagesize>
   <newslist>
     <news>
       <id>72168</id>
       <title>
       <![CDATA[ PC-BSD 10.3 发布,桌面 BSD 系统 ]]>
       </title>
       <body>
       <![CDATA[ PC-BSD 10.3 发布了。 PC-BSD10.3使用了一个带有可选择的供替代的GRUB ... ]]>
       </body>
       <commentCount>0</commentCount>
      <author>
      <![CDATA[ oschina ]]>
      </author>
      <authorid>1</authorid>
      <pubDate>2016-04-05 07:35:28</pubDate>
      <url/>
      <newstype>
        <type>0</type>
        <authoruid2>1</authoruid2>
        <eventurl/>
      </newstype>
   </news>
   <news>
       <id>72168</id>
       <title>
       <![CDATA[ PC-BSD 10.3 发布,桌面 BSD 系统 ]]>
       </title>
       <body>
       <![CDATA[ PC-BSD 10.3 发布了。 PC-BSD10.3使用了一个带有可选择的供替代的GRUB ... ]]>
       </body>
       <commentCount>0</commentCount>
      <author>
      <![CDATA[ oschina ]]>
      </author>
      <authorid>1</authorid>
      <pubDate>2016-04-05 07:35:28</pubDate>
      <url/>
      <newstype>
        <type>0</type>
        <authoruid2>1</authoruid2>
        <eventurl/>
      </newstype>
     </news>
   </newslist>
</oschina>
复制代码

Vo包   导xstream-1.4.7.jar包

/YueKao/src/com/bawei/vo/Good.java

复制代码
package com.bawei.vo;

import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("oschina")
public class Good {
private String catalog;
private String newsCount;
private String pagesize;
private Mynewslist newslist;


public String getCatalog() {
    return catalog;
}
public void setCatalog(String catalog) {
    this.catalog = catalog;
}
public String getNewsCount() {
    return newsCount;
}
public void setNewsCount(String newsCount) {
    this.newsCount = newsCount;
}
public String getPagesize() {
    return pagesize;
}
public void setPagesize(String pagesize) {
    this.pagesize = pagesize;
}
public Mynewslist getNewslist() {
    return newslist;
}
public void setNewslist(Mynewslist newslist) {
    this.newslist = newslist;
}
@Override
public String toString() {
    return "Good [catalog=" + catalog + ", newsCount=" + newsCount
            + ", pagesize=" + pagesize + ", newslist=" + newslist + "]";
}



}
复制代码

/YueKao/src/com/bawei/vo/Mynewslist.java

复制代码
package com.bawei.vo;

import java.util.List;

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

@XStreamAlias("newslist")
public class Mynewslist {
     @XStreamImplicit(itemFieldName="news")
       List<Mynews>  news;

    public List<Mynews> getNews() {
        return news;
    }

    public void setNews(List<Mynews> news) {
        this.news = news;
    }

    @Override
    public String toString() {
        return "Mynewslist [news=" + news + "]";
    }
     
}
复制代码

/YueKao/src/com/bawei/vo/Mynews.java

复制代码
package com.bawei.vo;

public class Mynews {
private String id;
private String title;
private String body;
private String commentCount;
private String author;
private String authorid;
private String pubDate;
private String url;
private Mynewstype newstype;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getBody() {
    return body;
}
public void setBody(String body) {
    this.body = body;
}
public String getCommentCount() {
    return commentCount;
}
public void setCommentCount(String commentCount) {
    this.commentCount = commentCount;
}
public String getAuthor() {
    return author;
}
public void setAuthor(String author) {
    this.author = author;
}
public String getAuthorid() {
    return authorid;
}
public void setAuthorid(String authorid) {
    this.authorid = authorid;
}
public String getPubDate() {
    return pubDate;
}
public void setPubDate(String pubDate) {
    this.pubDate = pubDate;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
public Mynewstype getNewstype() {
    return newstype;
}
public void setNewstype(Mynewstype newstype) {
    this.newstype = newstype;
}
@Override
public String toString() {
    return "Mynews [id=" + id + ", title=" + title + ", body=" + body
            + ", commentCount=" + commentCount + ", author=" + author
            + ", authorid=" + authorid + ", pubDate=" + pubDate + ", url="
            + url + ", newstype=" + newstype + "]";
}

}
复制代码

/YueKao/src/com/bawei/vo/Mynewstype.java

复制代码
package com.bawei.vo;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("newstype")
public class Mynewstype {
private String type;
private String authoruid2;
private String eventurl;
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
public String getAuthoruid2() {
    return authoruid2;
}
public void setAuthoruid2(String authoruid2) {
    this.authoruid2 = authoruid2;
}
public String getEventurl() {
    return eventurl;
}
public void setEventurl(String eventurl) {
    this.eventurl = eventurl;
}
@Override
public String toString() {
    return "Mynewstype [type=" + type + ", authoruid2=" + authoruid2
            + ", eventurl=" + eventurl + "]";
}


}
复制代码

解析

复制代码
     HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.POST, url , new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                String result = arg0.result;

XStream stream = new XStream(); stream.processAnnotations(Good.class); Good good = (Good) stream.fromXML(result); List<Mynews> news = good.getNewslist().getNews(); adapter = new Adpter(getActivity(), news); my_xlist.setAdapter(adapter); } });
复制代码
原文地址:https://www.cnblogs.com/wbp0818/p/5458581.html