xstream解析xml问题

问题描述

<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]]

时间: 2025-01-24 12:41:43

xstream解析xml问题的相关文章

用XStream 解析xml文件时,遇到&amp;amp;lt;![cdata[]]&amp;amp;gt;就报错,怎么办

问题描述 用XStream 解析xml文件时,遇到<![cdata[]]>就报错,怎么办 求给位大牛帮忙,为何我用XStream解析xml文件,遇到<![cdata[]]>,就解析不了,很着急困惑,呜呜呜 解决方案 这个CDATA不是大写吗!!!!!!

xstream中xml转bean-xstream解析xml为bean时一直出差,求帮忙看一下,在线等

问题描述 xstream解析xml为bean时一直出差,求帮忙看一下,在线等 public class BulletinResponse { private String transId; private String processTime; private String cpId; private String curPage; private String pageCount; private String returnCode; private String content; priva

xstream解析问题-xstream将xml解析为bean,出差,急急急

问题描述 xstream将xml解析为bean,出差,急急急 这是我定义的两个用来接收的bean public class BulletinResponse { private String transId; private String processTime; private String cpId; private String curPage; private String pageCount; private String returnCode; private String cont

用xstream解析map的问题,求助

问题描述 用xstream解析map的问题,求助 大家好,如图所示是被人发送过来的xml.我想要把这个xml转化成对象来自己实用,但是黑圈里面的东西,我百度一天了还是找不到解决之法,特来找各位求助,麻烦帮忙看看,提供下思路,感激不尽,鞠躬 --一只奋斗的小菜鸟 解决方案 求指教,求助攻啊!求不吝指教

java-jquery解析xml怎样把结果存到list或者hashmap最后生成excel表格

问题描述 jquery解析xml怎样把结果存到list或者hashmap最后生成excel表格 $.ajax({ type:""GET"" dataType:""XML"" timeout: 1000 //设定超时 cache: false //禁用缓存 url:""${pageContext.request.contextPath}/xml/from.xml"" success:fun

unity3d-Unity3D 实时解析XML时报错

问题描述 Unity3D 实时解析XML时报错 获取网络时间: IEnumerator GetTime() { WWW www = new WWW(timeURL); while (!www.isDone) { yield return www; Debug.Log("Doneeeeeeeeeeeeee" + www.text); ParseXml(www); } }` 后得到:` <?xml version="1.0" encoding="GB23

JS解析XML文件和XML字符串详解

 JS解析XML文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <script type='text/javascript'> loadXML = function(xmlFile){ var xmlDoc=null; //判断浏览器的类型 //支持IE浏览器 if(!window.DOMParser && windo

python sax模块解析xml遇到非法字符怎么解决?

问题描述 python sax模块解析xml遇到非法字符怎么解决? 1C 用python的sax模块解析xml的时候出现非法字符直接停止了,百度搜到有大神说用回调函数处理当前的非法字符或者跳过直接运行接下来的内容.不过具体应该怎么实现呢?求大神赐教.下面是我的代码,非法字符是出现再其中的很多个tagname=""url""中的内容,能在原来的基础上改就更好了,再次感谢 import xml.saxclass XmlHandler( xml.sax.ContentHa

解析xml文档的一个简单php类

xml //原创,可以自由使用,欢迎提出改进意见, <?PHP//xml中的元素class XMLTag{ var $parent;//父节点 var $child;//子节点 var $attribute;//本节点属性 var $data;//本节点数据 var $TagName;//本节点名称 var $depth;//本节点的深度,根节点为1 function XMLTag($tag='') {  $this->attribute = array();  $this->child