JSON和Map的互轉、xml轉Map

需要引入的jar包:

1.commons-lang.jar 

2.commons-beanutils.jar

3.commons-collections.jar

4.commons-logging.jar 

5.ezmorph.jar

6.json-lib-2.2.2-jdk15.jar

具體代碼如下:

  1. import java.io.BufferedReader; 
  2. import java.io.File; 
  3. import java.io.FileInputStream; 
  4. import java.io.InputStream; 
  5. import java.io.InputStreamReader; 
  6. import java.util.List; 
  7. import java.util.Map; 
  8.  
  9. import net.sf.json.JSONObject; 
  10.  
  11. import org.dom4j.Document; 
  12. import org.dom4j.DocumentException; 
  13. import org.dom4j.DocumentHelper; 
  14.  
  15. public class JsonTest { 
  16.  
  17.     public static void main(String[] args) throws DocumentException { 
  18.          
  19.         //*******************Map 轉 JSon********************** 
  20.         Document doc = DocumentHelper.parseText(getContent( 
  21.                 "C:\\Documents and Settings\\wzhe\\桌面\\測試用例\\安防\\", "413.txt")); 
  22.         Map map = XmlUtils.dom2Map(doc); 
  23.         JSONObject jsobj= JSONObject.fromObject(map); 
  24.         System.out.println(jsobj.toString()); 
  25.          
  26.         //*******************JSON 轉 Map********************** 
  27.         Map ttMap= ((Map)JSONObject.fromObject(getContent("C:\\Documents and Settings\\wzhe\\桌面\\測試用例\\JSON\\", "test.txt"))); 
  28.         Map tmpObj = (Map) ttMap.get("BCR"); 
  29.         List tmpList= (List) tmpObj.get("BC_INFO"); 
  30.         Map bcMap = (Map) tmpList.get(0); 
  31.         System.out.println(bcMap.get("FAULT_REASON").toString()); 
  32.     } 
  33.  
  34.     public static String getContent(String filePath, String fileName) { 
  35.         File file = new File(filePath, fileName); 
  36.         InputStream strm = null
  37.         StringBuffer content = new StringBuffer(); 
  38.         try { 
  39.             strm = new FileInputStream(file); 
  40.  
  41.             BufferedReader reader = new BufferedReader(new InputStreamReader( 
  42.                     strm, "GBK")); 
  43.             String line = ""
  44.             while ((line = reader.readLine()) != null) { 
  45.                 content.append(line); 
  46.             } 
  47.         } catch (Exception e1) { 
  48.             e1.printStackTrace(); 
  49.         } 
  50.         return content.toString(); 
  51.     } 

 

XmlUtils.java
  1. import java.io.BufferedReader; 
  2. import java.io.File; 
  3. import java.io.FileInputStream; 
  4. import java.io.InputStream; 
  5. import java.io.InputStreamReader; 
  6. import java.io.UnsupportedEncodingException; 
  7. import java.util.ArrayList; 
  8. import java.util.HashMap; 
  9. import java.util.Iterator; 
  10. import java.util.List; 
  11. import java.util.Map; 
  12.  
  13. import org.dom4j.Document; 
  14. import org.dom4j.DocumentException; 
  15. import org.dom4j.DocumentHelper; 
  16. import org.dom4j.Element; 
  17. import org.dom4j.tree.ContentListFacade; 
  18. import org.dom4j.tree.DefaultAttribute; 
  19.  
  20. public class XmlUtils { 
  21.    
  22.  
  23.     public static Map<String, Object> dom2Map(Document doc) { 
  24.         Map<String, Object> map = new HashMap<String, Object>(); 
  25.         // 判斷需要解析的文檔是否爲空 
  26.         if (doc == null) { 
  27.             return map; 
  28.         } 
  29.         // 獲取根節點 
  30.         Element root = doc.getRootElement(); 
  31.  
  32.         // 獲取根節點下的子節點迭代器 
  33.         Iterator iterator = root.elementIterator(); 
  34.  
  35.         // 循環子節點,開始向map中存值 
  36.         while (iterator.hasNext()) { 
  37.             Element e = (Element) iterator.next(); 
  38.             List list = e.elements(); 
  39.  
  40.             // 判斷當前節點是否有子節點 
  41.             // 如果存在子節點調用element2Map(Element e)方法,不存在子節點直接存進map中 
  42.             if (list.size() > 0) { 
  43.                 map.put(e.getName(), element2Map(e)); 
  44.             } else { 
  45.                 saveAttribute2Map(map, e); 
  46.                 map.put(e.getName(), e.getText()); 
  47.             } 
  48.         } 
  49.         return map; 
  50.     } 
  51.  
  52.     private static Map<String, Object> element2Map(Element e) { 
  53.         Map<String, Object> map = new HashMap<String, Object>(); 
  54.         List<?> list = e.elements(); 
  55.         saveAttribute2Map(map, e); 
  56.         if (list.size() > 0) { 
  57.             for (int i = 0, j = list.size(); i < j; i++) { 
  58.                 Element iter = (Element) list.get(i); 
  59.                 List<Object> mapList = new ArrayList<Object>(); 
  60.                 // 存在子節點 
  61.                 if (iter.elements().size() > 0) { 
  62.                     Map<?, ?> m = element2Map(iter); 
  63.                     if (map.get(iter.getName()) != null) { 
  64.                         Object obj = map.get(iter.getName()); 
  65.                         if (!obj.getClass().getName() 
  66.                                 .equals("java.util.ArrayList")) { 
  67.                             mapList = new ArrayList<Object>(); 
  68.                             mapList.add(obj); 
  69.                             mapList.add(m); 
  70.                         } 
  71.                         if (obj.getClass().getName() 
  72.                                 .equals("java.util.ArrayList")) { 
  73.                             mapList = (List) obj; 
  74.                             mapList.add(m); 
  75.                         } 
  76.                         map.put(iter.getName(), mapList); 
  77.                     } else 
  78.                         map.put(iter.getName(), m); 
  79.                 } else { 
  80.                     if (map.get(iter.getName()) != null) { 
  81.                         Object obj = map.get(iter.getName()); 
  82.                         if (!obj.getClass().getName() 
  83.                                 .equals("java.util.ArrayList")) { 
  84.                             mapList = new ArrayList(); 
  85.                             mapList.add(obj); 
  86.                             mapList.add(iter.getText()); 
  87.                         } 
  88.                         if (obj.getClass().getName() 
  89.                                 .equals("java.util.ArrayList")) { 
  90.                             mapList = (List) obj; 
  91.                             mapList.add(iter.getText()); 
  92.                         } 
  93.                         map.put(iter.getName(), mapList); 
  94.                     } else 
  95.                         map.put(iter.getName(), iter.getText()); 
  96.                 } 
  97.             } 
  98.         } else { 
  99.             saveAttribute2Map(map, e); 
  100.             map.put(e.getName(), e.getText()); 
  101.         } 
  102.  
  103.         return map; 
  104.     } 
  105.  
  106.     private static void saveAttribute2Map(Map<String, Object> map, Element e) { 
  107.         ContentListFacade attributes = (ContentListFacade) e.attributes(); 
  108.         if (attributes.size() > 0) { 
  109.             HashMap<String, String> attrMap = new HashMap<String, String>(); 
  110.             map.put("attribute", attrMap); 
  111.             DefaultAttribute attrTmp = null
  112.             for (int i = 0, j = attributes.size(); i < j; i++) { 
  113.                 attrTmp = (DefaultAttribute) attributes.get(i); 
  114.                 attrMap.put(attrTmp.getName(), attrTmp.getValue()); 
  115.             } 
  116.         } 
  117.     } 
  118.  
  119.     public static String getContent(String filePath, String fileName) { 
  120.         File file = new File(filePath, fileName); 
  121.         InputStream strm = null
  122.         StringBuffer content = new StringBuffer(); 
  123.         try { 
  124.             strm = new FileInputStream(file); 
  125.  
  126.             BufferedReader reader = new BufferedReader(new InputStreamReader( 
  127.                     strm, "GBK")); 
  128.             String line = ""
  129.             while ((line = reader.readLine()) != null) { 
  130.                 content.append(line); 
  131.             } 
  132.         } catch (Exception e1) { 
  133.             e1.printStackTrace(); 
  134.         } 
  135.         return content.toString(); 
  136.     } 
  137.  
  138.     public static void main(String[] args) throws DocumentException, 
  139.             UnsupportedEncodingException { 
  140.         Document doc = DocumentHelper.parseText(getContent( 
  141.                 "C:\\Documents and Settings\\wzhe\\桌面\\測試用例\\安防\\", "413.txt")); 
  142.         Map map = XmlUtils.dom2Map(doc); 
  143.         Map bcrMap = (Map) map.get("BCR"); 
  144.         System.out.println(((List) bcrMap.get("BC_INFO")).toArray().toString()); 
  145.         System.out.println(map.toString()); 
  146.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章