1
package
dom;
2
3
import
java.io.File;
4
import
java.io.FileWriter;
5
import
java.util.Iterator;
6
import
java.util.List;
7
8
import
org.dom4j.Attribute;
9
import
org.dom4j.Document;
10
import
org.dom4j.DocumentHelper;
11
import
org.dom4j.Element;
12
import
org.dom4j.io.OutputFormat;
13
import
org.dom4j.io.SAXReader;
14
import
org.dom4j.io.XMLWriter;
15
16
/** */
/**
17
*
@author
~@_@
18
*/
19
public
class
SimpleDom4j
{
20
21
/** */
/**
22
* 建立一个XML文档,文档名由输入属性决定
23
*
@param
filename 需建立的文件名
24
*
@return
返回操作结果, 0表失败, 1表成功
25
*/
26
public
int
createXMLFile(String filename)
{
27
/** */
/**
返回操作结果, 0表失败, 1表成功
*/
28
int
returnValue
=
0
;
29
/** */
/**
建立document对象
*/
30
Document document
=
DocumentHelper.createDocument();
31
/** */
/**
建立XML文档的根books
*/
32
Element booksElement
=
document.addElement(
"
books
"
);
33
/** */
/**
加入一行注释
*/
34
booksElement.addComment(
"
This is a test for dom4j, holen, 2004.9.11
"
);
35
/** */
/**
加入第一个book节点
*/
36
Element bookElement
=
booksElement.addElement(
"
book
"
);
37
/** */
/**
加入show属性内容
*/
38
bookElement.addAttribute(
"
show
"
,
"
yes
"
);
39
/** */
/**
加入title节点
*/
40
Element titleElement
=
bookElement.addElement(
"
title
"
);
41
/** */
/**
为title设置内容
*/
42
titleElement.setText(
"
Dom4j Tutorials
"
);
43
/** */
/**
类似的完成后两个book
*/
44
bookElement
=
booksElement.addElement(
"
book
"
);
45
bookElement.addAttribute(
"
show
"
,
"
yes
"
);
46
titleElement
=
bookElement.addElement(
"
title
"
);
47
titleElement.setText(
"
Lucene Studing
"
);
48
bookElement
=
booksElement.addElement(
"
book
"
);
49
bookElement.addAttribute(
"
show
"
,
"
no
"
);
50
titleElement
=
bookElement.addElement(
"
title
"
);
51
titleElement.setText(
"
Lucene in Action
"
);
52
/** */
/**
加入owner节点
*/
53
Element ownerElement
=
booksElement.addElement(
"
owner
"
);
54
ownerElement.setText(
"
O'Reilly
"
);
55
try
{
56
/** */
/**
将document中的内容写入文件中
*/
57
XMLWriter writer
=
new
XMLWriter(
new
FileWriter(
new
File(filename)));
58
writer.write(document);
59
writer.close();
60
/** */
/**
执行成功,需返回1
*/
61
returnValue
=
1
;
62
}
catch
(Exception ex)
{
63
ex.printStackTrace();
64
}
65
return
returnValue;
66
}
67
68
/** */
/**
69
* 修改XML文件中内容,并另存为一个新文件
70
* 重点掌握dom4j中如何添加节点,修改节点,删除节点
71
*
@param
filename 修改对象文件
72
*
@param
newfilename 修改后另存为该文件
73
*
@return
返回操作结果, 0表失败, 1表成功
74
*/
75
public
int
ModiXMLFile(String filename, String newfilename)
{
76
int
returnValue
=
0
;
77
try
{
78
SAXReader saxReader
=
new
SAXReader();
79
Document document
=
saxReader.read(
new
File(filename));
80
/** */
/**
修改内容之一: 如果book节点中show属性的内容为yes,则修改成no
*/
81
/** */
/**
先用xpath查找对象
*/
82
List list
=
document.selectNodes(
"
/books/book/@show
"
);
83
Iterator iter
=
list.iterator();
84
while
(iter.hasNext())
{
85
Attribute attribute
=
(Attribute) iter.next();
86
if
(attribute.getValue().equals(
"
yes
"
))
{
87
attribute.setValue(
"
no
"
);
88
}
89
}
90
/** */
/**
91
* 修改内容之二: 把owner项内容改为Tshinghua
92
* 并在owner节点中加入date节点,date节点的内容为2004-09-11,还为date节点添加一个属性type
93
*/
94
list
=
document.selectNodes(
"
/books/owner
"
);
95
iter
=
list.iterator();
96
if
(iter.hasNext())
{
97
Element ownerElement
=
(Element) iter.next();
98
ownerElement.setText(
"
Tshinghua
"
);
99
Element dateElement
=
ownerElement.addElement(
"
date
"
);
100
dateElement.setText(
"
2004-09-11
"
);
101
dateElement.addAttribute(
"
type
"
,
"
Gregorian calendar
"
);
102
}
103
/** */
/**
修改内容之三: 若title内容为Dom4j Tutorials,则删除该节点
*/
104
list
=
document.selectNodes(
"
/books/book
"
);
105
iter
=
list.iterator();
106
while
(iter.hasNext())
{
107
Element bookElement
=
(Element) iter.next();
108
Iterator iterator
=
bookElement.elementIterator(
"
title
"
);
109
while
(iterator.hasNext())
{
110
Element titleElement
=
(Element) iterator.next();
111
if
(titleElement.getText().equals(
"
Dom4j Tutorials
"
))
{
112
bookElement.remove(titleElement);
113
}
114
}
115
}
116
try
{
117
/** */
/**
将document中的内容写入文件中
*/
118
XMLWriter writer
=
new
XMLWriter(
new
FileWriter(
new
File(
119
newfilename)));
120
writer.write(document);
121
writer.close();
122
/** */
/**
执行成功,需返回1
*/
123
returnValue
=
1
;
124
}
catch
(Exception ex)
{
125
ex.printStackTrace();
126
}
127
}
catch
(Exception ex)
{
128
ex.printStackTrace();
129
}
130
return
returnValue;
131
}
132
133
/** */
/**
134
* 格式化XML文档,并解决中文问题
135
*
@param
filename
136
*
@return
137
*/
138
public
int
formatXMLFile(String filename)
{
139
int
returnValue
=
0
;
140
try
{
141
SAXReader saxReader
=
new
SAXReader();
142
Document document
=
saxReader.read(
new
File(filename));
143
XMLWriter writer
=
null
;
144
/** */
/**
格式化输出,类型IE浏览一样
*/
145
OutputFormat format
=
OutputFormat.createPrettyPrint();
146
/** */
/**
指定XML编码
*/
147
format.setEncoding(
"
GBK
"
);
148
writer
=
new
XMLWriter(
new
FileWriter(
new
File(filename)), format);
149
writer.write(document);
150
writer.close();
151
/** */
/**
执行成功,需返回1
*/
152
returnValue
=
1
;
153
}
catch
(Exception ex)
{
154
ex.printStackTrace();
155
}
156
return
returnValue;
157
}
158
}
posted on 2006-09-20 15:46
Warren.Wu 阅读(148)
评论(0) 编辑 收藏 所属分类:
DOM4J