zoukankan      html  css  js  c++  java
  • 通过JDOM实现XML与String的相互转换

    利用JDOM实现XML与String之间的相互转换:

    package com.util.xml;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.StringReader;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.jdom2.Document;
    import org.jdom2.Element;
    import org.jdom2.input.SAXBuilder;
    import org.jdom2.output.Format;
    import org.jdom2.output.XMLOutputter;
    
    /**
     * 通过JDOM解析XML文件 将字符串格式的XML文件转换为XML的document
     * 
     */
    
    // 将String转换成XML
    public class XMLUtils {
    
        public static Document str2XML(String string) throws Exception {
    
            SAXBuilder buider = new SAXBuilder();
    
            Document document = buider.build(new StringReader(string));
    
            return document;
    
        }
        
        //将XML转换成String输出
        public static String xml2Str(String file) throws Exception {
    
            SAXBuilder builder = new SAXBuilder();
            Document document = builder.build(new FileInputStream(new File(file)));
    
            Format format = Format.getCompactFormat();
            format.setEncoding("UTF-8");// 设置xml文件的字符为UTF-8,解决中文问题
            XMLOutputter xmlout = new XMLOutputter();
            
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            xmlout.output(document, bo);
            return bo.toString().trim();
            
        }
    
        // 将XML以键值对的形式输出,对于有两层以上的XML文件
        public static Map<String, String> paraseXML(String file) throws Exception {
    
            Map<String, String> map = new HashMap<String, String>();
    
            SAXBuilder builder = new SAXBuilder();
            Document document = builder.build(new File(file));
    
            Element root = document.getRootElement();
    
            List<Element> list = root.getChildren();
            for (Iterator<Element> iterator = list.iterator(); iterator.hasNext();) {
    
                Element firstChild = iterator.next();
                List<Element> secondElements = firstChild.getChildren();
    
                for (Iterator<Element> iterator2 = secondElements.iterator(); iterator2
                        .hasNext();) {
    
                    Element secondElement = iterator2.next();
                    map.put(secondElement.getName(), secondElement.getText());
                }
    
            }
    
            return map;
        }
        
        
        public static void main(String[] args) throws Exception {
    
            String str = "<students><Student><name>zhangsan</name><age>18</age><name>lisi</name><age>28</age></Student></students>";
            Document document = str2XML(str);
            Element root = document.getRootElement();
            System.out.println(root.getName());
            System.out.println(root.getChildText("name"));
    
            XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()
                    .setIndent("     "));
            FileOutputStream fos = new FileOutputStream(new File(
                    "date\string2xml.xml"));
            outputter.output(document, fos);
            fos.close();
            
            String xmlContent = xml2Str("date\string2xml.xml");
            System.out.println("XML的内容为: "+ "
    " + xmlContent);
            
            Map<String, String> xmlMap = new HashMap<String, String>();
            xmlMap = paraseXML("date\string2xml.xml");
            Set<String> keysSet = xmlMap.keySet();
            for(String key: keysSet){
                
                String value = xmlMap.get(key);
                System.out.println(key + " = " + value);
            }
            
    
        }
    
    }
  • 相关阅读:
    47. VUE-路由是什么?如何实现页面不请求刷新?
    21. SpringBoot 实现国际化 [i18n]
    20. SpringBoot 默认访问首页 以及 加载静态资源
    46. VUE 脚手架 —— vue ui 管理 以及 查看原始配置
    45. VUE ClI4 创建项目
    44.VUE2 项目目录结构解析 和 Runtime-Compiler和Runtime-only的区别
    2 . Mybatis — 增-删-改
    19. SpringBoot 扩展 SpringMVC功能、 接管、自定义SpringMVC
    17. Thymeleaf 模板 的 使用 和 语法
    16. SpringBoot 模板引擎
  • 原文地址:https://www.cnblogs.com/taoSir/p/5139283.html
Copyright © 2011-2022 走看看