使用 Dom4j 将 XML 转换为 MAP
本文为转载:http://blog.sina.com.cn/s/blog_6145ed810100z164.html 原文地址。
自己仅作备忘录方便查找留了一份。
这是解析Xml 的辅助类
1 package com.zwr.www.util; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 9 import org.dom4j.Document; 10 import org.dom4j.Element; 11 12 public class XmlUtil { 13 @SuppressWarnings({ "rawtypes" }) 14 public static Map<String, Object> Dom2Map(Document doc) { 15 Map<String, Object> map = new HashMap<String, Object>(); 16 if (doc == null) 17 return map; 18 Element root = doc.getRootElement(); 19 for (Iterator iterator = root.elementIterator(); iterator.hasNext();) { 20 Element e = (Element) iterator.next(); 21 List list = e.elements(); 22 if (list.size() > 0) { 23 map.put(e.getName(), Dom2Map(e)); 24 } else 25 map.put(e.getName(), e.getText()); 26 } 27 return map; 28 } 29 30 @SuppressWarnings({ "unchecked", "rawtypes" }) 31 public static Map Dom2Map(Element e) { 32 Map map = new HashMap(); 33 List list = e.elements(); 34 if (list.size() > 0) { 35 for (int i = 0; i < list.size(); i++) { 36 Element iter = (Element) list.get(i); 37 List mapList = new ArrayList(); 38 39 if (iter.elements().size() > 0) { 40 Map m = Dom2Map(iter); 41 if (map.get(iter.getName()) != null) { 42 Object obj = map.get(iter.getName()); 43 if (!obj.getClass().getName() 44 .equals("java.util.ArrayList")) { 45 mapList = new ArrayList(); 46 mapList.add(obj); 47 mapList.add(m); 48 } 49 if (obj.getClass().getName() 50 .equals("java.util.ArrayList")) { 51 mapList = (List) obj; 52 mapList.add(m); 53 } 54 map.put(iter.getName(), mapList); 55 } else 56 map.put(iter.getName(), m); 57 } else { 58 if (map.get(iter.getName()) != null) { 59 Object obj = map.get(iter.getName()); 60 if (!obj.getClass().getName() 61 .equals("java.util.ArrayList")) { 62 mapList = new ArrayList(); 63 mapList.add(obj); 64 mapList.add(iter.getText()); 65 } 66 if (obj.getClass().getName() 67 .equals("java.util.ArrayList")) { 68 mapList = (List) obj; 69 mapList.add(iter.getText()); 70 } 71 map.put(iter.getName(), mapList); 72 } else 73 map.put(iter.getName(), iter.getText()); 74 } 75 } 76 } else 77 map.put(e.getName(), e.getText()); 78 return map; 79 } 80 }
下面是测试类
1 package com.zwr.www.test; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.util.Map; 7 8 import org.dom4j.Document; 9 import org.dom4j.DocumentException; 10 import org.dom4j.DocumentHelper; 11 12 import com.zwr.www.util.XmlUtil; 13 14 public class XmlToMapTest { 15 public static void main(String[] args) throws DocumentException, IOException { 16 FileInputStream fis = new FileInputStream("bin/message.xml"); 17 byte[] b = new byte[fis.available()]; 18 fis.read(b); 19 String str = new String(b); 20 Document doc = DocumentHelper.parseText(str); 21 System.out.println(doc.asXML()); 22 long beginTime = System.currentTimeMillis(); 23 Map<String, Object> map = XmlUtil.Dom2Map(doc); 24 System.out.println(map.toString()); 25 System.out.println("Use time:" + (System.currentTimeMillis() - beginTime)); 26 } 27 }
下面是测试用的xml文件
1 <message> 2 <head> 3 <trade_time>2014-11-12 05:04:05</trade_time> 4 <trade_type>R1000002</trade_type> 5 <content_lenge>1</content_lenge> 6 <content_trans_type>0</content_trans_type> 7 <check_code></check_code> 8 </head> 9 <body class="authorzation"> 10 <batch_no>20141105112233</batch_no> 11 <authorzation_list> 12 <p13> 13 <p13_no>c_xiaoming-001</p13_no> 14 <followAcc_list> 15 <followacc> 16 <follow_acc>xiaoliu1</follow_acc> 17 <branch_org_code>9000000000001</branch_org_code> 18 <company>520000123</company> 19 <aci_list> 20 <aci> 21 <aci_no>1001</aci_no> 22 <scope_list> 23 <scope> 24 <scope_no>a0001</scope_no> 25 <scope_type>TXT</scope_type> 26 <scope_value_list> 27 <scope_value> 28 <min_value>abc</min_value> 29 <max_value></max_value> 30 </scope_value> 31 <scope_value> 32 <min_value>abc</min_value> 33 <max_value>123</max_value> 34 </scope_value> 35 </scope_value_list> 36 </scope> 37 <scope> 38 <scope_no>a0002</scope_no> 39 <scope_type>TXT</scope_type> 40 <scope_value_list> 41 <scope_value> 42 <min_value>abc</min_value> 43 <max_value>123</max_value> 44 </scope_value> 45 </scope_value_list> 46 </scope> 47 </scope_list> 48 <opertion_type>1</opertion_type> 49 </aci> 50 </aci_list> 51 </followacc> 52 </followAcc_list> 53 </p13> 54 55 <p13> 56 <p13_no>c_xiaoming-002</p13_no> 57 <followAcc_list> 58 <followacc> 59 <follow_acc>xiaoliu2</follow_acc> 60 <branch_org_code>9000000000001</branch_org_code> 61 <company>520000123</company> 62 <aci_list> 63 <aci> 64 <aci_no>1002</aci_no> 65 <scope_list> 66 <scope> 67 <scope_no>a0001</scope_no> 68 <scope_type>TXT</scope_type> 69 <scope_value_list> 70 <scope_value> 71 <min_value>abc</min_value> 72 <max_value></max_value> 73 </scope_value> 74 <scope_value> 75 <min_value>abc</min_value> 76 <max_value>123</max_value> 77 </scope_value> 78 </scope_value_list> 79 </scope> 80 <scope> 81 <scope_no>a0002</scope_no> 82 <scope_type>TXT</scope_type> 83 <scope_value_list> 84 <scope_value> 85 <min_value>abc</min_value> 86 <max_value>123</max_value> 87 </scope_value> 88 </scope_value_list> 89 </scope> 90 </scope_list> 91 <opertion_type>1</opertion_type> 92 </aci> 93 </aci_list> 94 </followacc> 95 </followAcc_list> 96 </p13> 97 98 </authorzation_list> 99 </body> 100 </message>
在此感谢 郑君华