• Java 操作resources下xml文件


    框架:SpringBoot 2.1.15.RELEASE

    JDK:1.8

    依赖:

            <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
            <dependency>
                <groupId>org.dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>2.1.1</version>
            </dependency> 
    

    工具类:

    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 功能描述: xml工具类
     * @Author: XinHai.Ma
     * @Date: 2020/9/24 15:50
     */
    public class XmlUtils {
    
        private XmlUtils() {}
    
        private static final String path = "src/main/resources/";
    
        /**
         * 功能描述: 读取xml文件
         *
         * @Param: [xmlPath]
         * @Return: java.util.List<www.maxinhai.com.calculate.Formula>
         * @Author: XinHai.Ma
         * @Date: 2020/9/24 15:36
         */
        public static List<Formula> ReadXml(String xmlPath) {
            SAXReader reader = new SAXReader();
            Document doc = null;
            try {
                doc = reader.read(new File(path + xmlPath));
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            Element root = doc.getRootElement();
            System.out.println("获取了根元素:" + root.getName());
            List<Element> list = root.elements();
            List<Formula> empList = new ArrayList<Formula>();
            for (Element element : list) {
                String name = element.element("key").getText();
                String value = element.element("value").getText();
                Formula formula = new Formula();
                formula.setKey(name);
                formula.setFormula(value);
                empList.add(formula);
            }
            return empList;
        }
    
        /**
         * 功能描述: 向xml写入数据
         * @Param: [formulas]
         * @Return: void
         * @Author: XinHai.Ma
         * @Date: 2020/9/24 15:50
         */
        public static void writeXml(List<Formula> formulas) {
            Document doc = DocumentHelper.createDocument();
            Element root = doc.addElement("formulaList");
            for (Formula formula : formulas) {
                Element empElement = root.addElement("formula");
                empElement.addElement("key").addText(formula.getKey());
                empElement.addElement("value").addText(formula.getFormula());
                empElement.addAttribute("id", formula.getId() + "");
            }
            try {
                XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
                FileOutputStream fos = new FileOutputStream(path + "formula.xml");
                writer.setOutputStream(fos);
                writer.write(doc);
                System.out.println("写出完毕!");
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    

    xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <formulaList>
      <formula id="1">
        <key>1</key>
        <value>1*2+4*3</value>
      </formula>
      <formula id="2">
        <key>2</key>
        <value>1+2+4*3</value>
      </formula>
    </formulaList>
    

      

  • 相关阅读:
    linux 图形界面形式和命令行形式
    linux 配置 jdk7
    StackOverflowError
    一次失败的svchost hacking
    VB调用API的学习
    植物打僵尸休闲花园外挂代码
    中文日文翻译工具
    vb跨域访问,获得特定的frame进行处理
    HTML对象库简介(Microsoft HTML Object Library > mshtml.tlb)
    用正则分析一段vb代码含有哪些过程或者函数
  • 原文地址:https://www.cnblogs.com/mxh-java/p/13724606.html
Copyright © 2020-2023  润新知