• xml解析工具类


    package com.yannis.utils;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.annotation.XmlAnyElement;
    import javax.xml.namespace.QName;
    import java.io.StringReader;
    import java.io.StringWriter;
    import java.util.Collection;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ConcurrentMap;
    
    public class JaxbMapper {
    
        private static ConcurrentMap<Class, JAXBContext> jaxbContexts = new ConcurrentHashMap<Class, JAXBContext>();
    
        public static String toXml(Object root) {
            return toXml(root, root.getClass(), null);
        }
    
        public static String toXml(Object root, String encoding) {
            return toXml(root, root.getClass(), encoding);
        }
    
    //    @SneakyThrows(JAXBException.class)
        public static String toXml(Object root, Class clazz, String encoding){
            String s = "";
            try {
                StringWriter writer = new StringWriter();
                createMarshaller(clazz, encoding).marshal(root, writer);
                s = writer.toString();
            } catch (Exception e) {
    
            }
         return s;
        }
    
        public static String toXml(Collection<?> root, String rootName, Class clazz) {
            return toXml(root, rootName, clazz, null);
        }
    
        public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
            String string = "";
            try {
                CollectionWrapper wrapper = new CollectionWrapper();
                wrapper.collection = root;
    
                JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                        CollectionWrapper.class, wrapper);
    
                StringWriter writer = new StringWriter();
                createMarshaller(clazz, encoding).marshal(wrapperElement, writer);
    
                string =  writer.toString();
            } catch (Exception e) {
    
            }
          return string;
        }
    
        @SuppressWarnings("unchecked")
        public static <T> T fromXml(String xml, Class<T> clazz) throws Exception{
            StringReader reader = new StringReader(xml);
            return (T) createUnmarshaller(clazz).unmarshal(reader);
        }
    
        /**
         * 创建Marshaller并设定encoding(可为null).
         * 线程不安全,需要每次创建或pooling。
         */
        public static Marshaller createMarshaller(Class clazz, String encoding) throws Exception{
            JAXBContext jaxbContext = getJaxbContext(clazz);
    
            Marshaller marshaller = jaxbContext.createMarshaller();
    
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    
            if (encoding != null) {
                marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
            }
    
            return marshaller;
        }
    
        /**
         * 创建UnMarshaller.
         * 线程不安全,需要每次创建或pooling。
         */
        public static Unmarshaller createUnmarshaller(Class clazz) throws Exception{
            JAXBContext jaxbContext = getJaxbContext(clazz);
            return jaxbContext.createUnmarshaller();
        }
    
        protected static JAXBContext getJaxbContext(Class clazz) throws Exception{
    //		Assert.notNull(clazz, "'clazz' must not be null");
            JAXBContext jaxbContext = jaxbContexts.get(clazz);
            if (jaxbContext == null) {
                jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
                jaxbContexts.putIfAbsent(clazz, jaxbContext);
            }
            return jaxbContext;
        }
    
        /**
         * 封装Root Element 是 Collection的情况.
         */
        public static class CollectionWrapper {
    
            @XmlAnyElement
            protected Collection<?> collection;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115

    示例

    //request对象需要符合JAXB规范。
    JaxbMapper.toXml(request);
    
    • 1
    • 2
  • 相关阅读:
    Java8 新特性之Stream(三)-- Stream的终结操作
    第60节——使用redux-toolkit实战一个商品列表的增删查改
    “Spark+Hive”在DPU环境下的性能测评 | OLAP数据库引擎选型白皮书(24版)DPU部分节选
    mmpose关键点(四):优化关键点模型(原理与代码讲解,持续更新)
    解决类重复的问题
    iNFTnews | 元宇宙为企业发展带来新思路
    协程概述讲解
    Ubuntu22.04下安装Spark2.4.0(Local模式)
    python自动化标注工具+自定义目标P图替换+深度学习大模型(代码+教程+告别手动标注)
    tf.Variable
  • 原文地址:https://blog.csdn.net/ityqing/article/details/132763472