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());
    }

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念