Java xml格式字符串转java对象

Java xml格式字符串转java对象,第1张

Java xml格式字符串转java对象 使用XStream实现xml格式字符串转java对象。
  1. 首先引入相关jar包依赖

	com.thoughtworks.xstream
	xstream
	1.4.10

  1. 实例xml字符串

	000
	
	
		
			20
			2016-12-05
			09:00
			14:00
			0
			0
		
		
			21
			2016-12-05
			14:00
			18:00
			0
			0
		
	

  1. 相关实体类
@XStreamAlias("ROOT")
public class Resever {
    private String result;
    private String errorDesc;
    @XStreamAlias("SEG_TIME_LIST")
    private List segTimeList;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getErrorDesc() {
        return errorDesc;
    }

    public void setErrorDesc(String errorDesc) {
        this.errorDesc = errorDesc;
    }

    public List getSegTimeList() {
        return segTimeList;
    }

    public void setSegTimeList(List segTimeList) {
        this.segTimeList = segTimeList;
    }
}
@XStreamAlias("SEG_TIME")
public class SegTime {
    private String segId;
    private String bookDate;
    private String segStartTime;
    private String segEndTime;
    private String bookedCount;
    private String bookMaxCount;

    public String getSegId() {
        return segId;
    }

    public void setSegId(String segId) {
        this.segId = segId;
    }

    public String getBookDate() {
        return bookDate;
    }

    public void setBookDate(String bookDate) {
        this.bookDate = bookDate;
    }

    public String getSegStartTime() {
        return segStartTime;
    }

    public void setSegStartTime(String segStartTime) {
        this.segStartTime = segStartTime;
    }

    public String getSegEndTime() {
        return segEndTime;
    }

    public void setSegEndTime(String segEndTime) {
        this.segEndTime = segEndTime;
    }

    public String getBookedCount() {
        return bookedCount;
    }

    public void setBookedCount(String bookedCount) {
        this.bookedCount = bookedCount;
    }

    public String getBookMaxCount() {
        return bookMaxCount;
    }

    public void setBookMaxCount(String bookMaxCount) {
        this.bookMaxCount = bookMaxCount;
    }
}
  1. 使用方式
String xml = "000202016-12-0509:0014:0000212016-12-0514:0018:0000";

XStream xstream=new XStream(new DomDriver());

xstream.processAnnotations(Resever.class);
// xml 标签与实体类属性名映射
//xstream.aliasField("SEG_TIME_LIST",Resever.class,"segTimeList");
xstream.aliasField("SEG_ID", SegTime.class,"segId");
xstream.aliasField("BOOK_DATE",SegTime.class,"bookDate");
xstream.aliasField("SEG_START_TIME",SegTime.class,"segStartTime");
xstream.aliasField("SEG_END_TIME",SegTime.class,"segEndTime");
xstream.aliasField("BOOKED_COUNT",SegTime.class,"bookedCount");
xstream.aliasField("BOOK_MAX_COUNT",SegTime.class,"bookMaxCount");
Resever resever = (Resever)xstream.fromXML(xml);

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5438629.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存