java XML解析成Map

时间:2023-03-10 00:00:17
java XML解析成Map

1.需要解析的文件.xml

 <?xml version="1.0" encoding="UTF-8"?>
<request>
<realName>test</realName>
<identityNumber>411525152242417185276</identityNumber>
<phone>1314456788</phone>
<user>
<value>1444444</value>
<name>12334</name>
<aa>
<hehe>222222</hehe>
</aa>
<age>23</age>
</user>
</request>

2.工具包

 package com.ynet.utils;

 import org.dom4j.Document;
import org.dom4j.Element; import java.util.*; /**
* Created by Arya on 2017/11/3.
*/
public class XmlUtils {
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();
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;
}
}

3.控制器

 package com.ynet.controller;

 import com.ynet.utils.XmlUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.dom4j.DocumentException; import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map; /**
* Created by Arya on 2017/11/3.
*/
@RestController
@EnableAutoConfiguration
public class XmlDemoController {
@RequestMapping(value="/xmlDemo")
public Map<String, Object> xmlDocumentFile(){
Document doc = null;
try {
FileInputStream fis = new FileInputStream("F:\\myweb\\gateway\\gateway2\\src\\main\\resources\\customer.xml");
byte[] b = new byte[fis.available()];
fis.read(b);
String str = new String(b);
doc = DocumentHelper.parseText(str);
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} return XmlUtils.Dom2Map(doc);
}
}

4.postman测试结果

java XML解析成Map