XmlKit.java
5.0 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.air.agent.imf;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.air.agent.imf.bean.ManifestEntity;
/**
* Created by cohesion on 2017/4/24.
*/
@XmlRootElement
public class XmlKit {
public static final Logger logger = LoggerFactory.getLogger(XmlKit.class);
public static void main(String[] args) {
String path = "/Users/Kevin/Downloads/zhu.xml";
try {
String xml = IO.readAsString(new File(path));
System.out.println("==========================xml==========================");
System.out.println(xml);
System.out.println();
System.out.println();
ManifestEntity mani = converyToJavaBean(xml, ManifestEntity.class);
System.out.println("==========================bean==========================");
System.out.println(mani.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* JavaBean转换成xml
*
* @param obj
* @return
*/
public static String convertToXml(Object obj) {
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 注意jdk版本
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos,
(String) marshaller.getProperty(Marshaller.JAXB_ENCODING));
xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
marshaller.marshal(obj, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.close();
return new String(baos.toString("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String convertToXml2(Object obj, String path) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
File file = new File(path);
if (file.exists()) {
file.delete();
}
file.getParentFile().mkdirs();
file.createNewFile();
byte bt[] = new byte[1024];
bt = writer.toString().getBytes();
try {
FileOutputStream in = new FileOutputStream(file);
try {
in.write(bt, 0, bt.length);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* xml转换成JavaBean
*
* @param xml
* @param c
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public static void writeXmlDocument(String content, String Encode, String XMLPathAndName) {
long lasting = System.currentTimeMillis();// 效率检测
try {
XMLWriter writer = null;// 声明写XML的对象
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(Encode);// 设置XML文件的编码格式
String filePath = XMLPathAndName;// 获得文件地址
File file = new File(filePath);// 获得文件
if (file.exists()) {
file.delete();
}
// 新建xml文件并新增内容
Document document = DocumentHelper.createDocument();
document.addElement(content);// 添加根节点
// 生成XML文件
writer = new XMLWriter(new FileWriter(file), format);
writer.write(document);
writer.close();
long lasting2 = System.currentTimeMillis();
System.out.println("写入XML文件结束,用时" + (lasting2 - lasting) + "ms");
} catch (Exception e) {
System.out.println("XML文件写入失败");
}
}
}