zoukankan      html  css  js  c++  java
  • xstream实现xml字符串与对象直接的转换

    /**
     * TODO 使用xStream 实现xml字符和对象之间的转换
     * <p>
     * <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
     * <!-- xml字符串,对象之间互转 -->
     * <dependency>
     * <groupId>com.thoughtworks.xstream</groupId>
     * <artifactId>xstream</artifactId>
     * <version>${xstream.version}</version>
     * </dependency>
     *
     * @author: geekswg@qq.com
     * @since: 2020/8/1 10:31
     */
    public class XmlUtils {
    
        private static final XStream xStreamHelper = new XStream(new StaxDriver());
        // 初始化配置
        static {
            XStream.setupDefaultSecurity(xStreamHelper);
            xStreamHelper.allowTypesByWildcard(new String[]{"org.demo.springcloud.**"});
            xStreamHelper.ignoreUnknownElements();//忽略未知节点
            xStreamHelper.autodetectAnnotations(true);
        }
    
        public static <T> T xmlToBean(String xmlStr, Class<?> clazz) {
            xStreamHelper.ignoreUnknownElements();//忽略未知节点
            xStreamHelper.autodetectAnnotations(true);
            xStreamHelper.processAnnotations(clazz);
            return (T) xStreamHelper.fromXML(xmlStr);
        }
    
        public static <T> T xmlToBean(File xmlFile, Class<?> clazz) {
            xStreamHelper.processAnnotations(clazz);
            return (T) xStreamHelper.fromXML(xmlFile);
        }
    
        public static <T> String beanToXmlStr(T t) {
            return xStreamHelper.toXML(t);
        }
    
        public static <T> boolean beanToXml(T t, String filePath){
            boolean flag = true;
            try {
                FileWriter fileWriter = new FileWriter(new File(filePath));
                fileWriter.write(xStreamHelper.toXML(t));
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                flag = false;
                e.printStackTrace();
            }finally {
                return flag;
            }
        }
    
        public static void main(String[] args) {
            BsBankInfo bsBankInfo = new BsBankInfo();
            bsBankInfo.setBankName("中国建设银行");
            bsBankInfo.setBankNo("b10001");
    
            String xmlStr = beanToXmlStr(bsBankInfo);
            System.out.println("===>" + xmlStr);
            System.out.println(xmlToBean(xmlStr, BsBankInfo.class).toString());
            System.out.println(beanToXml(bsBankInfo, "d:/t.xml"));
        }
    }
  • 相关阅读:
    [LeetCode] Output Contest Matches 输出比赛匹配对
    [LeetCode] 527. Word Abbreviation 单词缩写
    [LeetCode] Permutation in String 字符串中的全排列
    [LeetCode] 560. Subarray Sum Equals K 子数组和为K
    [LeetCode] Reshape the Matrix 重塑矩阵
    [LeetCode] 536. Construct Binary Tree from String 从字符串创建二叉树
    [LeetCode] IPO 上市
    [LeetCode] Binary Tree Tilt 二叉树的坡度
    [LeetCode] Array Partition I 数组分割之一
    [LeetCode] Zuma Game 祖玛游戏
  • 原文地址:https://www.cnblogs.com/geekswg/p/13413848.html
Copyright © 2011-2022 走看看