<array> <dict> <key>cities</key> <array> <dict> <key>areas</key> <array> <string>东城区</string> <string>西城区</string> <string>朝阳区</string> <string>丰台区</string> <string>石景山区</string> <string>海淀区</string> <string>门头沟区</string> <string>房山区</string> <string>通州区</string> <string>顺义区</string> <string>昌平区</string> <string>大兴区</string> <string>怀柔区</string> <string>平谷区</string> <string>密云县</string> <string>延庆县</string> <string>其他</string> </array> <key>city</key> <string>北京市</string> </dict> </array> <key>province</key> <string>北京</string> </dict> </array>
<array> <dict> <key>citiesCode</key> <array> <dict> <key>areasCode</key> <array> <string>37</string> <string>38</string> <string>41</string> <string>42</string> <string>43</string> <string>44</string> <string>45</string> <string>46</string> <string>47</string> <string>48</string> <string>49</string> <string>50</string> <string>51</string> <string>52</string> <string>53</string> <string>54</string> <string>566</string> </array> <key>cityCode</key> <string>36</string> </dict> </array> <key>provinceCode</key> <string>1</string> </dict> </array>
这两个文件一一对应的
根据第一个文件的省市县得到第二个文件的省市县的代号,或根据第二个文件的省市县的代号得到第一个文件的省市县
解决方案
40
package com.cdv.util.common; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Namespace; import org.dom4j.QName; public class XmltoMap { //根据字符串xml解析成map @SuppressWarnings("unchecked") public static Map<String,Object> getXmlMap(String content){ Map<String,Object> map = new HashMap<String, Object>(); if(content==null){ return null; } if(content.length()==0){ return map; } try { Document doc = DocumentHelper.parseText(content); Element element = doc.getRootElement(); map=(Map<String,Object>)xml2map(element); // System.out.println(map); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return map; } @SuppressWarnings("unchecked") public static Object xml2map(Element element) { // System.out.println(element); Map<String, Object> map = new HashMap<String, Object>(); List<Element> elements = element.elements(); if (elements.size() == 0) { map.put(element.getName(),element.getText() ); if (!element.isRootElement()) { return element.getText(); } } else if (elements.size() == 1) { map.put(elements.get(0).getName(), xml2map(elements.get(0))); } else if (elements.size() > 1) { // 多个子节点的话就得考虑list的情况了,例如多个子节点有节点名称相同的 // 构造一个map用来去重 Map<String, Element> tempMap = new HashMap<String, Element>(); for (Element ele : elements) { tempMap.put(ele.getName(), ele); } Set<String> keySet = tempMap.keySet(); for (String string : keySet) { Namespace namespace = tempMap.get(string).getNamespace(); List<Element> elements2 = element.elements(new QName(string, namespace)); // 假如同名的数目大于1则表示要构建list if (elements2.size() > 1) { List<Object> list = new ArrayList<Object>(); for (Element ele : elements2) { list.add(xml2map(ele)); } map.put(string, list); } else { // 同名的数量不大于1则直接递归去 map.put(string, xml2map(elements2.get(0))); } } } return map; } }