zoukankan      html  css  js  c++  java
  • 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");
        }
    }
  • 相关阅读:
    Java中String类两种实例化的区别(转)
    Linux内核如何装载和启动一个可执行程序(转)
    CentOS 7 下安装jdk1.8(转)
    MySQL中死锁(转)
    CTF取证方法大汇总,建议收藏!
    实战经验|大神战队都在i春秋教你打CTF
    CTF丨从零开始搭建WEB Docker靶场
    业务逻辑漏洞探索之敏感信息泄露
    实战经验丨业务逻辑漏洞探索之活动类漏洞
    漏洞经验分享丨Java审计之XXE(下)
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/7017535.html
Copyright © 2011-2022 走看看