xStream 将xml转为javaBean

前端之家收集整理的这篇文章主要介绍了xStream 将xml转为javaBean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、xml格式

<?xml version="1.0" encoding="utf-8"?>  
<MbfBody>   
    <tZmdifs319>    
		<cmmdtyCode>000000000183585090</cmmdtyCode>
		<DISTRIBUTE_SYS>FIMS,LOBA</DISTRIBUTE_SYS>
   </tZmdifs319>   
   <tZmdifs325>
    <cmmdtyCode>000000000183585090</cmmdtyCode>
    <businessField>PRMT_TMPL_CODE</businessField>
    <keywordCode>000001</keywordCode>
    <businessFieldDesc>0053749</businessFieldDesc>
   </tZmdifs325>
</MbfBody>

2、项目添加jar包

<dependency>
	<groupId>com.thoughtworks.xstream</groupId>
	<artifactId>xstream</artifactId>
	 <version>1.4.2</version>
</dependency>
3、根据xml层级写dto
import com.thoughtworks.xstream.annotations.XStreamAlias;

public class DistributeSysInfo {
    //与xml里字段不一致时加注解
    @XStreamAlias("DISTRIBUTE_SYS")
    private String distributeSys; 

    private String cmmdtyCode;

    /**
     * 获取distributeSys
     * 
     * @return distributeSys distributeSys
     */
    public String getDistributeSys() {
        return distributeSys;
    }

    /**
     * 设置distributeSys
     * 
     * @param distributeSys distributeSys
     */
    public void setDistributeSys(String distributeSys) {
        this.distributeSys = distributeSys;
    }

    /**
     * 获取cmmdtyCode
     * 
     * @return cmmdtyCode cmmdtyCode
     */
    public String getCmmdtyCode() {
        return cmmdtyCode;
    }

    /**
     * 设置cmmdtyCode
     * 
     * @param cmmdtyCode cmmdtyCode
     */
    public void setCmmdtyCode(String cmmdtyCode) {
        this.cmmdtyCode = cmmdtyCode;
    }

}


public class CmmdtyOtherInfo {
    private String cmmdtyCode;
    private String businessField;
    private String keywordCode;
    private String businessFieldDesc;

    /**
     * 获取cmmdtyCode
     * 
     * @return cmmdtyCode cmmdtyCode
     */
    public String getCmmdtyCode() {
        return cmmdtyCode;
    }

    /**
     * 设置cmmdtyCode
     * 
     * @param cmmdtyCode cmmdtyCode
     */
    public void setCmmdtyCode(String cmmdtyCode) {
        this.cmmdtyCode = cmmdtyCode;
    }

    /**
     * 获取businessField
     * 
     * @return businessField businessField
     */
    public String getBusinessField() {
        return businessField;
    }

    /**
     * 设置businessField
     * 
     * @param businessField businessField
     */
    public void setBusinessField(String businessField) {
        this.businessField = businessField;
    }

    /**
     * 获取keywordCode
     * 
     * @return keywordCode keywordCode
     */
    public String getKeywordCode() {
        return keywordCode;
    }

    /**
     * 设置keywordCode
     * 
     * @param keywordCode keywordCode
     */
    public void setKeywordCode(String keywordCode) {
        this.keywordCode = keywordCode;
    }

    /**
     * 获取businessFieldDesc
     * 
     * @return businessFieldDesc businessFieldDesc
     */
    public String getBusinessFieldDesc() {
        return businessFieldDesc;
    }

    /**
     * 设置businessFieldDesc
     * 
     * @param businessFieldDesc businessFieldDesc
     */
    public void setBusinessFieldDesc(String businessFieldDesc) {
        this.businessFieldDesc = businessFieldDesc;
    }

}
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("MbfBody")//节点注解
public class DistributeArticleBDateIn {
    @XStreamAlias("tZmdifs319")//节点注解,子节点对应的javaBean与节点名一致就不用注解
    private DistributeSysInfo       distributeSysInfo;
    @XStreamAlias("tZmdifs325")//节点注解,子节点对应的javaBean与节点名一致就不用注解
    private CmmdtyOtherInfo         cmmdtyOtherInfo;

    /**
     * 获取distributeSysInfo
     * 
     * @return distributeSysInfo distributeSysInfo
     */
    public DistributeSysInfo getDistributeSysInfo() {
        return distributeSysInfo;
    }

    /**
     * 设置distributeSysInfo
     * 
     * @param distributeSysInfo distributeSysInfo
     */
    public void setDistributeSysInfo(DistributeSysInfo distributeSysInfo) {
        this.distributeSysInfo = distributeSysInfo;
    }   
    /**
     * 获取cmmdtyOtherInfo
     * 
     * @return cmmdtyOtherInfo cmmdtyOtherInfo
     */
    public CmmdtyOtherInfo getCmmdtyOtherInfo() {
        return cmmdtyOtherInfo;
    }

    /**
     * 设置cmmdtyOtherInfo
     * 
     * @param cmmdtyOtherInfo cmmdtyOtherInfo
     */
    public void setCmmdtyOtherInfo(CmmdtyOtherInfo cmmdtyOtherInfo) {
        this.cmmdtyOtherInfo = cmmdtyOtherInfo;
    }

}


4、转化
 public static void main(String[] args) {
        XStream x = new XStream();
        x.processAnnotations(DistributeArticleBDateIn.class);
        DistributeArticleBDateIn out = (DistributeArticleBDateIn) x.fromXML(new File("E:\\product.xml"));
        //System.out.println(JacksonMapper.beanToJson(out));
        System.out.println(out.getDistributeSysInfo().toString());
    }

猜你在找的XML相关文章