Java xml格式字符串转java对象

使用XStream实现xml格式字符串转java对象。

  1. 首先引入相关jar包依赖
<dependency>
	<groupId>com.thoughtworks.xstream</groupId>
	<artifactId>xstream</artifactId>
	<version>1.4.10</version>
</dependency>
  1. 实例xml字符串
<ROOT>
	<result>000</result>
	<errorDesc></errorDesc>
	<SEG_TIME_LIST>
		<SEG_TIME>
			<SEG_ID>20</SEG_ID>
			<BOOK_DATE>2016-12-05</BOOK_DATE>
			<SEG_START_TIME>09:00</SEG_START_TIME>
			<SEG_END_TIME>14:00</SEG_END_TIME>
			<BOOKED_COUNT>0</BOOKED_COUNT>
			<BOOK_MAX_COUNT>0</BOOK_MAX_COUNT>
		</SEG_TIME>
		<SEG_TIME>
			<SEG_ID>21</SEG_ID>
			<BOOK_DATE>2016-12-05</BOOK_DATE>
			<SEG_START_TIME>14:00</SEG_START_TIME>
			<SEG_END_TIME>18:00</SEG_END_TIME>
			<BOOKED_COUNT>0</BOOKED_COUNT>
			<BOOK_MAX_COUNT>0</BOOK_MAX_COUNT>
		</SEG_TIME>
	</SEG_TIME_LIST>
</ROOT>
  1. 相关实体类
@XStreamAlias("ROOT")
public class Resever {
    private String result;
    private String errorDesc;
    @XStreamAlias("SEG_TIME_LIST")
    private List<SegTime> 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<SegTime> getSegTimeList() {
        return segTimeList;
    }

    public void setSegTimeList(List<SegTime> 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);

你可能感兴趣的:(java,xml,开发语言)