欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java XML文档的操作

程序员文章站 2022-05-30 10:37:10
...

参考链接:
https://www.cnblogs.com/ssjifm/p/7373089.html

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yss.henghe.iris.models.YarnSchedulerModels;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLResult;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class XMLUtil {


    /**
     * 将xml配置文件转化成map集合
     * @param xml
     * @return
     * @throws DocumentException
     */
    public static Map<String, String> parsehadoopConfig(String xml) throws DocumentException {

        Map<String, String> config = Maps.newHashMap();
        Document document = DocumentHelper.parseText(xml);
        Element root = document.getRootElement();

        for (Iterator<Element> it = root.elementIterator(); it.hasNext(); ) {
            Element element = it.next();
            String key = element.element("name").getStringValue();
            String value = element.element("value").getStringValue();
            config.put(key, value);
        }
        return config;
    }


    /**
     * <p>删除xml中指定的一条配置</p>
     * @param xml
     * @param node
     * @return
     */
    public static String deleteNode(String xml, String node) {
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new ByteArrayInputStream(xml.getBytes()));
            Element rootElement = doc.getRootElement();
            Iterator<Element> iterator = rootElement.elementIterator("property");
            while (iterator.hasNext()) {
                Element element = iterator.next();
                if (node.equals(element.element("name").getStringValue())) {
                    rootElement.remove(element);
                    XMLResult xmlResult = new XMLResult();
                    OutputStream os = new ByteArrayOutputStream();
                    xmlResult.setXMLWriter(new XMLWriter(os, OutputFormat.createPrettyPrint()));
                    xmlResult.getXMLWriter().write(doc);
                    return os.toString();
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return xml;
    }

    /**
     * <p>xml中增加一条配置</p>
     * @param xml
     * @param
     * @return
     */
    public static String addNode(String xml, String name, String value) {
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new ByteArrayInputStream(xml.getBytes()));
            Element rootElement = doc.getRootElement();

            Element menuElement = rootElement.addElement("property");
            Element engNameElement = menuElement.addElement("name");
            engNameElement.setText(name);
            Element chiNameElement = menuElement.addElement("value");
            chiNameElement.setText(value);

            XMLResult xmlResult = new XMLResult();
            OutputStream os = new ByteArrayOutputStream();
            xmlResult.setXMLWriter(new XMLWriter(os, OutputFormat.createPrettyPrint()));
            xmlResult.getXMLWriter().write(doc);
            return os.toString();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return xml;
    }

    /**
     * 修改xml中的某一条配置文件
     * @param xml
     * @param node
     * @return
     */
    public static String updateNode(String xml, String node, String nodeValue) {
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new ByteArrayInputStream(xml.getBytes()));
            Element rootElement = doc.getRootElement();
            Iterator<Element> iterator = rootElement.elementIterator("property");
            while (iterator.hasNext()) {
                Element element = iterator.next();
                if (node.equals(element.element("name").getStringValue())) {
                    element.element("value").setText(nodeValue);
                    //rootElement.remove(element);
                    XMLResult xmlResult = new XMLResult();
                    OutputStream os = new ByteArrayOutputStream();
                    xmlResult.setXMLWriter(new XMLWriter(os, OutputFormat.createPrettyPrint()));
                    xmlResult.getXMLWriter().write(doc);
                    return os.toString();
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return xml;
    }
}