posts - 2, comments - 1, trackbacks - 0, articles - 9
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

JDOM:test1.0(NetBeans5.5)

Posted on 2006-12-01 16:07 ljf2999 阅读(215) 评论(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.File;
 14 import  java.io.FileInputStream;
 15 import  java.util.List;
 16 import  javax.swing.JFileChooser;
 17 import  javax.swing.filechooser.FileFilter;
 18 import  org.jdom. * ;
 19 import  org.jdom.input. * ;
 20
 21 /**
 22  *
 23  *  @author   training
 24   */

 25 public   class  NewJFrame  extends  javax.swing.JFrame  {
 26     
 27      /**  Creates new form NewJFrame  */
 28      private  String xmlpath = null ;
 29     
 30      public  NewJFrame()  {
 31         initComponents();
 32         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
 33          int  x = ( int )(d.getWidth() - this .getWidth()) / 2 ;
 34          int  y = ( int )(d.getHeight() - this .getHeight()) / 2 ;
 35          this .setLocation(x,y);
 36     }

 37     
 38      /**  This method is called from within the constructor to
 39      * initialize the form.
 40      * WARNING: Do NOT modify this code. The content of this method is
 41      * always regenerated by the Form Editor.
 42       */

 43      //  <editor-fold defaultstate="collapsed" desc=" Generated Code "> // GEN-BEGIN:initComponents
 44      private   void  initComponents()  {
 45         jLabel1  =   new  javax.swing.JLabel();
 46         dir_txt  =   new  javax.swing.JTextField();
 47         jButton1  =   new  javax.swing.JButton();
 48         jScrollPane1  =   new  javax.swing.JScrollPane();
 49         xml_txtarea  =   new  javax.swing.JTextArea();
 50
 51         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 52         setTitle( " xmlreader " );
 53         setCursor( new  java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
 54         setResizable( false );
 55         jLabel1.setText( " xml file : " );
 56
 57         dir_txt.setName( "" );
 58         dir_txt.getAccessibleContext().setAccessibleName( "" );
 59
 60         jButton1.setText( " " );
 61         jButton1.addActionListener( new  java.awt.event.ActionListener()  {
 62              public   void  actionPerformed(java.awt.event.ActionEvent evt)  {
 63                 jButton1ActionPerformed(evt);
 64             }

 65         }
);
 66
 67         xml_txtarea.setColumns( 20 );
 68         xml_txtarea.setRows( 5 );
 69         jScrollPane1.setViewportView(xml_txtarea);
 70
 71         org.jdesktop.layout.GroupLayout layout  =   new  org.jdesktop.layout.GroupLayout(getContentPane());
 72         getContentPane().setLayout(layout);
 73         layout.setHorizontalGroup(
 74             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 75             .add(layout.createSequentialGroup()
 76                 .addContainerGap()
 77                 .add(jLabel1)
 78                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 79                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING,  false )
 80                     .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,  392 , Short.MAX_VALUE)
 81                     .add(org.jdesktop.layout.GroupLayout.TRAILING, dir_txt))
 82                 .add( 11 11 11 )
 83                 .add(jButton1)
 84                 .addContainerGap())
 85         );
 86         layout.setVerticalGroup(
 87             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 88             .add(layout.createSequentialGroup()
 89                 .addContainerGap()
 90                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
 91                     .add(jLabel1)
 92                     .add(jButton1)
 93                     .add(dir_txt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
 94                 .add( 17 17 17 )
 95                 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,  253 , org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
 96                 .addContainerGap( 23 , Short.MAX_VALUE))
 97         );
 98         pack();
 99     }
//  </editor-fold> // GEN-END:initComponents
100     
101      private   void  jButton1ActionPerformed(java.awt.event.ActionEvent evt)  { // GEN-FIRST:event_jButton1ActionPerformed
102 //  TODO add your handling code here:
103         JFileChooser jfc = new  JFileChooser();
104         jfc.setFileFilter( new  myFilter());
105         jfc.showOpenDialog( this );
106          if (jfc.getSelectedFile() != null ) {
107             xmlpath = jfc.getSelectedFile().getAbsolutePath().toString();
108              this .dir_txt.setText(xmlpath);
109         }

110          //  jfc.addActionListener(new myjfc(jfc));
111         
112         SAXBuilder saxb = new  SAXBuilder();
113         StringBuffer str = new  StringBuffer();
114          try {
115             Document doc = saxb.build( new  FileInputStream(xmlpath));
116             Element root = doc.getRootElement();
117             str.append( " < " + root.getName() + " > " );
118             str.append( this .read(root));
119             str.append( " \n " );
120             str.append( " </ " + root.getName() + " >\n "   +
121                      " ---------------------------------------------\n " );
122             
123         }
catch (Exception e) {
124             e.printStackTrace();
125         }

126         xml_txtarea.append(str.toString());
127     }
// GEN-LAST:event_jButton1ActionPerformed
128     
129      private  StringBuffer read(Element e) {
130         StringBuffer s = new  StringBuffer();
131         List list = e.getChildren();
132          for ( int  i = 0 ;i < list.size();i ++ ) {
133             Element ee = (Element)list.get(i);
134              if (ee.getChildren().size() != 0 ) {
135                 s.append( " \n< " + ee.getName() + " >\n " );
136                 s.append( this .read(ee));
137                 s.append( " </ " + ee.getName() + " >\n " );
138             }
  else {
139                 s.append( " \t< " + ee.getName() + " > " + ee.getText() + " </ " + ee.getName() + " >\n " );
140             }

141         }

142          return  s;
143     }

144     
145      /*
146     private class myjfc implements ActionListener{
147         private JFileChooser j=null;
148         public myjfc(JFileChooser jfc){
149             this.j=jfc;
150         }
151         public void actionPerformed(ActionEvent e){
152             if(e.equals("Open")){
153                 if(j.getName()!=null)
154                     dir_txt.setText(j.getName().toString());
155             }
156         }
157     }
158       */

159     
160      class  myFilter  extends  FileFilter {
161          public   boolean  accept(File f) {
162              if (f.isDirectory() || f.getName().endsWith( " .xml " ) || f.getName().endsWith( " .XML " )) {
163                  return   true ;
164             }
else
165                  return   false ;
166         }

167         
168          public  String getDescription()  {
169              return   "" ;
170         }

171     }

172     
173      /**
174      *  @param  args the command line arguments
175       */

176      public   static   void  main(String args[])  {
177         java.awt.EventQueue.invokeLater( new  Runnable()  {
178              public   void  run()  {
179                  new  NewJFrame().setVisible( true );
180             }

181         }
);
182     }

183     
184      //  Variables declaration - do not modify // GEN-BEGIN:variables
185      private  javax.swing.JTextField dir_txt;
186      private  javax.swing.JButton jButton1;
187      private  javax.swing.JLabel jLabel1;
188      private  javax.swing.JScrollPane jScrollPane1;
189      private  javax.swing.JTextArea xml_txtarea;
190      //  End of variables declaration // GEN-END:variables
191     
192 }

193

只有注册用户登录后才能发表评论。


网站导航: