XML工具类

  1     import java.io.File;  
2 import java.io.FileWriter;
3 import java.io.StringWriter;
4
5 import org.dom4j.Document;
6 import org.dom4j.DocumentException;
7 import org.dom4j.DocumentHelper;
8 import org.dom4j.Element;
9 import org.dom4j.io.OutputFormat;
10 import org.dom4j.io.SAXReader;
11 import org.dom4j.io.XMLWriter;
12
13 /**
14 * XML工具类
15 *
16 */
17 public class XMLUtils {
18
19 /**
20 * 返回格式化的XML字段串
21 *
22 * @param document
23 * 要格式化的文档
24 * @param encoding
25 * 使用的编码,如果为null刚使用默认编码(gb2312)
26 * @return 格式化的XML字段串
27 */
28 public static String toXMLString(Document document, String encoding) {
29 if (encoding == null) {
30 encoding = "gb2312";
31 }
32 StringWriter writer = new StringWriter();
33 OutputFormat format = OutputFormat.createPrettyPrint();
34 format.setEncoding("gb2312");
35 XMLWriter xmlwriter = new XMLWriter(writer, format);
36 try {
37 xmlwriter.write(document);
38 } catch (Exception e) {
39 e.printStackTrace();
40 }
41 return writer.toString();
42 }
43
44 /**
45 * 返回格式化的XML字段串
46 *
47 * @param element
48 * 要格式化的节点元素
49 * @param encoding
50 * 使用的编码,如果为null刚使用默认编码(gb2312)
51 * @return 格式化的XML字段串
52 */
53 public static String toXMLString(Element element, String encoding) {
54 if (encoding == null) {
55 encoding = "gb2312";
56 }
57 StringWriter writer = new StringWriter();
58 OutputFormat format = OutputFormat.createPrettyPrint();
59 format.setEncoding(encoding);
60 XMLWriter xmlwriter = new XMLWriter(writer, format);
61 try {
62 xmlwriter.write(element);
63 } catch (Exception e) {
64 e.printStackTrace();
65 }
66 return writer.toString();
67 }
68
69 /**
70 * 格式化文档并输出到文件
71 *
72 * @param document
73 * 要输出的文档
74 * @param filename
75 * XML文件名
76 * @param encoding
77 * 使用的编码,如果为null刚使用默认编码(gb2312)
78 * @return true or false
79 */
80 public static boolean toXMLFile(Document document, String filename,
81 String encoding) {
82 if (encoding == null) {
83 encoding = "gb2312";
84 }
85 boolean returnValue = false;
86 try {
87 XMLWriter output = null;
88 /** 格式化输出,类型IE浏览一样 */
89 OutputFormat format = OutputFormat.createPrettyPrint();
90 /** 指定XML字符集编码 */
91 format.setEncoding(encoding);
92 output = new XMLWriter(new FileWriter(new File(filename)), format);
93 output.write(document);
94 output.close();
95 /** 执行成功,需返回1 */
96 returnValue = true;
97 } catch (Exception ex) {
98 ex.printStackTrace();
99 returnValue = false;
100 }
101 return returnValue;
102 }
103
104 /**
105 * 格式化XML文件并保存
106 *
107 * @param srcFileName
108 * 源XML文件
109 * @param desFileName
110 * 格式化后的XML文件,如果为null,则使用srcFileName
111 * @param encoding
112 * 使用的编码,如果为null刚使用默认编码(gb2312)
113 * @return true or false
114 */
115 public static boolean toXMLFile(String srcFileName, String desFileName,
116 String encoding) {
117 if (encoding == null) {
118 encoding = "gb2312";
119 }
120 if (desFileName == null) {
121 desFileName = srcFileName;
122 }
123 boolean returnValue = false;
124 try {
125 SAXReader saxReader = new SAXReader();
126 Document document = saxReader.read(new File(srcFileName));
127 XMLWriter output = null;
128 /** 格式化输出,类型IE浏览一样 */
129 OutputFormat format = OutputFormat.createPrettyPrint();
130 /** 指定XML字符集编码 */
131 format.setEncoding(encoding);
132 output = new XMLWriter(new FileWriter(new File(desFileName)),
133 format);
134 output.write(document);
135 output.close();
136 /** 执行成功,需返回1 */
137 returnValue = true;
138 } catch (Exception ex) {
139 ex.printStackTrace();
140 returnValue = false;
141 }
142 return returnValue;
143 }
144
145 /**
146 * 从读取XML文件
147 *
148 * @param fileName
149 * @return Document对象
150 */
151 public static Document read(String fileName) {
152 SAXReader reader = new SAXReader();
153 Document document = null;
154 try {
155 document = reader.read(new File(fileName));
156 } catch (DocumentException e) {
157 // TODO Auto-generated catch block
158 e.printStackTrace();
159 }
160 return document;
161 }
162
163 /**
164 * 从XML字符串转换到document
165 *
166 * @param xmlStr
167 * XML字符串
168 * @return Document
169 */
170 public static Document parseText(String xmlStr) {
171 Document document = null;
172 try {
173 document = DocumentHelper.parseText(xmlStr);
174 } catch (DocumentException e) {
175 // TODO Auto-generated catch block
176 e.printStackTrace();
177 }
178 return document;
179 }
180 }



原文地址:https://www.cnblogs.com/ayan/p/2307972.html