package com.zjrs.ggfw.shbzk.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;


public class XmlUtils {
    private static final String HEAD = "head";
    private static final String BODY = "body";


    public static void main(String[] args) throws DocumentException {
        String xml = "<ERR>01</ERR><INFO>审核不通过。您已申办湛江市社保卡，无需重复申请。</INFO>";
        System.out.println(xmlToMap(xml));
    }

    public static Map<String,Object> xmlToMap(String xml) throws DocumentException {
        Map<String, Object> returnMap=new HashMap<>();
        String err=xml.substring(xml.indexOf("<ERR>")+5,xml.indexOf("</ERR>"));
        String rows="1";
        if(xml.contains("<ROWS>")){
            rows=xml.substring(xml.indexOf("<ROWS>")+6,xml.indexOf("</ROWS>"));
        }
        if(err.equals("00")||err.equals("OK")){
            if(xml.contains("<ROW>")){
                xml="<ap><body><ROWLIST>"+xml.substring(xml.indexOf("<ROW>"))+"</ROWLIST></body></ap>";
                Map<String, Object> xmlMap=parseXmlStr(xml);
                Map<String, Object> apMap= (Map<String, Object>) xmlMap.get("ap");
                List<Map<String,String>> rowlist= (List<Map<String, String>>) apMap.get("ROWLIST");
                returnMap.put("ROWS",rowlist);
            }else if(Integer.parseInt(rows)<=0){
                returnMap.put("ROWS",new ArrayList<Map<String, Object>>());
            }else{
                xml="<ap><body>"+xml+"</body></ap>";
                Map<String, Object> xmlMap=parseXmlStr(xml);
                Map<String, Object> apMap= (Map<String, Object>) xmlMap.get("ap");
                returnMap.putAll(apMap);
            }
        }else{
            xml="<ap><body>"+xml+"</body></ap>";
            Map<String, Object> xmlMap=parseXmlStr(xml);
            Map<String, Object> apMap= (Map<String, Object>) xmlMap.get("ap");
            returnMap.putAll(apMap);
        }
        returnMap.put("ERR",err);
        return returnMap;
    }

    /**
     * 解析XML字符串
     *
     * @param xml
     * @return
     * @throws DocumentException
     */
    private static Map<String, Object> parseXmlStr(String xml)
            throws DocumentException {
        Document document = DocumentHelper.parseText(xml);
        Element root = document.getRootElement();
        return parseElement(root);
    }


    /**
     * 解析Element
     *
     * @param root
     * @return
     */
    @SuppressWarnings("unchecked")
    private static Map<String, Object> parseElement(Element root) {
        String rootName = root.getName();
        Iterator<Element> rootItor = root.elementIterator();
        Map<String, Object> rMap = new HashMap<>();
        List<Map<String, Object>> rList = new ArrayList<>();
        Map<String, Object> rsltMap = null;
        while (rootItor.hasNext()) {
            Element tmpElement = rootItor.next();
            String name = tmpElement.getName();
            if (rsltMap == null || (!HEAD.equals(name) && !BODY.equals(name)
                    && !tmpElement.isTextOnly())) {
                if (!HEAD.equals(name) && !BODY.equals(name)
                        && !tmpElement.isTextOnly() && rsltMap != null) {
                    rList.add(rsltMap);
                }
                rsltMap = new HashMap<>();
            }
            if (!tmpElement.isTextOnly()) {
                Iterator<Element> headItor = tmpElement.elementIterator();
                while (headItor.hasNext()) {
                    Element hElement = headItor.next();
                    if (hElement.isTextOnly()) {
                        rsltMap.put(hElement.getName(), hElement.getTextTrim());
                    } else {
                        rsltMap.putAll(parseElement(hElement));
                    }
                }
            }
        }
        rList.add(rsltMap);
        if(!rootName.equals("ROWLIST")){
            rMap.put(rootName, rsltMap);
        }else{
            rMap.put(rootName, rList);
        }
        return rMap;
    }
}
