首先我想说,你的xml的结构不是很好。。。。。。
当然了以上你的xml也是可以解析的,看下面的例子
需要jar包 xstream-1.4.2.jar
Ats.java
package com.xulonghui.entity;
import java.io.Serializable;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("ats")
public class Ats implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2215367669576006438L;
private String version;
private boolean result;
private Integer code;
private String message;
private long timestamp;
private Integer total;
@XStreamImplicit
private List<GeoCode> geoCodes;
public String getVersion() {
return this.version;
}
public void setVersion(String version) {
this.version = version;
}
public boolean isResult() {
return this.result;
}
public void setResult(boolean result) {
this.result = result;
}
public Integer getCode() {
return this.code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public long getTimestamp() {
return this.timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public Integer getTotal() {
return this.total;
}
public void setTotal(Integer total) {
this.total = total;
}
public List<GeoCode> getGeoCodes() {
return this.geoCodes;
}
public void setGeoCodes(List<GeoCode> geoCodes) {
this.geoCodes = geoCodes;
}
}
GeoCode.java
package com.xulonghui.entity;
import java.io.Serializable;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("geocode")
public class GeoCode implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6655251876533605578L;
private String district;
private Integer level;
private String country;
private String region;
private String cityname;
private String longitude;
private String formattedaddress;
private String latitude;
@Override
public String toString() {
return "GeoCode [district=" + this.district + ", level=" + this.level + ", country=" + this.country
+ ", region=" + this.region + ", cityname=" + this.cityname + ", longitude=" + this.longitude
+ ", formattedaddress=" + this.formattedaddress + ", latitude=" + this.latitude + "]";
}
public String getDistrict() {
return this.district;
}
public void setDistrict(String district) {
this.district = district;
}
public Integer getLevel() {
return this.level;
}
public void setLevel(Integer level) {
this.level = level;
}
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
public String getRegion() {
return this.region;
}
public void setRegion(String region) {
this.region = region;
}
public String getCityname() {
return this.cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
public String getLongitude() {
return this.longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getFormattedaddress() {
return this.formattedaddress;
}
public void setFormattedaddress(String formattedaddress) {
this.formattedaddress = formattedaddress;
}
public String getLatitude() {
return this.latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
}
Main.java
package com.xulonghui.main;
import java.io.File;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xulonghui.entity.Ats;
public class Main {
public static void main(String[] args) {
XStream xStream = new XStream(new DomDriver());
xStream.setMode(XStream.NO_REFERENCES);
xStream.autodetectAnnotations(true);
xStream.alias("ats", Ats.class);
Ats ats = (Ats) xStream.fromXML(new File("c://ats.xml"));
System.out.println(ats.getGeoCodes());
}
}
执行后的结果为:
[GeoCode [district=上街区, level=7, country=中国, region=河南省, cityname=郑州市, longitude=113.313662, formattedaddress=河南省郑州市上街区东方明珠, latitude=34.809508], GeoCode [district=滕州市, level=7, country=中国, region=山东省, cityname=枣庄市, longitude=117.176897, formattedaddress=山东省枣庄市滕州市东方明珠, latitude=35.104039]]