xstream解析xml问题


2.0 - 2.0.3196.1747
true
1
Successful.
1374566330
15

上街区
7
中国
河南省
郑州市
113.313662
河南省郑州市上街区东方明珠
34.809508


滕州市
7
中国
山东省
枣庄市
117.176897
山东省枣庄市滕州市东方明珠
35.104039

这样的xml如何解析

我定义的
ats类中
private String version;
private String result;
private String code;
private String message;
private String timestamp;
private String total;
private List geocode;
这样的定义的

解析的时候geocode解析不出来。

谁能给我一个例子。

1个回答

首先我想说,你的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]]

xulonghui2011
随心TZ所欲 Ats.java 中将@XStreamImplicit 替换成 @XStreamAlias("geocodes"),Main 方法中实现是这样的: XStream xStream = new XStream(); xStream.setMode(XStream.NO_REFERENCES); xStream.autodetectAnnotations(true); List<GeoCode> geoCodes = new ArrayList<GeoCode>(); GeoCode gc1 = new GeoCode("上街区", 7, "中国", "河南省", "郑州市", "113.313662", "河南省郑州市上街区东方明珠", "34.809508"); GeoCode gc2 = new GeoCode("滕州市", 7, "中国", "山东省", "枣庄市", "117.176897", "山东省枣庄市滕州市东方明珠", "35.104039"); geoCodes.add(gc1); geoCodes.add(gc2); Ats ats = new Ats("2.0 - 2.0.3196.1747", true, 1, "Successful.", 1374566330, 15, geoCodes); System.out.println(xStream.toXML(ats)); 需要 xmlpull-1.1.3.1.jar 和 xstream-1.4.2.jar 支持;还有什么疑问可以直接邮件交流 xulonghui2011@163.com
6 年多之前 回复
xulonghui2011
随心TZ所欲 是这个意思,去掉之后,将javabean转换为xml的时候,如果属性是list,会出现一个root节点
6 年多之前 回复
fengfujie
fengfujie @XStreamImplicit 在问一下 我将这个注解去掉后,xml应该<geocodes><geocode>...</geocode></geocodes>这样吧,我现在为什么不出先这个geocodes那?
6 年多之前 回复
xulonghui2011
随心TZ所欲 呵呵 有用的话就采纳下,谢谢
6 年多之前 回复
fengfujie
fengfujie 非常感谢,我也知道这个xml格式不是很好,关键这个xml格式是我调用webservice接口,然后客户直接返回给我的。非常感谢,刚学习xstream。
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问