xml格式字符串与java对象互转

Java代码  

  1. import java.lang.reflect.Field;  
  2. import java.util.List;  
  3. import com.thoughtworks.xstream.XStream;  
  4. public class XmlUtil {  
  5. // 所有需要注册的类的配置,  
  6. public static final String CONFIGFILE = new GetClassPath()  
  7. .getMyConfigPath()  
  8. + "/xml.prop";  
  9. private static XStream xs = null;  
  10. private static XmlUtil xmlUtil = null;  
  11. private XmlUtil() {  
  12. try {  
  13. // 获取所有需要与处理的java完整名  
  14. List list = FileUtil.readFile(CONFIGFILE);  
  15. //初始化xs  
  16. xs = alias(list);  
  17. catch (ClassNotFoundException e) {  
  18. e.printStackTrace();  
  19. }  
  20. }  
  21. /** 
  22. * 获得实例 
  23. * @return 
  24. */  
  25. public static synchronized XmlUtil getInstance() {  
  26. if(null == xmlUtil){  
  27. xmlUtil = new XmlUtil();  
  28. }  
  29. return xmlUtil;  
  30. }  
  31. /** 
  32. * 根据类名做反射 
  33. *  
  34. * @param list 
  35. *            类完整名 
  36. * @return 
  37. * @throws ClassNotFoundException 
  38. */  
  39. public XStream alias(List list) throws ClassNotFoundException {  
  40. XStream xs = new XStream();  
  41. for (int i = 0; i < list.size(); i++) {  
  42. Object obj;  
  43. try {  
  44. obj = Class.forName((String) list.get(i)).newInstance();  
  45. Class zz = obj.getClass();  
  46. aliasAtt(xs, zz);  
  47. catch (InstantiationException e) {  
  48. e.printStackTrace();  
  49. catch (IllegalAccessException e) {  
  50. e.printStackTrace();  
  51. }  
  52. }  
  53. return xs;  
  54. }  
  55. /** 
  56. * 对类进行xml解析配置 
  57. *  
  58. * @param xs 
  59. * @param z 
  60. *            class对象 
  61. */  
  62. public void aliasAtt(XStream xs, Class z) {  
  63. if (null == z) {  
  64. return;  
  65. }  
  66. // 类名注册  
  67. xs.alias(z.getSimpleName(), z);  
  68. Field[] field = z.getDeclaredFields();  
  69. // 类中成员变量注册,继承的不注册  
  70. for (int i = 0; i < field.length; i++) {  
  71. xs.aliasField(field[i].getName(), z, field[i].getName());  
  72. }  
  73. }  
  74. /** 
  75. * xml 格式字符串转换为对象 
  76. * @param str 
  77. * @return 
  78. */  
  79. public Object xmlToObject(String str) {  
  80. return xs.fromXML(str);  
  81. }  
  82. /** 
  83. * 对象转换为xml格式字符串 
  84. * @param obj 
  85. * @return 
  86. */  
  87. public String objToXml(Object obj) {  
  88. return xs.toXML(obj);  
  89. }  
  90. }  
  91. FileUtil.readFile()方法如下  
  92. /** 
  93. * 读取文件 
  94. *  
  95. * @param filePath 
  96. *            文件完整名 
  97. * @return 文件内容 
  98. * @throws IOException 
  99. */  
  100. public static List readFile(String filePath) {  
  101. List list = new ArrayList();  
  102. FileInputStream fs = null;  
  103. InputStreamReader isr = null;  
  104. BufferedReader br = null;  
  105. try {  
  106. fs = new FileInputStream(filePath);  
  107. isr = new InputStreamReader(fs);  
  108. br = new BufferedReader(isr);  
  109. String data = "";  
  110. while ((data = br.readLine()) != null) {  
  111. list.add(data.trim());  
  112. }  
  113. catch (IOException e) {  
  114. e.printStackTrace();  
  115. finally {  
  116. try {  
  117. if (br != null)  
  118. br.close();  
  119. if (isr != null)  
  120. isr.close();  
  121. if (fs != null)  
  122. fs.close();  
  123. catch (IOException e) {  
  124. e.printStackTrace();  
  125. }  
  126. }  
  127. return list;  
  128. }  
  129. Test  
  130. Printdata pd = new Printdata();  
  131. pd.setPrintFileList(printFileList);  
  132. pd.setAuto("1");  
  133. pd.setBillReceiver("billReceiver");  
  134. pd.setCustId("cust_id");  
  135. pd.setYear("2011");  
  136. pd.setMonth("11");  
  137. List list = FileUtil.readFile(XmlUtil.CONFIGFILE);  
  138. XmlUtil xmlUtil = XmlUtil.getInstance();  
  139. String xml = xmlUtil.objToXml(pd);//对象至xml  
  140. System.out.println(xml);  
  141. Printdata obj = (Printdata)xmlUtil.xmlToObject(xml);//xml至对象  
posted @ 2017-09-22 15:30  太原中软  阅读(1916)  评论(0编辑  收藏  举报