问题描述
<ats><version>2.0 - 2.0.3196.1747</version><result>true</result><code>1</code><message>Successful.</message><timestamp>1374566330</timestamp><total>15</total><geocode><district>上街区</district><level>7</level><country>中国</country><region>河南省</region><cityname>郑州市</cityname><longitude>113.313662</longitude><formattedaddress>河南省郑州市上街区东方明珠</formattedaddress><latitude>34.809508</latitude></geocode><geocode><district>滕州市</district><level>7</level><country>中国</country><region>山东省</region><cityname>枣庄市</cityname><longitude>117.176897</longitude><formattedaddress>山东省枣庄市滕州市东方明珠</formattedaddress><latitude>35.104039</latitude></geocode></ats>这样的xml如何解析我定义的ats类中private String version;private String result;private String code;private String message;private String timestamp;private String total;private List<GeocodeEntity> geocode;这样的定义的 解析的时候geocode解析不出来。谁能给我一个例子。
解决方案
首先我想说,你的xml的结构不是很好。。。。。。当然了以上你的xml也是可以解析的,看下面的例子需要jar包 xstream-1.4.2.jarAts.javapackage 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.javapackage 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.javapackage 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]]