package com.webpush.util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class XMLUtil {
/**
* 将xml字符串转化为List<Map<String, Object>> 层级和xml层级相同
* @param xmlstr 需要转换的xml字符串
* @return 转化之后的List<Map<String, Object>>
* @throws Exception
*/
public static List<Map<String, Object>> XMLToList(String xmlstr) throws Exception {
// 1.创建一个工厂
xmlstr=xmlstr.replaceAll(">\\s+<", "><");
//System.out.println(xmlstr);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 2.得到解析器
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(xmlstr.getBytes());
Document dm = db.parse(is);
NodeList nl = dm.getChildNodes();
List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
list=xmltolist(nl, list);
return list;
}
private static List<Map<String, Object>> xmltolist(NodeList nl, List<Map<String, Object>> list){
Element el = null;
Map<String, Object> map=null;
for(int i=0,len=nl.getLength();i<len;i++) {//患者列表
map=new HashMap<String, Object>();
el = (Element) nl.item(i);
NodeList cn=el.getChildNodes();
if(cn.getLength()>1) {
map.put(nl.item(i).getNodeName().toUpperCase(), xmltolist(cn, new ArrayList<Map<String, Object>>()));
}else {
map.put(nl.item(i).getNodeName().toUpperCase(), nl.item(i).getTextContent());
}
list.add(map);
}
return list;
}
public static void main(String[] args) throws Exception {
String strTest = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:s='http://www.w3.org/2001/XMLSchema'> <SOAP-ENV:Body><GetWardDataResponse xmlns=\"http://dhcc.com.cn\"><GetWardDataResult><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Responce><ResultCode>0</ResultCode><ResultDesc></ResultDesc><RecordCount>88</RecordCount><Records><Ward><WardCode>ZYHL001</WardCode><WardDesc>呼吸内科一二护理单元</WardDesc><WardID>1</WardID></Ward><Ward><WardCode>ZYHL002</WardCode><WardDesc>呼吸内科三四护理单元</WardDesc><WardID>2</WardID></Ward><Ward><WardCode>ZYHL003</WardCode><WardDesc>消化内科一护理单元</WardDesc><WardID>3</WardID></Ward><Ward><WardCode>ZYHL004</WardCode><WardDesc>消化内科二护理单元</WardDesc><WardID>4</WardID></Ward><Ward><WardCode>ZYHL005</WardCode><WardDesc>内分泌科护理单元</WardDesc><WardID>5</WardID></Ward><Ward><WardCode>ZYHL006</WardCode><WardDesc>血液内科护理单元</WardDesc><WardID>6</WardID></Ward><Ward><WardCode>ZYHL007</WardCode><WardDesc>风湿免疫科护理单元</WardDesc><WardID>7</WardID></Ward><Ward><WardCode>ZYHL008</WardCode><WardDesc>心内科一护理单元</WardDesc><WardID>8</WardID></Ward><Ward><WardCode>ZYHL009</WardCode><WardDesc>心内科二护理单元</WardDesc><WardID>9</WardID></Ward><Ward><WardCode>ZYHL010</WardCode><WardDesc>心内科重症病房护理单元</WardDesc><WardID>10</WardID></Ward><Ward><WardCode>ZYHL011</WardCode><WardDesc>肾内科一护理单元</WardDesc><WardID>11</WardID></Ward><Ward><WardCode>ZYHL012</WardCode><WardDesc>肾内科二护理单元</WardDesc><WardID>12</WardID></Ward><Ward><WardCode>ZYHL014</WardCode><WardDesc>神经内科一护理单元</WardDesc><WardID>13</WardID></Ward><Ward><WardCode>ZYHL015</WardCode><WardDesc>神经内科二护理单元</WardDesc><WardID>14</WardID></Ward><Ward><WardCode>ZYHL016</WardCode><WardDesc>儿科一护理单元</WardDesc><WardID>15</WardID></Ward><Ward><WardCode>ZYHL017</WardCode><WardDesc>儿科二护理单元</WardDesc><WardID>16</WardID></Ward><Ward><WardCode>ZYHL018</WardCode><WardDesc>新生儿科护理单元</WardDesc><WardID>17</WardID></Ward><Ward><WardCode>ZYHL021</WardCode><WardDesc>重症医学科一护理单元</WardDesc><WardID>18</WardID></Ward><Ward><WardCode>ZYHL022</WardCode><WardDesc>重症医学科二护理单元</WardDesc><WardID>19</WardID></Ward><Ward><WardCode>ZYHL023</WardCode><WardDesc>中医科护理单元</WardDesc><WardID>20</WardID></Ward><Ward><WardCode>ZYHL024</WardCode><WardDesc>皮肤科护理单元</WardDesc><WardID>21</WardID></Ward><Ward><WardCode>ZYHL025</WardCode><WardDesc>肿瘤科一护理单元</WardDesc><WardID>22</WardID></Ward><Ward><WardCode>ZYHL026</WardCode><WardDesc>肿瘤科二护理单元</WardDesc><WardID>23</WardID></Ward><Ward><WardCode>ZYHL027</WardCode><WardDesc>肿瘤科三护理单元</WardDesc><WardID>24</WardID></Ward><Ward><WardCode>ZYHL028</WardCode><WardDesc>日间病房护理单元</WardDesc><WardID>25</WardID></Ward><Ward><WardCode>ZYHL029</WardCode><WardDesc>老年医学科护理单元</WardDesc><WardID>26</WardID></Ward><Ward><WardCode>ZYHL030</WardCode><WardDesc>保健病房护理单元</WardDesc><WardID>27</WardID></Ward><Ward><WardCode>ZYHL031</WardCode><WardDesc>肝胆外科护理单元</WardDesc><WardID>28</WardID></Ward><Ward><WardCode>ZYHL032</WardCode><WardDesc>胃肠外科护理单元</WardDesc><WardID>29</WardID></Ward><Ward><WardCode>ZYHL033</WardCode><WardDesc>乳甲外科护理单元</WardDesc><WardID>30</WardID></Ward><Ward><WardCode>ZYHL034</WardCode><WardDesc>血管外科护理单元</WardDesc><WardID>31</WardID></Ward><Ward><WardCode>ZYHL035</WardCode><WardDesc>脊柱外科护理单元</WardDesc><WardID>32</WardID></Ward><Ward><WardCode>ZYHL036</WardCode><WardDesc>骨关节外科护理单元</WardDesc><WardID>33</WardID></Ward><Ward><WardCode>ZYHL037</WardCode><WardDesc>手足外科护理单元</WardDesc><WardID>34</WardID></Ward><Ward><WardCode>ZYHL038</WardCode><WardDesc>泌一结石护理单元</WardDesc><WardID>35</WardID></Ward><Ward><WardCode>ZYHL039</WardCode><WardDesc>泌尿外科二护理单元</WardDesc><WardID>36</WardID></Ward><Ward><WardCode>ZYHL040</WardCode><WardDesc>小儿外科护理单元</WardDesc><WardID>37</WardID></Ward><Ward><WardCode>ZYHL041</WardCode><WardDesc>烧伤整形外科护理单元</WardDesc><WardID>38</WardID></Ward><Ward><WardCode>ZYHL042</WardCode><WardDesc>心胸外科护理护理单元</WardDesc><WardID>39</WardID></Ward><Ward><WardCode>ZYHL043</WardCode><WardDesc>神经外科肿瘤功能护理单元</WardDesc><WardID>40</WardID></Ward><Ward><WardCode>ZYHL044</WardCode><WardDesc>神经外科血管护理单元</WardDesc><WardID>41</WardID></Ward><Ward><WardCode>ZYHL045</WardCode><WardDesc>妇科一护理单元</WardDesc><WardID>42</WardID></Ward><Ward><WardCode>ZYHL046</WardCode><WardDesc>妇科二护理单元</WardDesc><WardID>43</WardID></Ward><Ward><WardCode>ZYHL047</WardCode><WardDesc>产科一护理单元</WardDesc><WardID>44</WardID></Ward><Ward><WardCode>ZYHL048</WardCode><WardDesc>产科二护理单元</WardDesc><WardID>45</WardID></Ward><Ward><WardCode>ZYHL049</WardCode><WardDesc>眼科护理单元</WardDesc><WardID>46</WardID></Ward><Ward><WardCode>ZYHL050</WardCode><WardDesc>耳鼻咽喉头颈外科护理单元</WardDesc><WardID>47</WardID></Ward><Ward><WardCode>ZYHL051</WardCode><WardDesc>口腔科护理单元</WardDesc><WardID>48</WardID></Ward><Ward><WardCode>ZYHL052</WardCode><WardDesc>急诊创伤外科护理单元</WardDesc><WardID>49</WardID></Ward><Ward><WardCode>FYHL001</WardCode><WardDesc>神经内科护理单元[分院]</WardDesc><WardID>50</WardID></Ward><Ward><WardCode>FYHL002</WardCode><WardDesc>心内科一护理单元[分院]</WardDesc><WardID>51</WardID></Ward><Ward><WardCode>FYHL003</WardCode><WardDesc>心内科二护理单元[分院]</WardDesc><WardID>52</WardID></Ward><Ward><WardCode>FYHL004</WardCode><WardDesc>呼吸内科护理单元[分院]</War