Xml2Object

         <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.4</version>
        </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<com.yundaex.wms.core.store.po.RivQuant>
  <qtId>-1</qtId>
  <qtAllocQty>100</qtAllocQty>
  <qtBillId>25</qtBillId>
  <qtBillType>1</qtBillType>
  <qtContainerId>11</qtContainerId>
  <qtContainerTypeId>19</qtContainerTypeId>
  <qtLocationId>10</qtLocationId>
  <qtLotItem1>22</qtLotItem1>
  <qtLotItem2>12</qtLotItem2>
  <qtLotItem3>12</qtLotItem3>
  <qtLotNo></qtLotNo>
  <qtLotString1></qtLotString1>
  <qtLotString2></qtLotString2>
  <qtLotString3></qtLotString3>
  <qtLotString4></qtLotString4>
  <qtLotString5></qtLotString5>
  <qtLpn></qtLpn>
  <qtMaterialId>17</qtMaterialId>
  <qtMaterialStatusId>8</qtMaterialStatusId>
  <qtOnhandQty>100</qtOnhandQty>
  <qtOnrecvQty>100</qtOnrecvQty>
  <qtOrgId>49</qtOrgId>
  <qtOwnerId>19</qtOwnerId>
  <qtTrolleyCellNo></qtTrolleyCellNo>
  <qtType>location</qtType>
  <qtVoucherNo>123456</qtVoucherNo>
  <versionNumber>0</versionNumber>
</com.yundaex.wms.core.store.po.RivQuant>
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.thoughtworks.xstream.XStream;


public class Xml2Object {

    private static XStream xstream;

    static {
        xstream = new XStream();
    }

    public static Object convert2Object(String xmlName) {
        String configPath = "classpath*:data/"+xmlName;
        //String path = Xml2Object.class.getResource("/").getPath() + xmlName;
        InputStream inputStream = null;
        try {
             final int index = StringUtils.lastIndexOf(configPath, ":") == -1 ? 0 : StringUtils.lastIndexOf(configPath, ":");
             final Resource resource = new ClassPathResource(StringUtils.substring(configPath, index+1, configPath.length()));
             inputStream = resource.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Object obj = xstream.fromXML(inputStream);
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    public static void main(String[] args) {
        convert2Object("owner.xml");
    }
}

 

转载于:https://www.cnblogs.com/tonggc1668/p/7017535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值