java 将xml类型转换java对象

1.maven  坐标

        
        
            com.thoughtworks.xstream
            xstream
            1.4.10
        

2.实体类

package com.baiyou.lisinterface.kangshengda.lis.pojo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.io.Serializable;
import java.util.List;

/**
 * 响应的参数
 */
@XmlRootElement(name = "result")
public class XMLResult implements Serializable {
    @XmlElement(name = "resultdetail")
    private List resultDetailList;
    @XmlTransient
    public List getResultDetailList() {
        return resultDetailList;
    }

    public void setResultDetailList(List resultDetailList) {
        this.resultDetailList = resultDetailList;
    }
    @Override
    public String toString() {
        return "XMLResult{" +
                "resultDetailList=" + resultDetailList +
                '}';
    }
    /**
     * 子类
     */
    public static class ResultDetail{
        /**
         * 条码
         */
        @XmlElement(name = "barcode")
        private   String barcode;
        /**
         * 检验者
         */
        @XmlElement(name = "jyys")
        private   String jyys;
        /**
         * 审核者
         */
        @XmlElement(name = "shsj")
        private   String shsj;
        /**
         * 审核时间
         */
        @XmlElement(name = "shys")
        private   String shys;
        /**
         * 医嘱编号
         */
        @XmlElement(name = "desccode")
        private   String desccode;
        /**
         * 项目编号
         */
        @XmlElement(name = "xmdh")
        private   String xmdh;
        /**
         * 项目名称
         */
        @XmlElement(name = "xmmc")
        private   String xmmc;

        /**
         * 检验结果
         */
        @XmlElement(name = "jyjg")
        private   String jyjg;
        /**
         * 提示
         */
        @XmlElement(name = "gdbz")
        private   String gdbz;
        /**
         * 单位
         */
        @XmlElement(name = "xmdw")
        private   String xmdw;

        @XmlElement(name = "xx")
        private   String xx;

        @XmlElement(name = "sx")
        private   String sx;
        /**
         * 参考范围
         */
        @XmlElement(name = "xmckz")
        private   String xmckz;

        @XmlElement(name = "wsdw")
        private   String wsdw;

        @XmlTransient
        public String getBarcode() {
            return barcode;
        }

        public void setBarcode(String barcode) {
            this.barcode = barcode;
        }
        @XmlTransient
        public String getJyys() {
            return jyys;
        }

        public void setJyys(String jyys) {
            this.jyys = jyys;
        }
        @XmlTransient
        public String getShys() {
            return shys;
        }

        public void setShys(String shys) {
            this.shys = shys;
        }
        @XmlTransient
        public String getDesccode() {
            return desccode;
        }

        public void setDesccode(String desccode) {
            this.desccode = desccode;
        }
        @XmlTransient
        public String getXmdh() {
            return xmdh;
        }
        public void setXmdh(String xmdh) {
            this.xmdh = xmdh;
        }
        @XmlTransient
        public String getShsj() {
            return shsj;
        }

        public void setShsj(String shsj) {
            this.shsj = shsj;
        }
        @XmlTransient
        public String getXmmc() {
            return xmmc;
        }

        public void setXmmc(String xmmc) {
            this.xmmc = xmmc;
        }
        @XmlTransient
        public String getJyjg() {
            return jyjg;
        }

        public void setJyjg(String jyjg) {
            this.jyjg = jyjg;
        }
        @XmlTransient
        public String getGdbz() {
            return gdbz;
        }

        public void setGdbz(String gdbz) {
            this.gdbz = gdbz;
        }
        @XmlTransient
        public String getXmdw() {
            return xmdw;
        }

        public void setXmdw(String xmdw) {
            this.xmdw = xmdw;
        }
        @XmlTransient
        public String getXx() {
            return xx;
        }

        public void setXx(String xx) {
            this.xx = xx;
        }
        @XmlTransient
        public String getSx() {
            return sx;
        }

        public void setSx(String sx) {
            this.sx = sx;
        }
        @XmlTransient
        public String getXmckz() {
            return xmckz;
        }

        public void setXmckz(String xmckz) {
            this.xmckz = xmckz;
        }
        @XmlTransient
        public String getWsdw() {
            return wsdw;
        }

        public void setWsdw(String wsdw) {
            this.wsdw = wsdw;
        }

        @Override
        public String toString() {
            return "ResultDetail{" +
                    "barcode='" + barcode + '\'' +
                    ", jyys='" + jyys + '\'' +
                    ", shys='" + shys + '\'' +
                    ", desccode='" + desccode + '\'' +
                    ", xmdh='" + xmdh + '\'' +
                    ", xmmc='" + xmmc + '\'' +
                    ", jyjg='" + jyjg + '\'' +
                    ", gdbz='" + gdbz + '\'' +
                    ", xmdw='" + xmdw + '\'' +
                    ", xx='" + xx + '\'' +
                    ", sx='" + sx + '\'' +
                    ", xmckz='" + xmckz + '\'' +
                    ", wsdw='" + wsdw + '\'' +
                    '}';
        }
    }
}

3.逻辑处理

        //解析xml
        JAXBContext jaxbContext=null;
        Unmarshaller unmarshaller=null;
        XMLResult xmlResult=null;
        try {
             jaxbContext = JAXBContext.newInstance(XMLResult.class);
             unmarshaller = jaxbContext.createUnmarshaller();
             xmlResult= (XMLResult)unmarshaller.unmarshal(new StringReader(resultXml));
 }catch(){

}

XmlResult  实体类  

 resultxml  xml字符串

你可能感兴趣的:(Java)