java XML解析成Map

來源:互聯網
上載者:User

標籤:dom4j   blog   toc   ring   port   cep   tcl   java   xml解析   

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             } else23                 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                     } else52                         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                     } else67                         map.put(iter.getName(), iter.getText());68                 }69             }70         } else71             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 @RestController21 @EnableAutoConfiguration22 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測試結果

 

java XML解析成Map

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.