1 Reply Latest reply: Apr 7, 2008 11:28 PM by 807601 RSS

    Delete item from my xml file

    807601
      Hi i am trying to delete a record of data from my xml file when the user clicks the delete button.

      below is the source code for my program can someone please tell me some code to this.

      package tractorgui;
      import java.awt.BorderLayout;
      import java.awt.GridLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.beans.XMLEncoder;
      import java.io.BufferedReader;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.FileReader;
      import java.beans.XMLDecoder;
      import javax.swing.*;
      import javax.swing.border.LineBorder;

      //import tractor.TextInputPrompt;
      import tractor.Tractor;

      /**
      * This code was edited or generated using CloudGarden's Jigloo
      * SWT/Swing GUI Builder, which is free for non-commercial
      * use. If Jigloo isbeing used commercially (ie, by a corporation,
      * company or business for any purpose whatever) then you
      * should purchase a license for each developer using Jigloo.
      * Please visit www.cloudgarden.com for details.
      * Use of Jigloo implies acceptance of these licensing terms.
      * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
      * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
      * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
      */

      public class NewSwingApp extends javax.swing.JFrame {

      // {
      // //Set Look & Feel
      // try {
      // javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
      // } catch(Exception e) {
      // e.printStackTrace();
      // }
      // }


      private static final long serialVersionUID = 1L;
      private JButton searchmanufacturer;
      private JButton jButton3;
      private JLabel companyname;
      private JPanel labelpannel;
      private JButton listall;
      private JPanel MenuButtons;
      private JButton archivetractor;
      private JTextArea outputscreen;
      private JButton exhibittractor;
      private JButton deletetractor;
      private JButton addtractor;
      private JButton listallexbited;
      private Tractor [ ] tractors;
      private JScrollPane jScrollPane2;
      private JScrollPane jScrollPane1;
      private int numberOfTractors;
      /**
      * Auto-generated main method to display this JFrame
      */
      public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
      public void run() {
      NewSwingApp inst = new NewSwingApp();
      inst.setLocationRelativeTo(null);
      inst.setVisible(true);
      }
      });
      }


      public NewSwingApp() {
      super();
      initGUI();
      }

      private void initGUI() {
      try {
      {
      }
      BorderLayout thisLayout = new BorderLayout();
      getContentPane().setLayout(thisLayout);
      this.setPreferredSize(new java.awt.Dimension(750, 700));
      {
      labelpannel = new JPanel();
      BorderLayout labelpannelLayout = new BorderLayout();
      getContentPane().add(labelpannel, BorderLayout.NORTH);
      labelpannel.setLayout(labelpannelLayout);
      }
      {
      jButton3 = new JButton();
      getContentPane().add(getExitsystem(), BorderLayout.SOUTH);
      jButton3.setText("Exit System");
      jButton3.setPreferredSize(new java.awt.Dimension(609, 57));
      jButton3.setBackground(new java.awt.Color(0,255,255));
      jButton3.setForeground(new java.awt.Color(0,0,0));
      jButton3.setFont(new java.awt.Font("Arial",1,24));
      jButton3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      System.exit(0);
      }
      });

      }

      {
      MenuButtons = new JPanel();
      getContentPane().add(MenuButtons, BorderLayout.WEST);
      GridLayout MenuButtonsLayout = new GridLayout(7, 1);
      MenuButtonsLayout.setColumns(1);
      MenuButtonsLayout.setRows(7);
      MenuButtonsLayout.setHgap(5);
      MenuButtonsLayout.setVgap(5);
      MenuButtons.setLayout(MenuButtonsLayout);
      MenuButtons.setPreferredSize(new java.awt.Dimension(223, 267));
      {
      listall = new JButton();
      MenuButtons.add(getListall());
      listall.setText("List All");
      listall.setBackground(new java.awt.Color(0,255,255));
      listall.setForeground(new java.awt.Color(0,0,0));
      listall.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
      listall.setFont(new java.awt.Font("Arial",2,14));
      listall.addActionListener(new ActionListener() {
      /* (non-Javadoc)
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
      */
      public void actionPerformed(ActionEvent evt) {

      String XMLFile = "tractor.xml-courseworkasignment/src";

      //** Prints the contents of my XML file
      try {
      String s;
      BufferedReader in = new BufferedReader( new FileReader(XMLFile)
      );
      outputscreen.setText("File successfully opened");

      try {
      while ( (s=in.readLine()) !=null)
      outputscreen.append(s);
      }
      catch(Exception e) {
      outputscreen.append("Error reading line: " + e.getMessage());
      }
      outputscreen.append("End of Document");
      }
      catch(FileNotFoundException e) {
      outputscreen.append("Error in opening file: " + e.getMessage());
      }

      }
      });
      }
      {
      listallexbited = new JButton();
      MenuButtons.add(getListallexbited());
      listallexbited.setText("List All Tractors On Exhibition ");
      listallexbited.setPreferredSize(new java.awt.Dimension(157, 57));
      listallexbited.setBackground(new java.awt.Color(0,255,255));
      listallexbited.setForeground(new java.awt.Color(64,0,0));
      listallexbited.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      outputscreen.repaint();
      String XMLFile = "C:tractor.xml";

      // Print contents of XML file
      try {
      String s;
      BufferedReader in = new BufferedReader( new FileReader(XMLFile)
      );
      outputscreen.setText("File successfully opened");

      try {
      while ( (s=in.readLine()) !=null)
      outputscreen.append(s);
      }
      catch(Exception e) {
      outputscreen.append("Error reading line: " + e.getMessage());
      }
      outputscreen.append("End of Document");
      }
      catch(FileNotFoundException e) {
      outputscreen.append("Error in opening file: " + e.getMessage());
      }
      }
      });
      }
      {
      addtractor = new JButton();
      MenuButtons.add(getAddtractor());
      addtractor.setText("Add Tractor ");
      addtractor.setBackground(new java.awt.Color(0,255,255));
      addtractor.setForeground(new java.awt.Color(64,0,0));
      addtractor.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      String manufacturer =JOptionPane.showInputDialog(getComponent(0), "Enter Manufacturer of Tractor");
      String shp = (JOptionPane.showInputDialog(getComponent(0), "Enter Horse Power of Tractor"));
      int hp =Integer.parseInt(shp);
      int sisRare =Integer.parseInt(JOptionPane.showInputDialog(getComponent(0), "Enter If the Tractor is rare (1=Yes/2=No)"));
      boolean isRare;
      if (sisRare== 1) {
      isRare =true;
      }else
      isRare =false;

      String yom= JOptionPane.showInputDialog(getComponent(0), "Enter Year of Manufacture");
      int yearOfManufacture =Integer.parseInt(yom);
      String yis =JOptionPane.showInputDialog(getComponent(0), "Enter Number of years the Tractor has been in service");
      int yearsInService =Integer.parseInt(yis);
      String svalue = JOptionPane.showInputDialog(getComponent(0), "Enter Tractor's Value (?Pounds)");
      double value = Double.parseDouble(svalue);
      String lastWorkPlace =JOptionPane.showInputDialog(getComponent(0), "Enter Last Workplace");
      if(NewSwingApp.addTractor(new Tractor(manufacturer, hp, isRare, yearOfManufacture, yearsInService, value, lastWorkPlace, false)))
      JOptionPane.showMessageDialog((getComponent(0)), "Tractor Added");
      else
      JOptionPane.showMessageDialog(getComponent(0), "Could not Add Tractor");
      }
      });
      }

      deletetractor = new JButton();
      MenuButtons.add(getDeletetractor());
      deletetractor.setText("Delete Tractor ");
      deletetractor.setBackground(new java.awt.Color(0,255,255));
      deletetractor.setForeground(new java.awt.Color(64,0,0));
      deletetractor.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      System.out.println("deletetractor.actionPerformed, event="+evt);
      JOptionPane.showInputDialog(getComponent(0), "Enter Tractor ID");



      }
      });
      }
      {
      exhibittractor = new JButton();
      MenuButtons.add(getExhibittractor());
      exhibittractor.setText("Exhibit Tractor");
      exhibittractor.setBackground(new java.awt.Color(0,255,255));
      exhibittractor.setForeground(new java.awt.Color(0,0,0));
      exhibittractor.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      System.out.println("exhibittractor.actionPerformed, event="+evt);
      JOptionPane.showInputDialog(getComponent(0), "Enter Tractor I.D");
      }
      });
      }
      {
      archivetractor = new JButton();
      MenuButtons.add(getArchivetractor());
      archivetractor.setText("Archive Tractor");
      archivetractor.setBackground(new java.awt.Color(0,255,255));
      archivetractor.setForeground(new java.awt.Color(0,0,0));
      archivetractor.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      System.out.println("archivetractor.actionPerformed, event="+evt);
      JOptionPane.showInputDialog(getComponent(0), "Enter Tractor I.D");
      }
      });
      }
      {
      searchmanufacturer = new JButton();
      MenuButtons.add(searchmanufacturer);
      searchmanufacturer.setText("Search Manufacturer");
      searchmanufacturer.setPreferredSize(new java.awt.Dimension(159, 21));
      searchmanufacturer.setBackground(new java.awt.Color(0,255,255));
      searchmanufacturer.setForeground(new java.awt.Color(64,0,0));
      searchmanufacturer.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      System.out.println("searchmanufacturer.actionPerformed, event="+evt);
      JOptionPane.showInputDialog(getComponent(0), "Enter Manufacturer Name");

      }
      });
      }

      {
      outputscreen = new JTextArea();
      getContentPane().add(outputscreen, BorderLayout.CENTER);
      outputscreen
      .setText("");

      outputscreen.setBorder(BorderFactory.createTitledBorder(""));
      outputscreen.setWrapStyleWord(true);
      outputscreen.setEditable(false);
      outputscreen.setEnabled(true);
      outputscreen.setBackground(new java.awt.Color(255, 255, 255));
      outputscreen.setForeground(new java.awt.Color(64, 0, 0));
      {

      }
      {

      }
      }
      {
      companyname = new JLabel();
      getContentPane().add(companyname, BorderLayout.NORTH);
      companyname.setText(" Wolvesville Tractor Museum");
      companyname.setPreferredSize(new java.awt.Dimension(609, 85));
      companyname.setBackground(new java.awt.Color(255,255,0));
      companyname.setFont(new java.awt.Font("Arial",1,28));
      companyname.setForeground(new java.awt.Color(0,0,0));
      companyname.setBorder(BorderFactory.createTitledBorder(""));
      companyname.setOpaque(true);
      }
      this.setSize(750, 750);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }


      protected static boolean addTractor(Tractor tractor) {
      if (tractor.getManufacturer()==null) return false; else
      if (tractor.getHp()<50||tractor.getHp()>1100) return false; else
      if (tractor.getIsRare()==false) return false; else
      if (tractor.getYearsInService()<1||tractor.getYearsInService()>200) return false; else
      if (tractor.getYearOfManufacture()<1800||tractor.getYearOfManufacture()>2008) return false; else
      if (tractor.getValue()<100||tractor.getValue()>1500) return false; else
      if (tractor.getLastWorkPlace()==null) return false; else


      return true;
      }

      public JPanel getMenuButtons() {
      return MenuButtons;
      }

      public JButton getListall() {
      return listall;
      }

      public JLabel getCompanyname() {
      return companyname;
      }

      public JButton getExitsystem() {
      return jButton3;
      }



      public JButton getSearchmanufacturer() {
      return searchmanufacturer;
      }

      public JButton getListallexbited() {
      return listallexbited;
      }

      public JButton getAddtractor() {
      return addtractor;
      }

      public JButton getDeletetractor() {
      return deletetractor;
      }

      public JButton getExhibittractor() {
      return exhibittractor;
      }

      public JButton getArchivetractor() {
      return archivetractor;
      }



      public JTextArea getOutputscreenx() {
      return outputscreen;

      }


      public void savetractors () {
      try {
      XMLEncoder encoder = new XMLEncoder(new FileOutputStream("tractor.xml"));
      encoder.writeObject(tractors);
      encoder.close();
      } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }
      public void loadtractors () {
      try {
      XMLDecoder decoder = new XMLDecoder(new FileInputStream("tractor.xml"));
      tractors = (Tractor[]) decoder.readObject();
      decoder.close();
      for (int i=0; i<tractors.length;i++) {
      if (tractors!=null)numberOfTractors =i;
      }
      numberOfTractors++;
      } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      tractors=new Tractor[25];
      numberOfTractors=0;
      }

      }


      }
        • 1. Re: Delete item from my xml file
          807601
          You can research java saxparser, and java domparser. They will open up the xml document and allow you to walk through the document and do things to it. I believe this is rather an advanced topic for you to handle.

          An alternative is to read the entire document (assuming its not too large) into an arrayList where each line in the file is a row in the arraylist.

          Then work down the arraylist to locate the record you want to delete and remove it.
          Example:
          Locate a line that has <record> in it.
          Locate the next line that has </record> in it.
          Delete the above two lines and anything between them.

          Note the above is not foolproof. The <record> and </record> tags may appear on the same line.

          Afer your done altering the array, write it back to the file (overwrite the previous file contents).