本文研究的主要是java使用DOM对XML文档进行增删改查操作的相关代码,具体实例如下所示。
源代码:
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
|
package com.zc.homeWork18;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLWriter {
private static String xmlPath = "src\\com\\zc\\homeWork18\\MyXml.xml" ;
public static void getFamilyMemebers() {
/*
* 创建文件工厂实例
*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 如果创建的解析器在解析XML文档时必须删除元素内容中的空格,则为true,否则为false
dbf.setIgnoringElementContentWhitespace(true);
try {
/*
* 创建文件对象
*/
DocumentBuilder db = dbf.newDocumentBuilder();// 创建解析器,解析XML文档
Document doc = db.parse(xmlPath); // 使用dom解析xml文件
/*
* 历遍列表,进行XML文件的数据提取
*/
// 根据节点名称来获取所有相关的节点
NodeList sonlist = doc.getElementsByTagName( "son" );
for ( int i = 0 ; i < sonlist.getLength(); i++) // 循环处理对象
{
// 节点属性的处理
Element son = (Element) sonlist.item(i);
// 循环节点son内的所有子节点
for (Node node = son.getFirstChild(); node != null ; node = node
.getNextSibling()) {
// 判断是否为元素节点
if (node.getNodeType() == Node.ELEMENT_NODE) {
String name = node.getNodeName();
String value = node.getFirstChild().getNodeValue();
System.out.println(name + " : " + value);
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 修改
public static void modifySon() {
// 创建文件工厂实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace( true );
try {
// 从XML文档中获取DOM文档实例
DocumentBuilder db = dbf.newDocumentBuilder();
// 获取Document对象
Document xmldoc = db.parse(xmlPath);
// 获取根节点
Element root = xmldoc.getDocumentElement();
// 定位id为001的节点
Element per = (Element) selectSingleNode( "/father/son[@id='001']" ,
root);
// 将age节点的内容更改为28
per.getElementsByTagName( "age" ).item( 0 ).setTextContent( "28" );
// 保存
TransformerFactory factory = TransformerFactory.newInstance();
Transformer former = factory.newTransformer();
former.transform( new DOMSource(xmldoc), new StreamResult( new File(
xmlPath)));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 获取目标节点,进行删除,最后保存
public static void discardSon() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace( true );
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document xmldoc = db.parse(xmlPath);
// 获取根节点
Element root = xmldoc.getDocumentElement();
// 定位根节点中的id=002的节点
Element son = (Element) selectSingleNode( "/father/son[@id='002']" ,
root);
// 删除该节点
root.removeChild(son);
// 保存
TransformerFactory factory = TransformerFactory.newInstance();
Transformer former = factory.newTransformer();
former.transform( new DOMSource(xmldoc), new StreamResult( new File(
xmlPath)));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 新增节点
public static void createSon() {
// 创建文件工厂实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace( false );
try {
DocumentBuilder db = dbf.newDocumentBuilder();
// 创建Document对象
Document xmldoc = db.parse(xmlPath);
// 获取根节点
Element root = xmldoc.getDocumentElement();
// 创建节点son,设置对应的id为004
Element son = xmldoc.createElement( "son" );
son.setAttribute( "id" , "004" );
// 创建节点name
Element name = xmldoc.createElement( "name" );
name.setTextContent( "小儿子" );
son.appendChild(name);
// 创建节点age
Element age = xmldoc.createElement( "age" );
age.setTextContent( "0" );
son.appendChild(age);
// 把son添加到根节点中
root.appendChild(son);
// 保存
TransformerFactory factory = TransformerFactory.newInstance();
Transformer former = factory.newTransformer();
former.transform( new DOMSource(xmldoc), new StreamResult( new File(
xmlPath)));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 修改节点信息
public static Node selectSingleNode(String express, Element source) {
Node result = null ;
//创建XPath工厂
XPathFactory xpathFactory = XPathFactory.newInstance();
//创建XPath对象
XPath xpath = xpathFactory.newXPath();
try {
result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);
System.out.println(result);
} catch (XPathExpressionException e) {
System.out.println(e.getMessage());
}
return result;
}
// 打印
public static void main(String[] args) {
getFamilyMemebers();
System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
modifySon();
System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
System.out.println( "修改数据" );
getFamilyMemebers();
System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
discardSon();
System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
System.out.println( "删除数据" );
getFamilyMemebers();
System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
createSon();
System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
System.out.println( "添加数据" );
getFamilyMemebers();
}
}
|
XML文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
< father >
< son id = "001" >
< name >老大</ name >
< age >20</ age >
</ son >
< son id = "002" >
< name >老二</ name >
< age >18</ age >
</ son >
< son id = "003" >
< name >老三</ name >
< age >13</ age >
</ son >
</ father >
|
总结
以上就是本文关于java使用DOM对XML文档进行增删改查操作实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://blog.csdn.net/zc2274460/article/details/51182536