码迷,mamicode.com
首页 > 其他好文 > 详细

XStream进行xml和bean互转

时间:2018-11-06 19:33:40      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:version   from   pre   pen   需要   public   ret   XML   set   

加入pom

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.3</version>
</dependency>
  1. 最简单的使用方式
XStream xStream = new XStream();
//声明XStream注解来源
xStream.processAnnotations(FuiouResponse.class);
//xml 转bean
Object o = xStream.fromXML(xmlStr);
//bean 转xml
xStream.toXML(response)
  1. 需要指明注解
/**
 * @author laoliangliang
 * @date 2018/11/6 17:00
 */
@XStreamAlias("ap")
public class FuiouResponse {

    @XStreamAlias("signature")
    private String signature;
    @XStreamAlias("plain")
    private Plain plain;

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getSignature() {
        return signature;
    }

    public Plain getPlain() {
        return plain;
    }

    public void setPlain(Plain plain) {
        this.plain = plain;
    }

    public class Plain {
        @XStreamAlias("resp_code")
        private String respCode;

        @XStreamAlias("resp_desc")
        private String respDesc;

        public String getRespCode() {
            return respCode;
        }

        public void setRespCode(String respCode) {
            this.respCode = respCode;
        }

        public String getRespDesc() {
            return respDesc;
        }

        public void setRespDesc(String respDesc) {
            this.respDesc = respDesc;
        }
    }
}

和jpa类似,取个别名

XStream进行xml和bean互转

标签:version   from   pre   pen   需要   public   ret   XML   set   

原文地址:https://www.cnblogs.com/sky-chen/p/9916957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!