|
Posted on 2006-12-04 14:16 ljf2999 阅读(105) 评论(0) 编辑 收藏 所属分类: JDOM.DOM
1
/**/
/*
2
* NewJFrame.java
3
*
4
* Created on December 1, 2006, 12:03 PM
5
*/
6
7
package
jdom;
8
9
import
java.awt.Dimension;
10
import
java.awt.Toolkit;
11
import
java.awt.event.ActionEvent;
12
import
java.awt.event.ActionListener;
13
import
java.io.BufferedReader;
14
import
java.io.BufferedWriter;
15
import
java.io.File;
16
import
java.io.FileInputStream;
17
import
java.io.FileReader;
18
import
java.io.FileWriter;
19
import
java.util.List;
20
import
javax.swing.JFileChooser;
21
import
javax.swing.JOptionPane;
22
import
javax.swing.filechooser.FileFilter;
23
import
org.jdom.
*
;
24
import
org.jdom.input.
*
;
25
26
/** */
/**
27
*
28
*
@author
training
29
*/
30
public
class
NewJFrame
extends
javax.swing.JFrame
{
31
32
/** */
/**
Creates new form NewJFrame
*/
33
private
String xmlpath
=
null
;
34
35
public
NewJFrame()
{
36
initComponents();
37
Dimension d
=
Toolkit.getDefaultToolkit().getScreenSize();
38
int
x
=
(
int
)(d.getWidth()
-
this
.getWidth())
/
2
;
39
int
y
=
(
int
)(d.getHeight()
-
this
.getHeight())
/
2
;
40
this
.setLocation(x,y);
41
}
42
43
/** */
/**
This method is called from within the constructor to
44
* initialize the form.
45
* WARNING: Do NOT modify this code. The content of this method is
46
* always regenerated by the Form Editor.
47
*/
48
//
<editor-fold defaultstate="collapsed" desc=" Generated Code ">
49
private
void
initComponents()
{
50
jLabel1
=
new
javax.swing.JLabel();
51
dir_txt
=
new
javax.swing.JTextField();
52
jButton1
=
new
javax.swing.JButton();
53
jScrollPane1
=
new
javax.swing.JScrollPane();
54
xml_txtarea
=
new
javax.swing.JTextArea();
55
jLabel2
=
new
javax.swing.JLabel();
56
jScrollPane2
=
new
javax.swing.JScrollPane();
57
txt_txtarea
=
new
javax.swing.JTextArea();
58
jLabel3
=
new
javax.swing.JLabel();
59
jButton2
=
new
javax.swing.JButton();
60
jButton3
=
new
javax.swing.JButton();
61
62
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
63
setTitle(
"
xmlreader
"
);
64
setCursor(
new
java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
65
setResizable(
false
);
66
jLabel1.setText(
"
xml file :
"
);
67
68
dir_txt.setName(
""
);
69
dir_txt.getAccessibleContext().setAccessibleName(
""
);
70
71
jButton1.setText(
"
"
);
72
jButton1.addActionListener(
new
java.awt.event.ActionListener()
{
73
public
void
actionPerformed(java.awt.event.ActionEvent evt)
{
74
jButton1ActionPerformed(evt);
75
}
76
}
);
77
78
xml_txtarea.setColumns(
20
);
79
xml_txtarea.setEditable(
false
);
80
xml_txtarea.setRows(
5
);
81
jScrollPane1.setViewportView(xml_txtarea);
82
83
jLabel2.setText(
"
jdom:
"
);
84
85
txt_txtarea.setColumns(
20
);
86
txt_txtarea.setEditable(
false
);
87
txt_txtarea.setRows(
5
);
88
jScrollPane2.setViewportView(txt_txtarea);
89
90
jLabel3.setText(
"
io:
"
);
91
92
jButton2.setText(
"
to xml
"
);
93
jButton2.addActionListener(
new
java.awt.event.ActionListener()
{
94
public
void
actionPerformed(java.awt.event.ActionEvent evt)
{
95
jButton2ActionPerformed(evt);
96
}
97
}
);
98
99
jButton3.setText(
"
to txt
"
);
100
jButton3.addActionListener(
new
java.awt.event.ActionListener()
{
101
public
void
actionPerformed(java.awt.event.ActionEvent evt)
{
102
jButton3ActionPerformed(evt);
103
}
104
}
);
105
106
org.jdesktop.layout.GroupLayout layout
=
new
org.jdesktop.layout.GroupLayout(getContentPane());
107
getContentPane().setLayout(layout);
108
layout.setHorizontalGroup(
109
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
110
.add(layout.createSequentialGroup()
111
.addContainerGap()
112
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
113
.add(layout.createSequentialGroup()
114
.add(jLabel1)
115
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
116
.add(dir_txt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
373
, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
117
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED,
15
, Short.MAX_VALUE)
118
.add(jButton1)
119
.add(
25
,
25
,
25
))
120
.add(layout.createSequentialGroup()
121
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
122
.add(jLabel2)
123
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
233
, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
124
.add(jButton2))
125
.add(
22
,
22
,
22
)
126
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
127
.add(jButton3)
128
.add(jLabel3)
129
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
237
, Short.MAX_VALUE))
130
.addContainerGap())))
131
);
132
layout.setVerticalGroup(
133
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
134
.add(layout.createSequentialGroup()
135
.addContainerGap()
136
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
137
.add(jLabel1)
138
.add(jButton1)
139
.add(dir_txt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
140
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED,
20
, Short.MAX_VALUE)
141
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
142
.add(jButton2)
143
.add(jButton3))
144
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
145
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
146
.add(jLabel2)
147
.add(jLabel3))
148
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
149
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING,
false
)
150
.add(jScrollPane2)
151
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
264
, Short.MAX_VALUE))
152
.addContainerGap())
153
);
154
pack();
155
}
//
</editor-fold>
156
157
private
void
jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
158
//
TODO add your handling code here:
159
if
(xmlpath
!=
null
&&
xmlpath.length()
!=
0
)
{
160
File f
=
new
File(xmlpath);
161
boolean
b
=
f.renameTo(
new
File(xmlpath.substring(
0
,xmlpath.length()
-
3
)
+
"
txt
"
));
162
JOptionPane jo
=
new
JOptionPane();
163
if
(b)
{
164
jo.showMessageDialog(
this
,
"
to txt is successful !!
"
);
165
166
//
167
String newstr
=
new
String(),endstr
=
new
String();
168
try
{
169
FileReader fr
=
new
FileReader(f.getAbsolutePath());
170
BufferedReader br
=
new
BufferedReader(fr);
171
while
(br.readLine()
!=
null
)
{
172
newstr
=
br.readLine();
173
endstr
=
endstr
+
"
\n
"
+
newstr;
174
}
175
}
catch
(Exception e)
{
176
e.printStackTrace();
177
}
178
179
txt_txtarea.append(endstr);
180
}
else
{
181
jo.showMessageDialog(
this
,
"
to txt is fail !!
"
);
182
}
183
}
184
}
185
186
private
void
jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
187
//
TODO add your handling code here:
188
if
(xmlpath
!=
null
&&
xmlpath.length()
!=
0
)
{
189
File f
=
new
File(xmlpath);
190
boolean
b
=
f.renameTo(
new
File(xmlpath.substring(
0
,xmlpath.length()
-
3
)
+
"
xml
"
));
191
JOptionPane jo
=
new
JOptionPane();
192
if
(b)
{
193
jo.showMessageDialog(
this
,
"
to xml is successful !!
"
);
194
195
196
//
197
SAXBuilder saxb
=
new
SAXBuilder();
198
StringBuffer str
=
new
StringBuffer();
199
try
{
200
Document doc
=
saxb.build(
new
FileInputStream(xmlpath));
201
Element root
=
doc.getRootElement();
202
str.append(
"
<
"
+
root.getName()
+
"
>
"
);
203
str.append(
this
.read(root));
204
str.append(
"
\n
"
);
205
str.append(
"
</
"
+
root.getName()
+
"
>\n
"
+
206
"
---------------------------------------------\n
"
);
207
208
}
catch
(Exception e)
{
209
e.printStackTrace();
210
}
211
if
(str
!=
null
&&
str.length()
!=
0
)
212
xml_txtarea.append(str.toString());
213
}
else
{
214
jo.showMessageDialog(
this
,
"
to xml is fail !!
"
);
215
}
216
}
217
}
218
219
private
void
jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
220
//
TODO add your handling code here:
221
JFileChooser jfc
=
new
JFileChooser();
222
jfc.setFileFilter(
new
myFilter());
223
jfc.showOpenDialog(
this
);
224
if
(jfc.getSelectedFile()
!=
null
)
{
225
xmlpath
=
jfc.getSelectedFile().getAbsolutePath().toString();
226
this
.dir_txt.setText(xmlpath);
227
//
jfc.addActionListener(new myjfc(jfc));
228
229
/**/
/*
230
File fread=new File(xmlpath);
231
File fwrite=new File(xmlpath.substring(xmlpath.length()-3,xmlpath.length()-1)+"txt");
232
try{
233
BufferedReader br=new BufferedReader(new FileReader(fread));
234
BufferedWriter bw=new BufferedWriter(new FileWriter(fwrite));
235
bw.close();
236
br.close();
237
}catch(Exception e){
238
e.printStackTrace();
239
}
240
241
*/
//
242
}
243
}
244
245
private
StringBuffer read(Element e)
{
246
StringBuffer s
=
new
StringBuffer();
247
List list
=
e.getChildren();
248
for
(
int
i
=
0
;i
<
list.size();i
++
)
{
249
Element ee
=
(Element)list.get(i);
250
if
(ee.getChildren().size()
!=
0
)
{
251
s.append(
"
\n<
"
+
ee.getName()
+
"
>\n
"
);
252
s.append(
this
.read(ee));
253
s.append(
"
</
"
+
ee.getName()
+
"
>\n
"
);
254
}
else
{
255
s.append(
"
\t<
"
+
ee.getName()
+
"
>
"
+
ee.getText()
+
"
</
"
+
ee.getName()
+
"
>\n
"
);
256
}
257
}
258
return
s;
259
}
260
261
/**/
/*
262
private class myjfc implements ActionListener{
263
private JFileChooser j=null;
264
public myjfc(JFileChooser jfc){
265
this.j=jfc;
266
}
267
public void actionPerformed(ActionEvent e){
268
if(e.equals("Open")){
269
if(j.getName()!=null)
270
dir_txt.setText(j.getName().toString());
271
}
272
}
273
}
274
*/
275
276
class
myFilter
extends
FileFilter
{
277
public
boolean
accept(File f)
{
278
if
(f.isDirectory()
||
f.getName().toUpperCase().endsWith(
"
.XML
"
)
||
f.getName().toUpperCase().endsWith(
"
.TXT
"
))
{
279
return
true
;
280
}
else
281
return
false
;
282
}
283
284
public
String getDescription()
{
285
return
""
;
286
}
287
}
288
289
/** */
/**
290
*
@param
args the command line arguments
291
*/
292
public
static
void
main(String args[])
{
293
java.awt.EventQueue.invokeLater(
new
Runnable()
{
294
public
void
run()
{
295
new
NewJFrame().setVisible(
true
);
296
}
297
}
);
298
}
299
300
//
Variables declaration - do not modify
301
private
javax.swing.JTextField dir_txt;
302
private
javax.swing.JButton jButton1;
303
private
javax.swing.JButton jButton2;
304
private
javax.swing.JButton jButton3;
305
private
javax.swing.JLabel jLabel1;
306
private
javax.swing.JLabel jLabel2;
307
private
javax.swing.JLabel jLabel3;
308
private
javax.swing.JScrollPane jScrollPane1;
309
private
javax.swing.JScrollPane jScrollPane2;
310
private
javax.swing.JTextArea txt_txtarea;
311
private
javax.swing.JTextArea xml_txtarea;
312
//
End of variables declaration
313
314
}
315
|