0 Replies Latest reply: Jul 28, 2013 4:43 AM by 40c32b5e-69da-4cfc-bc0b-75a988b92948 RSS

    Multiple Selection in XML files

    40c32b5e-69da-4cfc-bc0b-75a988b92948

      i have been having issue getting this code to work for multiple files. it does work for one xml file but i want it to work for multiple files can anybody help out

      here is the code:

       

      import java.sql.*;

      import java.awt.*;

      import javax.swing.*;   

      import java.awt.event.*;

      import javax.xml.parsers.DocumentBuilderFactory;

      import javax.xml.parsers.DocumentBuilder;

      import org.w3c.dom.Document;

      import org.w3c.dom.NodeList;

      import org.w3c.dom.Node;

      import org.w3c.dom.Element;

      import java.io.*;

      import java.util.*;

      import java.text.*;

      public class XMLExtractorPanel extends JPanel implements ActionListener

      {

          JButton button, cButton, exitButton;

          JTextField field;

          JPanel buttonFieldPanel;

          Connection con;

          PreparedStatement stat;

       

          JPanel imagePanel;

          JPanel exitPanel;

       

          java.sql.Date sqlDate;

       

          public XMLExtractorPanel()

          {

              this.setLayout(new BorderLayout());

       

              imagePanel = new JPanel();

              JLabel consult = new JLabel();

              consult.setIcon(new ImageIcon("images/t.jpg"));

              JLabel unity = new JLabel();

              unity.setIcon(new ImageIcon("images/y.jpg"));

              imagePanel.add(unity);

              imagePanel.add(consult);

              add(imagePanel, BorderLayout.PAGE_START);

       

              buttonFieldPanel = new JPanel();

              button = new JButton("");

              field = new JTextField(10);

       

              field.setBounds(30, 50, 200, 25);

              button.setBounds(240, 50, 100, 25);

              buttonFieldPanel.add(field);

              buttonFieldPanel.add(button);

             

              add(buttonFieldPanel, BorderLayout.CENTER);

       

              button.addActionListener(new ActionListener()

              {

                  public void actionPerformed(ActionEvent e)

                  {

                      Chooser panel = new Chooser();

                      field.setText(panel.fileName);

                  }

              });

       

              JPanel cpanel = new JPanel();

              cButton = new JButton("CLICK TO SAVE DATA");

              cButton.addActionListener(this);

              cpanel.add(cButton);

              add(cpanel, BorderLayout.CENTER);

       

              exitPanel = new JPanel();

              exitButton = new JButton("EXIT");

              exitButton.setBounds(240, 50, 100, 25);

              exitButton.addActionListener(new ActionListener()

              {

                  public void actionPerformed(ActionEvent e)

                  {

                      System.exit(0);

                  }

              });

              exitPanel.add(exitButton);

              add(exitPanel, BorderLayout.PAGE_END);

          }

          public void actionPerformed(ActionEvent e)

          {

              cButton = (JButton)e.getSource();    

                  getOperation();

          }

          public void getOperation()

              {      

              Chooser panel = new Chooser();

       

                  try

              {

                  ArrayList timeStamp = new ArrayList();

                       ArrayList Y = new ArrayList();

                   File fXmlFile = new File(panel.fileName);

                  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

                  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

                             

                  Document doc = dBuilder.parse(fXmlFile);

                  

                  doc.getDocumentElement().normalize();

       

                  BufferedReader rd = new BufferedReader(new FileReader(fXmlFile));

       

                          String fileUpload = "";

                  do

                  {

       

                      field.setText(fileUpload);

          

                      System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

       

                      NodeList nList = doc.getElementsByTagName("Item");

       

                      System.out.println("----------------------------");

       

                      for (int temp = 0; temp < nList.getLength(); temp++)

                      {

          

                          Node nNode = nList.item(temp);

       

                          System.out.println("\nCurrent Element :" + nNode.getNodeName());

       

                          if (nNode.getNodeType() == Node.ELEMENT_NODE)

                          {

       

                               Calendar cal = Calendar.getInstance();

                              Format formatter = new SimpleDateFormat("dd/MM/yyyy");

                              String creationDate = formatter.format(new java.sql.Date(cal.getTimeInMillis()));

                              Element eElement = (Element) nNode;

                         

                             

                              double transCode =  Double.parseDouble(eElement.getAttribute("TransCode"));

                              double serialNo = Double.parseDouble(eElement.getAttribute("SerialNo"));

                              double amount = Double.parseDouble(eElement.getAttribute("Amount"));

                             

                             

       

                              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                                      Connection con = DriverManager.getConnection("jdbc:odbc:DataSource", "", "");

                              stat = con.prepareStatement("INSERT INTO ExcelTable(TransCode, SerialNo, Amount) VALUES(?,?,?);");

         

                         

                             

                              stat.setDouble(4, transCode);

                              stat.setDouble(5, serialNo);

                              stat.setDouble(6, amount);

                             

                              stat.executeUpdate();

                              con.close();

                         

                             

                          }

                      }

                      //JOptionPane.showMessageDialog(null, "Data Recorded!", "Data Entry", JOptionPane.INFORMATION_MESSAGE);

                  }while((fileUpload = rd.readLine()) != null); JOptionPane.showMessageDialog(null, "Data Recorded!", "Data Entry", JOptionPane.INFORMATION_MESSAGE);

                  }

                  catch(SQLException ex)

                  {

                      System.out.println("SQLException");

                      ex.printStackTrace();

                      JOptionPane.showMessageDialog(null, "Recorded Altered!", "Data Entry", JOptionPane.WARNING_MESSAGE);

                  }

                  catch(NumberFormatException ex)

                  {   

                      System.out.println("NumberFormatException");

                      System.out.println(ex);

                      JOptionPane.showMessageDialog(null, "Recorded Altered!", "Data Entry", JOptionPane.WARNING_MESSAGE);

                  }

                  catch(Exception e)   

                  {

                      System.out.println("Error1 "+ e);

                      JOptionPane.showMessageDialog(null, "Cannot Record Data!", "Data Entry", JOptionPane.WARNING_MESSAGE);

                  }

                  if(con != null)

                  {

                      try

                      {

                          con.close();

                      }

                      catch(SQLException sqle)

                      {

                          System.err.println(sqle.getMessage());

                          JOptionPane.showMessageDialog(null, "Cannot Record Data!", "Data Entry", JOptionPane.WARNING_MESSAGE);

                      }

                  } 

              }

         

      }

      class Chooser extends JPanel

      {

          JFileChooser chooser;

          String fileName;

       

          public Chooser()

          {

              chooser = new JFileChooser();

              chooser.setMultiSelectionEnabled(true);

              int r = chooser.showOpenDialog(new JFrame());

              if (r == JFileChooser.APPROVE_OPTION)

              {

                  fileName = chooser.getSelectedFile().getPath();

              }

             

          }

      }