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>

  

posted @ 2020-09-24 16:11  尘世间迷茫的小书童  阅读(2139)  评论(0编辑  收藏  举报