XMLUtil

import org.apache.commons.lang3.StringUtils;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class XMLUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(XMLUtil.class);

    /**
     * 默认编码
     */
    private static final String DEFAULT_ENCODING = "UTF-8";

    /**
     * 创建一个新的XML文档
     */
    public static Document createDocument() {
        return DocumentHelper.createDocument();
    }

    /**
     * 获取xml 文档
     *
     * @param inputXml
     * @return
     */
    public static Document getDocument(InputStream inputXml) {
        Document document = null;
        SAXReader saxReader = null;
        try {
            saxReader = new SAXReader();
            document = saxReader.read(inputXml);
        } catch (DocumentException e) {
            LOGGER.error(e.getMessage(), e);
        }
        return document;
    }

    /**
     * 添加元素
     *
     * @param document
     * @param name
     * @return
     */
    public static Element addElement(Document document, String name) {
        return document.addElement(name);
    }

    /**
     * 给元素中添加注释
     *
     * @param obj
     * @param comment
     */
    public static void addComment(Object obj, String comment) {
        if (obj instanceof Document) {
            ((Document) obj).addComment(comment);
        } else if (obj instanceof Element) {
            ((Element) obj).addComment(comment);
        }
    }

    /**
     * 给元素添加属性和属性值
     *
     * @param name
     * @param value
     */
    public static void setAttribute(Element element, String name, String value) {
        element.addAttribute(name, value);
    }

    /**
     * 写xml文件
     *
     * @param outXml
     * @param document
     * @param encoding
     */
    public static void writerXML(OutputStream outXml, Document document, String encoding) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        if (StringUtils.isBlank(encoding)) {
            encoding = DEFAULT_ENCODING;
        }
        format.setEncoding(encoding);
        XMLWriter out = null;
        try {
            out = new XMLWriter(outXml, format);
            out.write(document);
            out.flush();
        } catch (UnsupportedEncodingException e) {
            LOGGER.error(e.getMessage(), e);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
        }
    }

    public static Map<String, String> parserXml(String xml) {
        Map<String, String> responseMap = new HashMap<String, String>();
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(DEFAULT_ENCODING)));
            Element employees = document.getRootElement();
            for (Iterator i = employees.elementIterator(); i.hasNext(); ) {
                Element employee = (Element) i.next();
                Iterator j = employee.elementIterator();
                if (j.hasNext()) {
                    for (; j.hasNext(); ) {
                        Element nlement = (Element) j.next();
                        responseMap.put(employee.getName(), employee.getText());
                    }
                } else {
                    responseMap.put(employee.getName(), employee.getText());
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return responseMap;
    }
}

转载于:https://my.oschina.net/easy3399/blog/1594624

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值