-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dom4jHelper.java
175 lines (164 loc) · 4.33 KB
/
Dom4jHelper.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
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
/**
*
*/
package com.gootrip.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* @author advance
*
*/
public class Dom4jHelper {
/**
* 解析url xml文档
* @param url
* @return
* @throws DocumentException
*/
public Document parse(URL url) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(url);
return document;
}
/**
* 遍历解析文档
* @param document
*/
public void treeWalk(Document document) {
treeWalk( document.getRootElement() );
}
/**
* 遍历解析元素
* @param element
*/
public void treeWalk(Element element) {
for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
Node node = element.node(i);
if ( node instanceof Element ) {
treeWalk( (Element) node );
}
else {
// 处理....
}
}
}
/**
* 解析文件,获得根元素
* @param xmlPath
* @param encoding
* @return
* @throws Exception
*/
public static Element parse(String xmlPath,String encoding)throws Exception{
//文件是否存在
File file = new File(xmlPath);
if(!file.exists()){
throw new Exception("找不到xml文件:"+xmlPath);
}
//解析
SAXReader reader = new SAXReader(false);
Document doc = reader.read(new FileInputStream(file),encoding);
Element root = doc.getRootElement();
return root;
}
/**
* 保存文档
* @param doc
* @param xmlPath
* @param encoding
* @throws Exception
*/
public static void save(Document doc,String xmlPath,String encoding)throws Exception{
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding(encoding);
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(xmlPath),encoding),format);
writer.write(doc);
writer.flush();
writer.close();
}
/**
* 修改xml某节点的值
* @param inputXml 原xml文件
* @param nodes 要修改的节点
* @param attributename 属性名称
* @param value 新值
* @param outXml 输出文件路径及文件名 如果输出文件为null,则默认为原xml文件
*/
public static void modifyDocument(File inputXml, String nodes, String attributename, String value, String outXml) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
List list = document.selectNodes(nodes);
Iterator iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getName().equals(attributename))
attribute.setValue(value);
}
XMLWriter output;
if (outXml != null){ //指定输出文件
output = new XMLWriter(new FileWriter(new File(outXml)));
}else{ //输出文件为原文件
output = new XMLWriter(new FileWriter(inputXml));
}
output.write(document);
output.close();
}
catch (DocumentException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* xml转换为字符串
* @param doc
* @param encoding
* @return
* @throws Exception
*/
public static String toString(Document doc,String encoding)throws Exception{
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding(encoding);
ByteArrayOutputStream byteOS=new ByteArrayOutputStream();
XMLWriter writer = new XMLWriter(new OutputStreamWriter(byteOS,encoding),format);
writer.write(doc);
writer.flush();
writer.close();
writer=null;
return byteOS.toString(encoding);
}
/**
* 字符串转换为Document
* @param text
* @return
* @throws DocumentException
*/
public static Document str2Document(String text) throws DocumentException{
Document document = DocumentHelper.parseText(text);
return document;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}