xml與object互轉方法

1.pom.xml引入:java

<dependency>
       <groupId>com.thoughtworks.xstream</groupId>
       <artifactId>xstream</artifactId>
       <version>1.4.1</version>
   </dependency>app

2.定義實體類:測試

package com.zdnst.common.infra.xml;

import com.thoughtworks.xstream.annotations.XStreamAlias;

/**
 * Created by yongqin.zhong
 * Date 2017-09-06.3:48 PM
 */
@XStreamAlias("xml")
public class WxXmlPayDto {


    @XStreamAlias("body")
    // 屬性註解,此price就是book的屬性,在XML中顯示爲:<book price="108">
    //@XStreamAsAttribute()
    private String body;


    @XStreamAlias("trade_type")
    private String tradeType;

    @XStreamAlias("mch_id")
    private String mchId;

    @XStreamAlias("sign_type")
    private String signType;

    @XStreamAlias("nonce_str")
    private String nonceStr;

    @XStreamAlias("detail")
    private String detail;

    @XStreamAlias("fee_type")
    private String feeType;

    @XStreamAlias("device_info")
    private String deviceInfo;

    @XStreamAlias("out_trade_no")
    private String outTradeNo;

    @XStreamAlias("total_fee")
    private Integer totalFee;

    @XStreamAlias("appid")
    private String appid;

    @XStreamAlias("notify_url")
    private String notifyUrl;

    @XStreamAlias("sign")
    private String sign;

    @XStreamAlias("spbill_create_ip")
    private String spbillCreateIp;

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getTradeType() {
        return tradeType;
    }

    public void setTradeType(String tradeType) {
        this.tradeType = tradeType;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getFeeType() {
        return feeType;
    }

    public void setFeeType(String feeType) {
        this.feeType = feeType;
    }

    public String getDeviceInfo() {
        return deviceInfo;
    }

    public void setDeviceInfo(String deviceInfo) {
        this.deviceInfo = deviceInfo;
    }

    public String getOutTradeNo() {
        return outTradeNo;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.outTradeNo = outTradeNo;
    }

    public Integer getTotalFee() {
        return totalFee;
    }

    public void setTotalFee(Integer totalFee) {
        this.totalFee = totalFee;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getSpbillCreateIp() {
        return spbillCreateIp;
    }

    public void setSpbillCreateIp(String spbillCreateIp) {
        this.spbillCreateIp = spbillCreateIp;
    }
}

三、定義轉換類this

package com.zdnst.common.infra.xml;
import java.awt.print.Book;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * Created by yongqin.zhong
 * Date 2017-09-06.3:43 PM
 */
public class XStreamHandle {
    private static final String xmlString =  "<xml><body>測試商家-商品類目</body><trade_type>NATIVE</trade_type><mch_id>11473623</mch_id><sign_type>HMAC-SHA256</sign_type><nonce_str>b1089cb0231011e7b7e1484520356fdc</nonce_str><detail /><fee_type>CNY</fee_type><device_info>WEB</device_info><out_trade_no>20161909105959000000111108</out_trade_no><total_fee>1</total_fee><appid>wxab8acb865bb1637e</appid><notify_url>http://test.letiantian.com/wxpay/notify</notify_url><sign>78F24E555374B988277D18633BF2D4CA23A6EAF06FEE0CF1E50EA4EADEEC41A3</sign><spbill_create_ip>123.12.12.123</spbill_create_ip></xml>";

    public static String toXml(Object obj) {
        XStream xstream = new XStream(new DomDriver("utf8"));
        xstream.processAnnotations(obj.getClass()); // 識別obj類中的註解
        /*
         // 以壓縮的方式輸出XML
         StringWriter sw = new StringWriter();
         xstream.marshal(obj, new CompactWriter(sw));
         return sw.toString();
         */
        // 以格式化的方式輸出XML
        return xstream.toXML(obj);
    }

    public static <T> T toBean(String xmlStr, Class<T> cls) {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(cls);
        @SuppressWarnings("unchecked")
        T t = (T) xstream.fromXML(xmlStr);
        return t;
    }

    public static void main(String[] args) {
        WxXmlPayDto book = toBean(xmlString, WxXmlPayDto.class);

        System.out.println("body=" + book.getBody() + "\tappid=" + book.getAppid()
                + "\ttotalPrice=" + book.getTotalFee());
        System.out.println(toXml(book));
    }
}
相關文章
相關標籤/搜索