XMLUtil.java
4.8 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Created on 2006-3-30
*
*
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.sitech.util;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
/**
* @author Administrator
*
* XML工具类
*
* DocumentBuilderFactory 和 DocumentBuilder is NOT guaranteed to be thread safe.
* 所以每个方法中都是新创建
*/
public class XMLUtil {
/**
* Normalizes the given string. 在向xml文件中写入东西时,一些特殊字符需要处理 不过如果使用 <![CDATA[]]>
* 的话,可以不处理
*/
public static String normalize(String s) {
StringBuffer str = new StringBuffer();
int len = (s != null) ? s.length() : 0;
for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
switch (ch) {
case '<': {
str.append("<");
break;
}
case '>': {
str.append(">");
break;
}
case '&': {
str.append("&");
break;
}
case '"': {
str.append(""");
break;
}
/*
* case '\r': case '\n': { str.append("&#");
* str.append(Integer.toString(ch)); str.append(';'); break; }
*/
default: {
str.append(ch);
}
}
}
return str.toString();
}
/**
* 解析一个xml文件,返回其Document filename为绝对路径
*/
public static Document parseFile2Doc(String filename) {
Document doc = null;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse(filename);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return doc;
}// end parseFile2Doc(String filename)
/**
* 解析一个String格式的xml数据,返回其Document
*/
public static Document parseString2Doc(String content) {
Document doc = null;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse(new java.io.ByteArrayInputStream(content
.getBytes()));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return doc;
}// end parseString2Doc(String filename)
/**
* 创建一个新xml document
*/
public static Document newDocument() {
Document doc = null;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return doc;
}// end Document newDocument()
/**
* 将document写入到文件中 filename为绝对路径
*/
public static void writeXmlFile(Document doc, String filename) {
try {
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
// Prepare the output file
File file = new File(filename);
Result result = new StreamResult(file);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}// end writeXmlFile(Document doc, String filename)
/**
* Creates ElementNode for given tag with particular text as content.
*/
public static Element createTextNode(Document doc, String tag,
String content) throws ParserConfigurationException {
Element node = doc.createElement(tag);
Text t = doc.createTextNode(content);
node.appendChild(t);
return node;
}
/**
* Creates ElementNode for given tag with particular text as content.
*/
public static Element createCDATATextNode(Document doc, String tag,
String content) throws ParserConfigurationException {
Element node = doc.createElement(tag);
Text t = doc.createCDATASection(content);
node.appendChild(t);
return node;
}
}
// XmlUtil
// ////////////////// end of file ////////////////////