• xml转对象,对象转xml工具类


     1 package com.dq.schooldomain.utils;
     2 
     3 
     4 
     5 import com.thoughtworks.xstream.XStream;
     6 import com.thoughtworks.xstream.io.xml.DomDriver;
     7 import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
     8 
     9 import java.io.InputStream;
    10 /**
    11  * @Author Allen.Lv
    12  * @Description //TODO
    13  * @Date 11:51 2019/3/1
    14  * @Desc: Coding Happy!
    15  **/
    16 public class XmlUtil {
    17 
    18     /**
    19      * 
    20      * @param inputXml
    21      * @param type
    22      * @return
    23      * @throws Exception
    24      */
    25     public static Object xml2Object(String inputXml, Class<?> type) throws Exception {
    26         if (null == inputXml || "".equals(inputXml)) {
    27             return null;
    28         }
    29         XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    30         xstream.alias("xml", type);
    31         return xstream.fromXML(inputXml);
    32     }
    33 
    34     /**
    35      *
    36      * @param inputStream
    37      * @param type
    38      * @return
    39      * @throws Exception
    40      */
    41     public static Object xml2Object(InputStream inputStream, Class<?> type) throws Exception {
    42         if (null == inputStream) {
    43             return null;
    44         }
    45         XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    46         xstream.alias("xml", type);
    47         return xstream.fromXML(inputStream, type);
    48     }
    49 
    50     /**
    51      *
    52      * @param ro
    53      * @param types
    54      * @return
    55      * @throws Exception
    56      */
    57     public static String object2Xml(Object ro, Class<?> types) throws Exception {
    58         if (null == ro) {
    59             return null;
    60         }
    61         XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    62         xstream.alias("xml", types);
    63         return xstream.toXML(ro);
    64     }
    65 
    66 }
  • 相关阅读:
    解决mac osx下pip安装ipython权限的问题
    [转载][翻译]Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[1]
    PhantomJS 基础及示例 (转)
    Go -- 别人的博客
    [转]Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误-高级
    Go -- etcd详解(转)
    对require.js 的使用进行总结
    gatsbyjs 使用
    gatsbyjs 了解
    JAMstack 最佳实践
  • 原文地址:https://www.cnblogs.com/javallh/p/10477185.html
Copyright © 2020-2023  润新知