zoukankan      html  css  js  c++  java
  • 利用dom4j读写XML

    public static  HashMap<String, String> ReadConfig() {  
            HashMap<String, String> map=new HashMap<>();
            try {
                SAXReader reader = new SAXReader();  
                Document document= reader.read(new File("config.xml"));
                Element node = document.getRootElement();
                 @SuppressWarnings("unchecked")
                List<Element> listElement = node.elements();
                for (final Element e : listElement) {
                    map.put(e.getName(), e.getText());
                } 
            } catch (Exception e) {
                e.printStackTrace();
                logger.info("读取配置文件错误"+e.getMessage());
            }  
            return map;
        }
        
        public static void WriterConfig(HashMap<String, String> map) {  
            try {
                SAXReader reader = new SAXReader();  
                Document document= reader.read(new File("config.xml"));
                Element node = document.getRootElement(); 
                for (Entry<String, String> entry : map.entrySet()) {
                    node.addElement(entry.getKey()).setText(entry.getValue());
                }
                OutputFormat format = OutputFormat.createPrettyPrint();  
                format.setEncoding("UTF-8");  
                XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("config.xml")), "UTF-8"), format);
                writer.write(document);  
                writer.flush();  
                writer.close();  
            } catch (Exception e) {
                logger.info("写入配置文件出错"+e.getMessage());
                e.printStackTrace();
            } 
        } 
  • 相关阅读:
    十、 Spring Boot Shiro 权限管理
    十六、Spring Boot 部署与服务配置
    十五、Spring Boot 环境变量读取 和 属性对象的绑定
    三、spring cloud 服务提供与调用
    CSS 表格实例
    CSS 列表实例
    CSS 内边距 (padding) 实例
    CSS 外边距
    CSS 边框(border)实例
    CSS 字体(font)实例
  • 原文地址:https://www.cnblogs.com/zhangjinru123/p/7988841.html
Copyright © 2011-2022 走看看