/* * 数据转换工具类,用以将单个对象、List转换为json、xml格式的字符串 */

/** * 数据转换工具类,用于将单个对象、List转换为json、xml格式的字符串 */
注意:List中只有存放的是相应的Java对象,才能正确转换成json、xml格式
1、核心
Java代码  /*   * 数据转换工具类,用以将单个对象、List转换为json、xml格式的字符串   */
  1. package com.dreamoa.util;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import com.dreamoa.domain.Menu;  
  9. import com.thoughtworks.xstream.XStream;  
  10. import com.thoughtworks.xstream.io.xml.DomDriver;  
  11.   
  12. import net.sf.json.JSONObject;  
  13.   
  14. /** 
  15.  * 数据转换工具类,用于将单个对象、List转换为json、xml格式的字符串 
  16.  */  
  17. public class ExtUtil {  
  18.       
  19.     /** 
  20.      * 将list对象转换为json格式的数据 
  21.      * @param totalNum,记录总数 
  22.      * @param inList,需要转换的list 
  23.      * @return 
  24.      */  
  25.     public static String getJsonFromList(long totalNum,List inList){  
  26.         Map<String, Object> map = new HashMap<String, Object>();  
  27.         map.put("totalNum", inList.size());  
  28.         map.put("resultList", inList);  
  29.         JSONObject jsonObj = JSONObject.fromObject(map);  
  30.         return jsonObj.toString();  
  31.     }  
  32.       
  33.     /** 
  34.      * 将单个对象转换为json格式,此对象不能为集合类型 
  35.      * @param inObject 
  36.      * @return 
  37.      */  
  38.     public static String getJsonFromObject(Object inObject){  
  39.         JSONObject jsonString = JSONObject.fromObject(inObject);  
  40.         return jsonString.toString();  
  41.     }  
  42.       
  43.     /** 
  44.      * 将List转化为xml格式的数据 
  45.      * @param totalNum 
  46.      * @param inList,需要转换的list 
  47.      * @return String 
  48.      */  
  49.     public static String getXmlFromList(long totalNum,List inList){  
  50.         Total total = new Total();  
  51.         total.setTotalNum(totalNum);  
  52.         //创建临时的List对象  
  53.         List results = inList;  
  54.         results.add(total);  
  55.         //创建XStream对象  
  56.         XStream xs = new XStream(new DomDriver());  
  57.         //为所有的类创建别名,别名为不包含包名的类名  
  58.         for (int i = 0; i < results.size(); i++) {  
  59.             Class clzz = results.get(i).getClass();  
  60.             //得到全限定类名  
  61.             String fullName = clzz.getName();  
  62.             //以"."分割字符串  
  63.             String [] temp = fullName.split("\\.");  
  64.             xs.alias(temp[temp.length-1], clzz);  
  65.         }  
  66.         String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+xs.toXML(results);  
  67.         return xmlString;  
  68.     }  
  69.       
  70.     /** 
  71.      * 将一个Object对象转化为xml格式输出 
  72.      * @param object 
  73.      * @return 
  74.      */  
  75.     public static String getXmlFromObject(Object object){  
  76.         XStream xs = new XStream(new DomDriver());  
  77.         Class clazz = object.getClass();  
  78.         String [] temp = clazz.getName().split("\\.");  
  79.         xs.alias(temp[temp.length-1], clazz);  
  80.         String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+xs.toXML(object);  
  81.         return xmlString;  
  82.     }  
  83.       
  84.     /** 
  85.      * 将对象转换为表单数据加载需要的格式 
  86.      * @param object 
  87.      * @return 
  88.      */  
  89.     public static String getLoadFormData(Object object){  
  90.           
  91.         return "";  
  92.     }  
  93.     public static void main(String[] args) {  
  94.         Menu menu = new Menu();  
  95.         menu.setName("name");  
  96.         menu.setParentid("123");  
  97.         Menu menu2 = new Menu();  
  98.         menu2.setName("name2");  
  99.         menu2.setParentid("1233");  
  100.         List inList = new ArrayList();  
  101.         inList.add(menu);  
  102.         inList.add(menu2);  
  103.         System.out.println(ExtUtil.getXmlFromList(inList.size(), inList));  
  104.     }  
  105. }  


2、Total.java
Java代码  /*   * 数据转换工具类,用以将单个对象、List转换为json、xml格式的字符串   */
  1. package com.hf.sfm.util;  
  2.   
  3. import java.util.List;  
  4.   
  5. /** 
  6.  * 将List转化为xml时getXmlFromList(),用来存放记录总数 
  7.  * @author Administrator 
  8.  * 
  9.  */  
  10. public class Total {  
  11.       
  12.     private long totalNum;//记录总数  
  13.     private List resultList;//要转换的List,也是转换后的结果集名称  
  14.   
  15.     public long getTotalNum() {  
  16.         return totalNum;  
  17.     }  
  18.   
  19.     public void setTotalNum(long totalNum) {  
  20.         this.totalNum = totalNum;  
  21.     }  
  22.   
  23.     public List getResultList() {  
  24.         return resultList;  
  25.     }  
  26.   
  27.     public void setResultList(List resultsList) {  
  28.         this.resultList = resultsList;  
  29.     }  
  30.       
  31. }