java XML解析成Map

1.需要解析的文件.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <request>
 3     <realName>test</realName>
 4     <identityNumber>411525152242417185276</identityNumber>
 5     <phone>1314456788</phone>
 6     <user>
 7         <value>1444444</value>
 8         <name>12334</name>
 9             <aa>
10                 <hehe>222222</hehe>
11             </aa>
12         <age>23</age>
13     </user>
14 </request>

2.工具包

 1 package com.ynet.utils;
 2 
 3 import org.dom4j.Document;
 4 import org.dom4j.Element;
 5 
 6 import java.util.*;
 7 
 8 /**
 9  * Created by Arya on 2017/11/3.
10  */
11 public class XmlUtils {
12     public static Map<String, Object> Dom2Map(Document doc) {
13         Map<String, Object> map = new HashMap<String, Object>();
14         if (doc == null)
15             return map;
16         Element root = doc.getRootElement();
17         for (Iterator iterator = root.elementIterator(); iterator.hasNext(); ) {
18             Element e = (Element) iterator.next();
19             List list = e.elements();
20             if (list.size() > 0) {
21                 map.put(e.getName(), Dom2Map(e));
22             } else
23                 map.put(e.getName(), e.getText());
24         }
25         return map;
26     }
27 
28 
29     public static Map Dom2Map(Element e) {
30         Map map = new HashMap();
31         List list = e.elements();
32         if (list.size() > 0) {
33             for (int i = 0; i < list.size(); i++) {
34                 Element iter = (Element) list.get(i);
35                 List mapList = new ArrayList();
36 
37                 if (iter.elements().size() > 0) {
38                     Map m = Dom2Map(iter);
39                     if (map.get(iter.getName()) != null) {
40                         Object obj = map.get(iter.getName());
41                         if (!obj.getClass().getName().equals("java.util.ArrayList")) {
42                             mapList = new ArrayList();
43                             mapList.add(obj);
44                             mapList.add(m);
45                         }
46                         if (obj.getClass().getName().equals("java.util.ArrayList")) {
47                             mapList = (List) obj;
48                             mapList.add(m);
49                         }
50                         map.put(iter.getName(), mapList);
51                     } else
52                         map.put(iter.getName(), m);
53                 } else {
54                     if (map.get(iter.getName()) != null) {
55                         Object obj = map.get(iter.getName());
56                         if (!obj.getClass().getName().equals("java.util.ArrayList")) {
57                             mapList = new ArrayList();
58                             mapList.add(obj);
59                             mapList.add(iter.getText());
60                         }
61                         if (obj.getClass().getName().equals("java.util.ArrayList")) {
62                             mapList = (List) obj;
63                             mapList.add(iter.getText());
64                         }
65                         map.put(iter.getName(), mapList);
66                     } else
67                         map.put(iter.getName(), iter.getText());
68                 }
69             }
70         } else
71             map.put(e.getName(), e.getText());
72         return map;
73     }
74 }

3.控制器

 1 package com.ynet.controller;
 2 
 3 
 4 import com.ynet.utils.XmlUtils;
 5 import org.dom4j.Document;
 6 import org.dom4j.DocumentHelper;
 7 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 import org.dom4j.DocumentException;
12 
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.util.Map;
16 
17 /**
18  * Created by Arya on 2017/11/3.
19  */
20 @RestController
21 @EnableAutoConfiguration
22 public class XmlDemoController {
23     @RequestMapping(value="/xmlDemo")
24     public Map<String, Object> xmlDocumentFile(){
25         Document doc = null;
26         try {
27             FileInputStream fis = new FileInputStream("F:\\myweb\\gateway\\gateway2\\src\\main\\resources\\customer.xml");
28             byte[] b = new byte[fis.available()];
29             fis.read(b);
30             String str = new String(b);
31             doc = DocumentHelper.parseText(str);
32         } catch (IOException e) {
33             e.printStackTrace();
34         } catch (DocumentException e) {
35             e.printStackTrace();
36         }
37 
38         return XmlUtils.Dom2Map(doc);
39     }
40 }

4.postman测试结果

原文地址:https://www.cnblogs.com/arrrrrya/p/7780104.html