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

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...