1 package com.dq.schooldomain.utils;
2
3
4
5 import com.thoughtworks.xstream.XStream;
6 import com.thoughtworks.xstream.io.xml.DomDriver;
7 import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
8
9 import java.io.InputStream;
10 /**
11 * @Author Allen.Lv
12 * @Description //TODO
13 * @Date 11:51 2019/3/1
14 * @Desc: Coding Happy!
15 **/
16 public class XmlUtil {
17
18 /**
19 *
20 * @param inputXml
21 * @param type
22 * @return
23 * @throws Exception
24 */
25 public static Object xml2Object(String inputXml, Class<?> type)
throws Exception {
26 if (
null == inputXml || ""
.equals(inputXml)) {
27 return null;
28 }
29 XStream xstream =
new XStream(
new DomDriver("UTF-8",
new XmlFriendlyNameCoder("-_", "_"
)));
30 xstream.alias("xml"
, type);
31 return xstream.fromXML(inputXml);
32 }
33
34 /**
35 *
36 * @param inputStream
37 * @param type
38 * @return
39 * @throws Exception
40 */
41 public static Object xml2Object(InputStream inputStream, Class<?> type)
throws Exception {
42 if (
null ==
inputStream) {
43 return null;
44 }
45 XStream xstream =
new XStream(
new DomDriver("UTF-8",
new XmlFriendlyNameCoder("-_", "_"
)));
46 xstream.alias("xml"
, type);
47 return xstream.fromXML(inputStream, type);
48 }
49
50 /**
51 *
52 * @param ro
53 * @param types
54 * @return
55 * @throws Exception
56 */
57 public static String object2Xml(Object ro, Class<?> types)
throws Exception {
58 if (
null ==
ro) {
59 return null;
60 }
61 XStream xstream =
new XStream(
new DomDriver("UTF-8",
new XmlFriendlyNameCoder("-_", "_"
)));
62 xstream.alias("xml"
, types);
63 return xstream.toXML(ro);
64 }
65
66 }
转载于:https://www.cnblogs.com/javallh/p/10477185.html
相关资源:xml转对象封装的简单与复杂xml的转化写法