码迷,mamicode.com
首页 > 编程语言 > 详细

Java xml文件转换为map集合

时间:2015-09-03 11:39:18      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

public class XmlToMap {
  public static Map<String, Object> Dom2Map(Document doc){ 
         Map<String, Object> map = new HashMap<String, Object>(); 
         if(doc == null) 
             return map; 
         Element root = doc.getRootElement(); 
         for (Iterator iterator = root.elementIterator(); iterator.hasNext();) { 
             Element e = (Element) iterator.next(); 
             //System.out.println(e.getName()); 
             List list = e.elements(); 
             if(list.size() > 0){ 
                 map.put(e.getName(), Dom2Map(e)); 
             }else 
                 map.put(e.getName(), e.getText()); 
         } 
         return map; 
     }  
       
   
   public static Map Dom2Map(Element e){ 
   Map map = new HashMap(); 
         List list = e.elements(); 
         if(list.size() > 0){ 
             for (int i = 0;i < list.size(); i++) { 
                 Element iter = (Element) list.get(i); 
                 List mapList = new ArrayList(); 
                  
                 if(iter.elements().size() > 0){ 
                     Map m = Dom2Map(iter); 
                     if(map.get(iter.getName()) != null){ 
                         Object obj = map.get(iter.getName()); 
                         if(!obj.getClass().getName().equals("java.util.ArrayList")){ 
                             mapList = new ArrayList(); 
                             mapList.add(obj); 
                             mapList.add(m); 
                         } 
                         if(obj.getClass().getName().equals("java.util.ArrayList")){ 
                             mapList = (List) obj; 
                             mapList.add(m); 
                         } 
                         map.put(iter.getName(), mapList); 
                     }else 
                         map.put(iter.getName(), m); 
                 } 
                 else{ 
                     if(map.get(iter.getName()) != null){ 
                         Object obj = map.get(iter.getName()); 
                         if(!obj.getClass().getName().equals("java.util.ArrayList")){ 
                             mapList = new ArrayList(); 
                             mapList.add(obj); 
                             mapList.add(iter.getText()); 
                         } 
                         if(obj.getClass().getName().equals("java.util.ArrayList")){ 
                             mapList = (List) obj; 
                             mapList.add(iter.getText()); 
                         } 
                         map.put(iter.getName(), mapList); 
                     }else 
                         map.put(iter.getName(), iter.getText()); 
                 } 
             } 
         }else 
             map.put(e.getName(), e.getText()); 
         return map; 
     } 
}

Java xml文件转换为map集合

标签:

原文地址:http://my.oschina.net/u/2253208/blog/500877

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