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