• java对象与xml相互转换工具类


    public class XmlHelper {
        /**
         * Object转XML
         *
         * @param object
         * @return
         * @throws Exception
         */
        public static String ObjectToXml(Object object) throws Exception {
            JAXBContext context = JAXBContext.newInstance(object.getClass());    // 获取上下文对象
            Marshaller marshaller = context.createMarshaller(); // 根据上下文获取marshaller对象
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "GB2312");  // 设置编码字符集
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化XML输出,有分行和缩进
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            marshaller.marshal(object, baos);
            String xmlObj = new String(baos.toByteArray());         // 生成XML字符串
            return xmlObj.trim();
        }
    
        /**
         * Object转XML
         *
         * @param object
         * @return
         * @throws Exception
         */
        public static String ObjectToXmlUtf8(Object object) throws Exception {
            JAXBContext context = JAXBContext.newInstance(object.getClass());    // 获取上下文对象
            Marshaller marshaller = context.createMarshaller(); // 根据上下文获取marshaller对象
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");  // 设置编码字符集
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化XML输出,有分行和缩进
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            marshaller.marshal(object, baos);
            String xmlObj = new String(baos.toByteArray());         // 生成XML字符串
            return xmlObj.trim();
        }
    
        /**
         * XML转Object
         *
         * @param xmlStr
         * @param classz
         * @return
         */
        public static Object XmlToObject(String xmlStr, Class classz) {
            try {
                JAXBContext context = JAXBContext.newInstance(classz);
                Unmarshaller unmarshaller = context.createUnmarshaller();
                Object object = unmarshaller.unmarshal(new StringReader(xmlStr));
                return object;
            } catch (JAXBException ex) {
                ex.printStackTrace();
                return null;
            }
        }
    
    
        /**
         * xml格式字符串获取所有子节点存入list,每个List分两个部分:name和value,中间用“:”隔开
         *
         * @param srcXml
         * @return
         */
        public static List<String> getListXML(String srcXml) {
            //存储xml元素信息的容器
            List<Leaf> elemList = new ArrayList<Leaf>();
            List<String> list = new ArrayList<String>();
            Document srcdoc = null;
            try {
                srcdoc = DocumentHelper.parseText(srcXml);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            Element elem = srcdoc.getRootElement();
            getElementList(elem, elemList);
            for (Iterator<Leaf> it = elemList.iterator(); it.hasNext(); ) {
                Leaf leaf = it.next();
                list.add(leaf.getName() + ":" + leaf.getValue());
            }
            return list;
        }
    
        /**
         * 递归遍历方法
         *
         * @param element
         */
        public static void getElementList(Element element, List<Leaf> elemList) {
            List elements = element.elements();
            if (elements.size() == 0) {
                //没有子元素
                //String xpath = element.getPath();
                String xpath = element.getName();
                String value = element.getTextTrim();
                Leaf leaf = new Leaf(xpath, value);
                elemList.add(leaf);
            } else {
                //有子元素
                for (Iterator it = elements.iterator(); it.hasNext(); ) {
                    Element elem = (Element) it.next();
                    //递归遍历
                    getElementList(elem, elemList);
                }
            }
        }
    
        static class Leaf {
            private String name;
            private String value;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getValue() {
                return value;
            }
    
            public void setValue(String value) {
                this.value = value;
            }
    
            public Leaf(String name, String value) {
                this.name = name;
                this.value = value;
            }
        }
    }
    
  • 相关阅读:
    Windows10系统下在Docker中部署静态网站
    NET接入Mesher--------解决微服务什么时候能支持.net 的应用开发的问题
    Windows系统下 ASP.NET Core 的 Docker 映像创建
    Windows平台部署 Asp.Net Core 3.1.0,将 ASP.NET Core 应用发布到 IIS ,使用 IIS 在 Windows 上托管 ASP.NET Core
    如何使用Activator.CreateInstance创建一个列表<T>,其中T在运行时是未知的?
    在 Visual Studio 中安装 FxCop 分析器
    .Net Core3.1下使用Swagger搭建web api项目
    Vuex访问状态对象的方法
    Vuex最基本样例
    搜索框 展示相关搜索内容列表,及延迟改进(仿百度)
  • 原文地址:https://www.cnblogs.com/black-spike/p/10174630.html
Copyright © 2020-2023  润新知