xStream 将xml转为javaBean

1、xml格式

  
   
        
		000000000183585090
		FIMS,LOBA
      
   
    000000000183585090
    PRMT_TMPL_CODE
    000001
    0053749
   

2、项目添加jar包


	com.thoughtworks.xstream
	xstream
	 1.4.2
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());
    }








你可能感兴趣的:(JAVA)