提交 | 用户 | age
|
826b66
|
1 |
package com.hx.util; |
C |
2 |
|
|
3 |
import javax.xml.bind.JAXBContext; |
|
4 |
import javax.xml.bind.JAXBException; |
|
5 |
import javax.xml.bind.Marshaller; |
|
6 |
import javax.xml.bind.Unmarshaller; |
|
7 |
import java.io.*; |
|
8 |
|
|
9 |
public class XmlUtil { |
|
10 |
|
|
11 |
/** |
|
12 |
* 将对象直接转换成String类型的 XML输出 |
|
13 |
* |
|
14 |
* @param obj |
|
15 |
* @return |
|
16 |
*/ |
|
17 |
public static String convertToXml(Object obj) { |
|
18 |
// 创建输出流 |
|
19 |
StringWriter sw = new StringWriter(); |
|
20 |
try { |
|
21 |
// 利用jdk中自带的转换类实现 |
|
22 |
JAXBContext context = JAXBContext.newInstance(obj.getClass()); |
|
23 |
|
|
24 |
Marshaller marshaller = context.createMarshaller(); |
|
25 |
// 格式化xml输出的格式 |
|
26 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, |
|
27 |
Boolean.TRUE); |
|
28 |
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式 |
|
29 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串 |
|
30 |
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xm头声明信息 |
|
31 |
// 将对象转换成输出流形式的xml |
|
32 |
marshaller.marshal(obj, sw); |
|
33 |
} catch (JAXBException e) { |
|
34 |
e.printStackTrace(); |
|
35 |
} |
|
36 |
return sw.toString(); |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* 将对象根据路径转换成xml文件 |
|
41 |
* |
|
42 |
* @param obj |
|
43 |
* @param path |
|
44 |
* @return |
|
45 |
*/ |
|
46 |
public static void convertToXml(Object obj, String path) { |
|
47 |
try { |
|
48 |
// 利用jdk中自带的转换类实现 |
|
49 |
JAXBContext context = JAXBContext.newInstance(obj.getClass()); |
|
50 |
|
|
51 |
Marshaller marshaller = context.createMarshaller(); |
|
52 |
// 格式化xml输出的格式 |
|
53 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, |
|
54 |
Boolean.TRUE); |
|
55 |
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式 |
|
56 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串 |
|
57 |
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xm头声明信息 |
|
58 |
// 将对象转换成输出流形式的xml |
|
59 |
// 创建输出流 |
|
60 |
FileWriter fw = null; |
|
61 |
try { |
|
62 |
fw = new FileWriter(path); |
|
63 |
} catch (IOException e) { |
|
64 |
e.printStackTrace(); |
|
65 |
} |
|
66 |
marshaller.marshal(obj, fw); |
|
67 |
} catch (JAXBException e) { |
|
68 |
e.printStackTrace(); |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
@SuppressWarnings("unchecked") |
|
73 |
/** |
|
74 |
* 将String类型的xml转换成对象 |
|
75 |
*/ |
|
76 |
public static Object convertXmlStrToObject(Class clazz, String xmlStr) { |
|
77 |
Object xmlObject = null; |
|
78 |
try { |
|
79 |
JAXBContext context = JAXBContext.newInstance(clazz); |
|
80 |
// 进行将Xml转成对象的核心接口 |
|
81 |
Unmarshaller unmarshaller = context.createUnmarshaller(); |
|
82 |
StringReader sr = new StringReader(xmlStr); |
|
83 |
xmlObject = unmarshaller.unmarshal(sr); |
|
84 |
} catch (JAXBException e) { |
|
85 |
e.printStackTrace(); |
|
86 |
} |
|
87 |
return xmlObject; |
|
88 |
} |
|
89 |
|
|
90 |
@SuppressWarnings("unchecked") |
|
91 |
/** |
|
92 |
* 将file类型的xml转换成对象 |
|
93 |
*/ |
|
94 |
public static Object convertXmlFileToObject(Class clazz, String xmlPath) { |
|
95 |
Object xmlObject = null; |
|
96 |
try { |
|
97 |
JAXBContext context = JAXBContext.newInstance(clazz); |
|
98 |
Unmarshaller unmarshaller = context.createUnmarshaller(); |
|
99 |
FileReader fr = null; |
|
100 |
try { |
|
101 |
fr = new FileReader(xmlPath); |
|
102 |
} catch (FileNotFoundException e) { |
|
103 |
e.printStackTrace(); |
|
104 |
} |
|
105 |
xmlObject = unmarshaller.unmarshal(fr); |
|
106 |
} catch (JAXBException e) { |
|
107 |
e.printStackTrace(); |
|
108 |
} |
|
109 |
return xmlObject; |
|
110 |
} |
|
111 |
|
|
112 |
|
|
113 |
} |