iqq.app.util.XmlUtils.java Source code

Java tutorial

Introduction

Here is the source code for iqq.app.util.XmlUtils.java

Source

package iqq.app.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * XML? ?//?/
 *
 * Project  : iqq
 * Author   :  < 6208317@qq.com >
 * Created  : 14-4-16
 * License  : Apache License 2.0
 */
public class XmlUtils {
    private static final Logger LOG = LoggerFactory.getLogger(XmlUtils.class);
    private static Map<String, Document> xmlCache = new HashMap<String, Document>();

    /**
     * ?XML
     *
     * @param filename
     * @param key
     * @return
     * @throws DocumentException
     */
    public static String getNodeText(String filename, String key) throws DocumentException {
        return getNode(filename, key).getText();
    }

    /**
     * ?XML
     *
     * @param filename
     * @param key
     * @param Value
     */
    public static void setNodeText(String filename, String key, String value)
            throws DocumentException, IOException {
        Document document = readXml(filename);
        Element rootElement = document.getRootElement();
        Node node = rootElement.selectSingleNode(key);
        if (node == null) {
            node = rootElement.addElement(key);
        }
        node.setText(value);
        writeXml(filename, document);
    }

    /**
     * ?XML
     *
     * @param filename
     * @param key
     * @return
     * @throws DocumentException
     */
    public static Node getNode(String filename, String key) throws DocumentException {
        return getRootElement(filename).selectSingleNode(key);
    }

    /**
     * ?XML
     *
     * @param filename
     * @return
     * @throws DocumentException
     */
    public static Element getRootElement(String filename) throws DocumentException {
        return readXml(filename).getRootElement();
    }

    /**
     * XML
     *
     * @param filename
     * @return
     * @throws DocumentException
     */
    public static Document readXml(String filename) throws DocumentException {
        if (xmlCache.get(filename) == null) {
            LOG.debug("XML: " + filename);
            // SAXReader?
            SAXReader saxReader = new SAXReader();
            // ?xml
            Document document = saxReader.read(new File(filename));
            // 
            xmlCache.put(filename, document);
            return document;
        }
        return xmlCache.get(filename);
    }

    /**
     * XML
     *
     * @param document
     * @param xmlFile
     * @throws IOException
     */
    public static void writeXml(String filename, Document document) throws IOException {
        LOG.debug("XML: " + filename);
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();// XML?
        outputFormat.setEncoding("UTF-8");// XML?
        outputFormat.setIndent(true);// ?
        outputFormat.setIndent("   ");// TAB?
        outputFormat.setNewlines(true);// ??
        synchronized (document) {
            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filename), outputFormat);
            xmlWriter.write(document);
            xmlWriter.close();
        }
    }
}