-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlSerializeUtils.java
58 lines (53 loc) · 1.19 KB
/
XmlSerializeUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
*
*/
package com.autonavi.test.yxb.lib;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author xiangbin.yang
*
*/
public class XmlSerializeUtils {
/**
* Serialize object to xml file
*
* @param object
* @param xmlFile
* @throws IOException
*/
public static void SaveToXml(Object object, String xmlFile) throws IOException {
File save2File = new File(xmlFile);
if (!save2File.exists()) {
save2File.createNewFile();
}
try (OutputStream out = new FileOutputStream(save2File)) {
try (XMLEncoder xmlEncoder = new XMLEncoder(out)) {
xmlEncoder.writeObject(object);
}
}
}
/**
* Deserialize object from xml file
*
* @param xmlFile
* @return
* @throws IOException
*/
public static Object deserialze(String xmlFile) throws IOException {
Object object = null;
File file = new File(xmlFile);
try (InputStream in = new FileInputStream(file)) {
try (XMLDecoder xmlDecoder = new XMLDecoder(in)) {
object = xmlDecoder.readObject();
}
}
return object;
}
}