1. 首页 > 知识 > Java

Java递归多层(多节点)xml转map方式

/**
* 通过XML转换为Map<String,Object>
* @param xml 为String类型的Xml
* @return 第一个为Root节点,Root节点之后为Root的元素,如果为多层,可以通过key获取下一层Map
*/
public static Map<String, Object> createMapByXml(String xml) {
    Document doc = null;
    try {
        doc = DocumentHelper.parseText(xml);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    Map<String, Object> map = new HashMap<String, Object>();
    if (doc == null)
        return map;
    Element rootElement = doc.getRootElement();
    elementTomap(rootElement, map);
    return map;
}

/**
* 通过XML转换为Map<String,Object>
* @param xml 为File类型的Xml   例:new File("/gtook.com.xml")
* @return 第一个为Root节点,Root节点之后为Root的元素,如果为多层,可以通过key获取下一层Map
*/
public static Map<String, Object> createMapByXml(File xml) {
    SAXReader reader = new SAXReader();
    Document doc = null;
    try {
        doc = reader.read(xml);
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    ///+++++++++++++
    System.out.println(doc.asXML());

    Map<String, Object> map = new HashMap<String, Object>();
    if (doc == null)
        return map;
    Element rootElement = doc.getRootElement();
    elementTomap(rootElement, map);
    return map;
}

/***
* XmlToMap核心方法,里面有递归调用
* @param map
* @param ele
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> elementTomap(Element outele,
        Map<String, Object> outmap) {
    List<Element> list = outele.elements();
    int size = list.size();
    if (size == 0) {
        outmap.put(outele.getName(), outele.getTextTrim());
    } else {
        Map<String, Object> innermap = new HashMap<String, Object>();
        for (Element ele1 : list) {
            String eleName = ele1.getName();
            Object obj = innermap.get(eleName);
            if (obj == null) {
                elementTomap(ele1, innermap);
            } else {
                if (obj instanceof java.util.Map) {
                    List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
                    list1.add((Map<String, Object>) innermap.remove(eleName));
                    elementTomap(ele1, innermap);
                    list1.add((Map<String, Object>) innermap.remove(eleName));
                    innermap.put(eleName, list1);
                } else if (obj instanceof List) {
                    List<Object> list2 = (List<Object>) obj;
                    if (list2.get(0) instanceof Map) {                          //判断List下是String还是Map
                        elementTomap(ele1, innermap);
                        list2.add((Map<String, Object>) innermap.remove(eleName));
                        innermap.put(eleName, list2);
                    }else {
                        elementTomap(ele1, innermap);
                        list2.add(ele1.getTextTrim());
                        innermap.put(eleName, list2);
                    }
                } else if (obj instanceof String) {                         //同一级只有一层的标签
                    List<Object> list2 = new ArrayList<Object>();
                    list2.add((String) obj);
                    elementTomap(ele1, innermap);
                    list2.add(ele1.getTextTrim());
                    innermap.put(eleName, list2);
                }else {
                    elementTomap(ele1, innermap);
                    ((List<Map<String, Object>>) obj).add(innermap);
                }
            }
        }
        outmap.put(outele.getName(), innermap);
    }
    return outmap;
}


本文由一叶发布,不代表一叶立场,转载联系作者并注明出处:https://yiyeo.com/java/34.html

加载中~

联系我们

在线咨询:点击这里给我发消息

微信号:

工作日:10:30-15:30,节假日休息