package com.icloud.server.payutils;


import com.google.common.collect.Maps;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Description: XMLUtil
 * @Author: lin.shi
 * @CreateTime: 2017-08-10 16:47
 */
public class XMLUtil {

    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。
     *
     * @param strxml
     * @return
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) {
        Document document = null;
        try {
            document = DocumentHelper.parseText(strxml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element rootElement = document.getRootElement();
        @SuppressWarnings("unchecked")
        List<org.dom4j.Element> elements = rootElement.elements();
        Map<String, String> map = Maps.newHashMap();
        for (org.dom4j.Element element : elements) {
            map.put(element.getName(), element.getText());
        }
        return map;
    }


}