1
import java.io.*;
2
import java.util.*;
3
import javax.xml.parsers.*;
4
import org.w3c.dom.*;
5
import org.apache.crimson.tree.XmlDocument;
6
7
public class XMLDemo {
8
9
Hashtable ht;
10
11
protected void readXML(String filename)throws Exception{
12
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
13
DocumentBuilder db = null;
14
try{
15
db = dbf.newDocumentBuilder();
16
}catch(ParserConfigurationException ce){
17
System.err.println(ce);
18
System.exit(1);
19
}
20
Document doc = null;
21
try{
22
doc = db.parse(filename);
23
}catch(IOException ioe){
24
System.err.println(ioe);
25
System.exit(1);
26
}catch(DOMException e){
27
System.err.println(e.getMessage());
28
System.exit(1);
29
}
30
31
Element root = doc.getDocumentElement();
32
NodeList students = root.getElementsByTagName("學(xué)生");
33
for(int i = 0; i < students.getLength(); i++){
34
Element student = (Element)students.item(i);
35
javaxml.StudentBean sb = new javaxml.StudentBean();
36
sb.setSex(student.getAttribute("性別"));
37
NodeList name = student.getElementsByTagName("姓名");
38
if(name.getLength()==1){
39
Element e = (Element)name.item(0);
40
Text t = (Text)e.getFirstChild();
41
sb.setName(t.getNodeValue());
42
}
43
NodeList age = student.getElementsByTagName("年齡");
44
if(age.getLength()==1){
45
Element e = (Element)age.item(0);
46
Text t = (Text)e.getFirstChild();
47
sb.setAge(Integer.parseInt(t.getNodeValue()));
48
}
49
NodeList phone = student.getElementsByTagName("電話");
50
if(phone.getLength()==1){
51
Element e = (Element)phone.item(0);
52
Text t = (Text)e.getFirstChild();
53
sb.setPhone(t.getNodeValue());
54
}
55
ht.put(i, sb);
56
}
57
}
58
59
protected void writeXML(String filename) throws Exception{
60
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
61
DocumentBuilder db = null;
62
try{
63
db = dbf.newDocumentBuilder();
64
}catch(ParserConfigurationException pce){
65
System.err.println(pce);
66
System.exit(1);
67
}
68
Document doc = null;
69
try{
70
doc = db.newDocument();
71
}catch(DOMException de){
72
System.err.println(de.getMessage());
73
System.exit(1);
74
}
75
76
Element root = doc.createElement("學(xué)生花名冊(cè)");
77
doc.appendChild(root);
78
for(int i = 0; i < ht.size(); i++){
79
javaxml.StudentBean sb = (javaxml.StudentBean)ht.get(i);
80
Element student = doc.createElement("學(xué)生");
81
student.setAttribute("性別", sb.getSex());
82
root.appendChild(student);
83
Element name = doc.createElement("姓名");
84
student.appendChild(name);
85
Text t_name = doc.createTextNode(sb.getName());
86
name.appendChild(t_name);
87
Element age = doc.createElement("年齡");
88
student.appendChild(age);
89
Text t_age = doc.createTextNode(sb.getAge()+"");
90
age.appendChild(t_age);
91
Element phone = doc.createElement("電話");
92
student.appendChild(phone);
93
Text t_phone = doc.createTextNode(sb.getPhone());
94
phone.appendChild(t_phone);
95
}
96
FileOutputStream fo = new FileOutputStream(filename);
97
OutputStreamWriter osw = new OutputStreamWriter(fo);
98
((XmlDocument)doc).write(osw, "gb2312");
99
osw.close();
100
fo.close();
101
}
102
void showxml(int i){
103
//for(int i = 0; i < ht.size(); i++){
104
javaxml.StudentBean sb = (javaxml.StudentBean)ht.get(i);
105
System.out.println(sb.getName());
106
System.out.println(sb.getSex());
107
System.out.println(sb.getAge());
108
System.out.println(sb.getPhone());
109
//}
110
}
111
112
113
/**
114
* @param args
115
* @throws Exception
116
*/
117
public static void main(String[] args) throws Exception {
118
// TODO Auto-generated method stub
119
long stat = System.currentTimeMillis();
120
long end;
121
System.out.println("開(kāi)始讀取文件!");
122
XMLDemo x = new XMLDemo();
123
x.ht = new Hashtable();
124
x.readXML("Input.xml");
125
System.out.println("文件讀取完畢!\r
顯示讀取內(nèi)容
");
126
x.showxml(0);
127
System.out.println("
讀取內(nèi)容顯示完畢
\r輸出讀取內(nèi)容到OutputXmlDemo.xml");
128
x.writeXML("OutputXmlDemo.xml");
129
end = System.currentTimeMillis();
130
System.out.println("程序運(yùn)行時(shí)間:" + (end - stat));
131
}
132
133
}
134
Input.xml
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

1
<?xml version="1.0" encoding="GB2312"?>
2
3
<學(xué)生花名冊(cè)>
4
<學(xué)生 性別="男">
5
<姓名>李華 </姓名>
6
<年齡>19</年齡>
7
<電話>1234567</電話>
8
</學(xué)生>
9
<學(xué)生 性別="女">
10
<姓名>張六 </姓名>
11
<年齡>18</年齡>
12
<電話>7654321</電話>
13
</學(xué)生>
14
</學(xué)生花名冊(cè)>
其中apache.crimson可來(lái)這里下載得到
2

3

4

5

6

7

8

9

10

11

12

13

14

僅供學(xué)習(xí),有興趣大家交流下