码迷,mamicode.com
首页 > 其他好文 > 详细

XML字符串转为Map集合

时间:2019-07-26 17:41:48      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:except   password   shang   strong   info   add   XML   属性   param   

public class xmlToMapUtils {

    /**
     * xml字符串转为map集合
     * @param xmlStr
     * @return
     */
    public static Map<String, Object> xmlToMap(String xmlStr){
        List<Map<String, String>> resultList = iterateWholeXML(xmlStr);
        Map<String, Object> retMap = new HashMap<String, Object>();
        for (int i = 0; i < resultList.size(); i++) {
            Map<String,Object> map = (Map)resultList.get(i);

            for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
                String key = (String) iterator.next();
                retMap.put(key, map.get(key));

            }
        }
        return retMap;
    }

    /**
     * 递归解析任意的xml 遍历每个节点和属性
     *
     * @param xmlStr
     */
    private static List<Map<String, String>> iterateWholeXML(String xmlStr) {

        List<Map<String, String>> list = new ArrayList();

        try {
            Document document = DocumentHelper.parseText(xmlStr);
            Element root = document.getRootElement();
            recursiveNode(root, list);
            return list;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 递归遍历所有的节点获得对应的值
     *
     * @param
     */
    private static void recursiveNode(Element root, List<Map<String, String>> list) {

        // 遍历根结点的所有孩子节点
        HashMap<String, String> map = new HashMap();
        for (Iterator iter = root.elementIterator(); iter.hasNext();) {
            Element element = (Element) iter.next();
            if (element == null)
                continue;
            // 获取属性和它的值
            for (Iterator attrs = element.attributeIterator(); attrs.hasNext();) {
                Attribute attr = (Attribute) attrs.next();
                if (attr == null)
                    continue;
                String attrName = attr.getName();
                String attrValue = attr.getValue();

                map.put(attrName, attrValue);
            }
            // 如果有PCDATA,则直接提出
            if (element.isTextOnly()) {
                String innerName = element.getName();
                String innerValue = element.getText();
                map.put(innerName, innerValue);
                list.add(map);
            } else {
                // 递归调用
                recursiveNode(element, list);
            }
        }
    }
    /**
     * 测试
     */
    public static void main(String[] args) {
        String xmlStr = "<?xml version='1.0' encoding='UTF-8'?><OPERATION><HEAD><TIMESTAMP>20190621173338</TIMESTAMP><LOCATION>SHANGHAI</LOCATION></HEAD><BODY><OPERATORID>admin</OPERATORID><OPERATORIP>127.0.0.1</OPERATORIP><USERINFO><USERID>1</USERID><USERNAME>zhangsan</USERNAME><PASSWORD>123456</PASSWORD><MOBILE>11111111111</MOBILE></USERINFO></BODY></OPERATION>";
        Map<String, Object> xmlToMap = xmlToMap(xmlStr);
        Set<Map.Entry<String,Object>> entrySet = xmlToMap.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            System.out.println(entry.getKey()+"---"+entry.getValue());
        }
    }
}

结果如下:
技术图片

XML字符串转为Map集合

标签:except   password   shang   strong   info   add   XML   属性   param   

原文地址:https://www.cnblogs.com/JohnDawson/p/11251651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!