package com.hym.xStream;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("c")
public class BKEuropOddsDTO {
@XStreamImplicit(itemFieldName = "h")
private List<Match> matchs;
public List<Match> getMatchs() {
return matchs;
}
public void setMatchs(List<Match> matchs) {
this.matchs = matchs;
}
@XStreamAlias("h")
public static class Match {
@XStreamAlias("id")
private String id;
@XStreamAlias("odds")
private Odds odds;
@XStreamAlias("time")
private String time;
@XStreamAlias("league")
private String league;
@XStreamAlias("home")
private String home;
@XStreamAlias("away")
private String away;
@XStreamAlias("odds")
public static class Odds {
@XStreamImplicit(itemFieldName = "o")
private List<String> o;
public List<String> getO() {
return o;
}
public void setO(List<String> o) {
this.o = o;
}
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getLeague() {
return league;
}
public void setLeague(String league) {
this.league = league;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getAway() {
return away;
}
public void setAway(String away) {
this.away = away;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Odds getOdds() {
return odds;
}
public void setOdds(Odds odds) {
this.odds = odds;
}
}
}
package com.hym.xStream;
import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XppDomDriver;
public class XStreamTest {
private static final String xml = "<?xml version=\"1.0\" encoding=\"UTF8\"?><c><h><id>155991</id><time>2013/4/27 8:00:00</time><league>NBA,美國男子職業籃球聯賽,美国男子职业篮球联赛</league><home>Boston Celtics,波士頓塞爾特人,波士顿凯尔特人</home><away>New York Knicks,紐約人,纽约尼克斯</away><odds><o>379,JustBet,1.69,2.25,1.68,2.28,2013/4/26 14:07:38</o><o>431,Sporttery Official,1.65,1.84,1.68,1.81,2013/4/26 14:07:37</o></odds></h><h><id>155976</id><time>2013/4/27 10:30:00</time><league>NBA,美國男子職業籃球聯賽,美国男子职业篮球联赛</league><home>Los Angeles Lakers,洛杉磯湖人,洛杉矶湖人</home><away>San Antonio Spurs,聖安東尼奧馬刺,圣安东尼奥马刺</away><odds><o>278,Sportbet,2.85,1.5,2.88,1.49,2013/4/26 14:07:49</o><o>11,5Dimes,2.85,1.5,2.88,1.49,2013/4/26 14:07:49</o><o>334,Island Casino,2.85,1.5,2.88,1.49,2013/4/26 14:07:49</o></odds></h><h><id>155972</id><time>2013/4/27 10:30:00</time><league>NBA,美國男子職業籃球聯賽,美国男子职业篮球联赛</league><home>Golden State Warriors,金州勇士,金州勇士</home><away>Denver Nuggets,丹佛金塊,丹佛掘金</away><odds><o>334,Island Casino,1.89,2.02,1.93,1.98,2013/4/26 14:05:55</o><o>278,Sportbet,1.89,2.02,1.93,1.98,2013/4/26 14:05:55</o><o>11,5Dimes,1.89,2.02,1.93,1.98,2013/4/26 14:05:54</o></odds></h></c>";
public static void main(String[] args) {
XStreamTest test = new XStreamTest();
test.parseXML(xml);
}
public void parseXML(String content) {
if (content == null || content.length() == 0) {
return;
}
XStream xs = new XStream(new XppDomDriver());
// xs.autodetectAnnotations(true);
//xs.alias("h", List.class);
xs.processAnnotations(BKEuropOddsDTO.class);
BKEuropOddsDTO bkEuropOddsDTO = (BKEuropOddsDTO) xs.fromXML(content);
List<BKEuropOddsDTO.Match> list = bkEuropOddsDTO.getMatchs();
// List<String> list = (List<String>) xs.fromXML(content);
if (list == null || list.size() <= 0) {
return;
}
for (int i = 0; i < list.size(); i++) {
BKEuropOddsDTO.Match h = list.get(i);
System.out.println("h.MatchId = " + h.getId());
BKEuropOddsDTO.Match.Odds odd = h.getOdds();
List<String> odds = odd.getO();
for (String o : odds) {
System.out.println("o = " + o);
}
}
}
}
没有别的窍门,根据xml的层次来定义实体类,就不会出错。