xml解析工具类

java 复制代码
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;
    }
}
相关推荐
&白帝&33 分钟前
JAVA JDK7时间相关类
java·开发语言·python
2301_8187320636 分钟前
用layui表单,前端页面的样式正常显示,但是表格内无数据显示(数据库连接和获取数据无问题)——已经解决
java·前端·javascript·前端框架·layui·intellij idea
狄加山67543 分钟前
系统编程(线程互斥)
java·开发语言
星迹日44 分钟前
数据结构:二叉树—面试题(二)
java·数据结构·笔记·二叉树·面试题
组合缺一1 小时前
solon-flow 你好世界!
java·solon·oneflow
HHhha.1 小时前
JVM深入学习(二)
java·jvm
叩叮ING1 小时前
正则表达式中常见的贪婪词
java·服务器·正则表达式
组合缺一2 小时前
Solon Cloud Gateway 开发:熟悉 Completable 响应式接口
java·gateway·reactor·solon
组合缺一2 小时前
Solon Cloud Gateway 开发:Route 的配置与注册方式
java·gateway·reactor·solon
栗豆包2 小时前
w179基于Java Web的流浪宠物管理系统的设计与实现
java·开发语言·spring boot·后端·spring·宠物