XmlRead.java
5.1 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
package com.espeed.text;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class XmlRead {
/**读xml文件*/
public List readXml(String file){
List list = new ArrayList();
File isf = new File(file);
if (!isf.exists()) {
System.out.println("mailService.xml文件不存在,请配置服务器参数");
}else{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document document;
try {
builder = factory.newDocumentBuilder();
document = builder.parse(file);
Element root = document.getDocumentElement();// 取得根元素
NodeList serviceList = root.getElementsByTagName("CustomerNO");// 取得子元素
System.out.println(serviceList.getLength());
for (int i = 0; i < serviceList.getLength(); i++) {
Text t1 = (Text) serviceList.item(i).getFirstChild();
System.out.println(t1.getNodeValue());
//int i=serviceList.getLength()-1;
//读最后一个节点信息
Element service = (Element) serviceList.item(i);// 每一个子元素
//获取文件夹名字
NodeList snum = service.getElementsByTagName("CustomerNO");
if (snum.getLength() == 1) {
Element e = (Element) snum.item(0);
Text t = (Text) e.getFirstChild();
if (t != null) {
System.out.println(t.getNodeValue());
list.add(t.getNodeValue().toString());
}
}
//获取文件夹数量
// NodeList hosts = service.getElementsByTagName("Category");
// if (hosts.getLength() == 1) {
// Element e = (Element) hosts.item(0);
// Text t = (Text) e.getFirstChild();
//
// if (t != null) {
// System.out.println(t.getNodeValue());
// list.add(t.getNodeValue().toString());
// }
// }
}
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
/**写xml文件*/
public static void writeXml(String filename,String filenum,String file){
try {
Document document = getDocument(file);
NodeList nodeList = document.getElementsByTagName("MAILSERVICELIST");
// 创建新的节点
Node studentNode = document.createElement("MAILSERVICE");
Node numNode=document.createElement("FILENAME");
numNode.appendChild(document.createTextNode(filename));
Node nameNode = document.createElement("FILENUM");
nameNode.appendChild(document.createTextNode(filenum));
// 添加节点
studentNode.appendChild(numNode);
studentNode.appendChild(nameNode);
nodeList.item(0).appendChild(studentNode);
// 此时真正的处理将新数据添加到文件中(磁盘)
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
StreamResult result = new StreamResult(pw);
DOMSource dsource = new DOMSource(document);
//StreamResult sr = new StreamResult(new File(file));
transformer.transform(dsource, result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**读xml文件所用的方法*/
public static Document getDocument(String fileName) {
Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(fileName));
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
/**修改xml文件*/
public static void updateNode(String filePath,int value) {
Document document = getDocument(filePath);
NodeList nodeList = document.getElementsByTagName("FILENUM");
int i=nodeList.getLength()-1;
Node parentNode = nodeList.item(i).getParentNode();
NodeList nl = parentNode.getChildNodes();
for (int j = 0; j < nl.getLength(); j++) {
String modifyNode = nl.item(j).getNodeName();
if (modifyNode.equalsIgnoreCase("FILENUM")) {
nl.item(j).getFirstChild().setTextContent(String.valueOf(value));
}
}
modifyFile(document, filePath);
}
/**将改动持久到文件*/
public static void modifyFile(Document doc, String distFileName) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer tfer = tf.newTransformer();
DOMSource dsource = new DOMSource(doc);
StreamResult sr = new StreamResult(new File(distFileName));
tfer.transform(dsource, sr);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
XmlRead a=new XmlRead();
a.readXml("F://xml.xml");
}
}