Results 1 to 6 of 6
Thread: Parsing Txt and XML files.
- 05-09-2011, 03:00 AM #1
Member
- Join Date
- May 2011
- Posts
- 9
- Rep Power
- 0
Parsing Txt and XML files.
I have a Practice project for my finals and I cannot for the life of me figure out how to get this work right. The Professor has never gone over parsing or how to do the majority of the stuff in the project. She expects us to learn from each other and the internet. Right now I have the GUI and all the of buttons to work perfectly. I however cannot get the Frame Class to work with the Calling class and I'm not even entirely sure the XML parsing code is right. The program reads the information from an Access Database originally and needs to also be able to read and write XML and TXT files. I would really appreciate it if someone could help me with my coding and explain it so I can do good on the final next week.
MailList:
MailListFrame:Java Code:// import libraries import javax.swing.*; import java.awt.*; public class MailList { public static void main(String[] args) { MailListFrame frame = new MailListFrame(); frame.setTitle("MailListFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(410, 220); frame.setVisible(true); // Get the dimension of the screen Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int screenWidth = screenSize.width; int screenHeight = screenSize.height; // Get the dimension of the frame Dimension frameSize = frame.getSize(); int x = (screenWidth - frameSize.width)/2; int y = (screenHeight - frameSize.height)/2; frame.setLocation(x, y); frame.setVisible(true); } }
ParseLib:Java Code:// imports libraries import javax.swing.JOptionPane; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; import java.io.*; import java.net.URL; import java.util.Scanner; import javax.swing.JFileChooser; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; public class MailListFrame extends JFrame implements ActionListener { // instance variables - database Statement stmt; ResultSet rs; // instance variables - visual components+ JPanel jpnlText = new JPanel(); JTextField jfldLast = new JTextField(" "); JTextField jfldFirst = new JTextField(" "); JTextField jfldStreet = new JTextField(" "); JTextField jfldCity = new JTextField(" "); JTextField jfldState = new JTextField(" "); JTextField jfldZip = new JTextField(" "); JTextField jfldPhone = new JTextField(" "); //Menu Variables JMenuBar jmb = new JMenuBar(); // declare the menubar JMenu mnuFile = new JMenu("File"); // declare the "File" Menu JMenu mnuHelp = new JMenu("Help"); // declare the "Help" Menu JMenuItem mnu1Help = new JMenuItem("Help"); // declare the "Help" Submenu JMenuItem mnu1Load = new JMenuItem("Load"); // File submenu item "Load" JMenuItem mnu1Save = new JMenuItem("Save"); JMenuItem mnu1Exit = new JMenuItem("Exit");// File submenu item "Save" //Button Variables JPanel jpnlBtn = new JPanel(); JButton jbtnPrevious = new JButton("<-Previous"); JButton jbtnNew = new JButton("New"); JButton jbtnSave = new JButton("Save "); JButton jbtnDelete = new JButton("Delete"); JButton jbtnNext = new JButton("Next->"); // Constructor for objects of class MailListFrame public MailListFrame() { // Initialize database connection and create a Statemet object initializeDB(); // Get the content pane of the frame and set Layout type Container container = getContentPane(); container.setLayout(new BorderLayout()); // pnlLabels Layout and Add Components JPanel jplbl = new JPanel(); jplbl.setLayout(new GridLayout(7,1)); jplbl.add(new JLabel("Last")); jplbl.add(new JLabel("First")); jplbl.add(new JLabel("Street")); jplbl.add(new JLabel("City")); jplbl.add(new JLabel("State")); jplbl.add(new JLabel("Zip")); jplbl.add(new JLabel("Phone")); // pnlFields Layout and Add Components JPanel jpnlText = new JPanel(); jpnlText.setLayout(new GridLayout(7,1)); jpnlText.add(jfldLast); jpnlText.add(jfldFirst); jpnlText.add(jfldStreet); jpnlText.add(jfldCity); jpnlText.add(jfldState); jpnlText.add(jfldZip); jpnlText.add(jfldPhone); // pnlButtons Layout and Add Components JPanel jpnlBtn = new JPanel(); jpnlBtn.setLayout(new FlowLayout()); jpnlBtn.add(jbtnPrevious); jpnlBtn.add(jbtnNew); jpnlBtn.add(jbtnSave); jpnlBtn.add(jbtnDelete); jpnlBtn.add(jbtnNext); // pnlMenu Layout and Add Components // add items to mnubar jmb.add(mnuFile); jmb.add(mnuHelp); // add items to mnuFile mnuFile.add(mnu1Load); mnuFile.add(mnu1Save); mnuHelp.add(mnu1Help); mnuFile.add(mnu1Exit); // add the populated menubar to "this" form this.setJMenuBar(jmb); // add event handlers to menu items mnuHelp.addActionListener(this); mnuFile.addActionListener(this); mnu1Load.addActionListener(this); mnu1Save.addActionListener(this); mnu1Help.addActionListener(this); mnu1Exit.addActionListener(this); // add event handlers to Button items jbtnPrevious.addActionListener(this); jbtnNew.addActionListener(this); jbtnSave.addActionListener(this); jbtnDelete.addActionListener(this); jbtnNext.addActionListener(this); // add panels to container container.add(jplbl, BorderLayout.WEST); container.add(jpnlBtn, BorderLayout.SOUTH); container.add(jpnlText, BorderLayout.CENTER); // register event handlers with all buttons } private void initializeDB() { Connection connection; try { // Load the JDBC driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println("Driver loaded"); // Establish a connection connection = DriverManager.getConnection ("jdbc:odbc:MailList"); System.out.println("Database connected"); // Create a statement stmt = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery("SELECT Last, First, Street, City, State, Zip, Phone FROM MailList"); rs.first(); String Last = rs.getString("Last"); String First = rs.getString("First"); String Street = rs.getString("Street"); String City = rs.getString("City"); String State = rs.getString("State"); String Zip = rs.getString("Zip"); String Phone = rs.getString("Phone"); jfldLast.setText(Last); jfldFirst.setText(First); jfldStreet.setText(Street); jfldCity.setText(City); jfldState.setText(State); jfldZip.setText(Zip); jfldPhone.setText(Phone); } catch (Exception ex) { System.out.println(ex.toString()); } } // Handler for an Action event (Component Clicked) public void actionPerformed(ActionEvent e) { //Actions for the Buttons if (e.getSource() == jbtnPrevious) jbtnPrevious_Click(); if (e.getSource() == jbtnNew) jbtnNew_Click(); if (e.getSource() == jbtnSave) jbtnSave_Click(); if (e.getSource() == jbtnDelete) jbtnDelete_Click(); if (e.getSource() == jbtnNext) jbtnNext_Click(); //Actions for the Menu Items if (e.getSource() == mnu1Save) mnu1Save_Click(); if (e.getSource() == mnu1Load) mnu1Load_Click(); if (e.getSource() == mnu1Help) mnu1Help_Click(); if (e.getSource() == mnu1Exit) mnu1Exit_Click(); } // User defined methods private void mnu1Load_Click() { try{ // instantialte the JFileChooser component JFileChooser fileChooser = new JFileChooser(); // set filters for JFileChooser fileChooser.addChoosableFileFilter(new TextFilter()); fileChooser.addChoosableFileFilter(new XMLFilter()); if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { // Get the selected file File file = fileChooser.getSelectedFile(); // Create a Scanner for the file Scanner input = new Scanner(file); // Read text from the file while (input.hasNext()) { //Reads the TxT or XML files here. /*String Last = string.String[0]; String First = rs.getString("First"); String Street = rs.getString("Street"); String City = rs.getString("City"); String State = rs.getString("State"); String Zip = rs.getString("Zip"); String Phone = rs.getString("Phone"); jfldLast.setText(Last); jfldFirst.setText(First); jfldStreet.setText(Street); jfldCity.setText(City); jfldState.setText(State); jfldZip.setText(Zip); jfldPhone.setText(Phone);*/ } // Close the file input.close(); } else { System.out.println("No file selected"); } } catch (Exception ex) { System.out.println(ex.toString()); } } /** * inner classess to create custom FileFilters */ class TextFilter extends FileFilter { public boolean accept(File f) { // needed to allow directory browsing if (f.isDirectory()) return true; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) // search-for file extension is .equals string argument if (s.substring(i + 1).toLowerCase().equals("txt")) return true; return false; } // description to display in JFileChooser combobox public String getDescription() { return "Text files (*.txt)"; } } class XMLFilter extends FileFilter { public boolean accept(File f) { // needed to allow directory browsing if (f.isDirectory()) return true; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) // search-for file extension is .equals string argument if (s.substring(i + 1).toLowerCase().equals("xml")) return true; return false; } // description to display in JFileChooser combobox public String getDescription() { return "XML files (*.xml)"; } } private void mnu1Help_Click() { JOptionPane.showMessageDialog(null, " Version 1 \n 2011 \n Created by Donavan Payne" ,"Help", JOptionPane.INFORMATION_MESSAGE); } private void mnu1Save_Click() { try{ rs.moveToCurrentRow(); rs.updateString("Last", jfldLast.getText()); rs.updateString("First", jfldFirst.getText()); rs.updateString("Street", jfldStreet.getText()); rs.updateString("City", jfldCity.getText()); rs.updateString("State", jfldState.getText()); rs.updateInt("Zip", Integer.parseInt(jfldZip.getText())); rs.updateString("Phone", jfldPhone.getText()); rs.updateRow(); rs.moveToCurrentRow(); rs.refreshRow(); } catch (Exception ex){ System.out.println("Cannot save"); } } private void mnu1Exit_Click() { System.exit(0); //JOptionPane.showMessageDialog(null, "Exit" ,"Menu Item Selected", // JOptionPane.INFORMATION_MESSAGE); } private void jbtnPrevious_Click() { try { rs.previous(); String Last = rs.getString("Last"); String First = rs.getString("First"); String Street = rs.getString("Street"); String City = rs.getString("City"); String State = rs.getString("State"); String Zip = rs.getString("Zip"); String Phone = rs.getString("Phone"); jfldLast.setText(Last); jfldFirst.setText(First); jfldStreet.setText(Street); jfldCity.setText(City); jfldState.setText(State); jfldZip.setText(Zip); jfldPhone.setText(Phone); } catch(Exception ex) { System.out.println("No Previous Files"); } } private void jbtnNew_Click() { try{ rs.last(); rs.moveToCurrentRow(); String Last = " "; String First = " "; String Street = " "; String City = " "; String State = " "; String Zip = ""; String Phone = ""; jfldLast.setText(Last); jfldFirst.setText(First); jfldStreet.setText(Street); jfldCity.setText(City); jfldState.setText(State); jfldZip.setText(Zip); jfldPhone.setText(Phone); rs.updateString("Last", jfldLast.getText()); rs.updateString("First", jfldFirst.getText()); rs.updateString("Street", jfldStreet.getText()); rs.updateString("City", jfldCity.getText()); rs.updateString("State", jfldState.getText()); rs.updateInt("Zip", Integer.parseInt(jfldZip.getText())); rs.updateString("Phone", jfldPhone.getText()); rs.insertRow(); rs.moveToCurrentRow(); rs.refreshRow(); } catch(Exception ex) { System.out.println("Cannot create a new row"); } } private void jbtnDelete_Click() { try{ rs.deleteRow(); rs.previous(); } catch(Exception ex) { System.out.println("Cannot delete row"); } } private void jbtnSave_Click() { try{ /*String Last ; String First ; String Street; String City ; String State ; String Zip ; String Phone;*/ rs.moveToCurrentRow(); rs.updateString("Last", jfldLast.getText()); rs.updateString("First", jfldFirst.getText()); rs.updateString("Street", jfldStreet.getText()); rs.updateString("City", jfldCity.getText()); rs.updateString("State", jfldState.getText()); rs.updateInt("Zip", Integer.parseInt(jfldZip.getText())); rs.updateString("Phone", jfldPhone.getText()); rs.updateRow(); rs.moveToCurrentRow(); rs.refreshRow(); } catch (Exception ex){ System.out.println("Cannot save"); } } private void jbtnNext_Click() { try { rs.next(); String Last = rs.getString("Last"); String First = rs.getString("First"); String Street = rs.getString("Street"); String City = rs.getString("City"); String State = rs.getString("State"); String Zip = rs.getString("Zip"); String Phone = rs.getString("Phone"); jfldLast.setText(Last); jfldFirst.setText(First); jfldStreet.setText(Street); jfldCity.setText(City); jfldState.setText(State); jfldZip.setText(Zip); jfldPhone.setText(Phone); } catch(Exception ex) { System.out.println("No More Files"); } } }
Java Code:// contains generalized code for parsing (and assembling) recods for comma delimited and xml files //import libraries import java.util.*; import java.io.*; public class ParseLib { //declare properties and variables to be used in class private static ArrayList lines = new ArrayList(); private static String inputLines = ""; public ParseLib() { } // returns an array of field values parsed from a comma delimited file (method) public static String[] parseString(ArrayList strvar, String delimiter, int index) throws Exception { //sets up an array and get size of arraylist String []recordsarray = new String[strvar.size()]; //sets the arraylist to an array strvar.toArray(recordsarray); //creates a string String single; //sets the string to array with an index of 0 single = recordsarray[index]; //creates an array String []newstring = new String[7]; //sets new array to an array without the , newstring = single.split(","); return newstring; } //creates a method to read in the file given and adds it to an array public static ArrayList readcommadata(String filename) throws Exception { //scans in the given file Scanner scan = new Scanner(new File(filename)); //sets an int to 0 int i=0; //while the scanner reads lines while (scan.hasNext()){ //sets arraylist to lines and nextlines lines.add(scan.nextLine()); //increments i++; } //close scanner scan.close(); //returns an arraylist return lines; } // returns an array of field values parsed from a record in an xml file public static String[] parsexml(ArrayList strvar, String delimiter, int index) throws Exception { //creates an array and sets it to arraylist and its size String []recordsarray = new String[strvar.size()]; //turns arraylist to array strvar.toArray(recordsarray); //creates a string String single1; //sets string of delimiters String delims = "< >=\""; //sets string to array with an index single1 = recordsarray[index]; //creates new string array String []newstring = new String[7]; //sets string array to string with delimiters taken out newstring = single1.split(delims); //System.out.println(newstring); //returns array return newstring; } } //}
- 05-09-2011, 03:40 AM #2
tl;dr
Recommended reading:
How to ask questions the smart way
SSCCE
db
- 05-09-2011, 03:47 AM #3
Member
- Join Date
- May 2011
- Posts
- 9
- Rep Power
- 0
How is it too long? I have a simple paragraph and all of my code.
- 05-09-2011, 04:17 AM #4
-
It may be simple for you, but for us we have to go through it line by line trying to understand all the assumptions that you already know. We're all volunteers with our own work, family, and life responsibilities and our time is precious, and for a volunteer help forum, it is too much code, believe me. If you need our free help, we'll be happy to give it, but we would appreciate it if you put in the effort to make it easy for us to help you. Consider creating and posting an SSCCE
- 05-09-2011, 04:38 AM #6
Member
- Join Date
- May 2011
- Posts
- 9
- Rep Power
- 0
Sorry, Let me try to make this simpler.
I need the array in ParseLib:
To go to the Frame Class and have the Load menu button read an XML or TXT file (Only gave XML Code of ParseLib) and go into the text fields I have set up.Java Code:public static String[] parseString(ArrayList strvar, String delimiter, int index) throws Exception { //sets up an array and get size of arraylist String []recordsarray = new String[strvar.size()]; //sets the arraylist to an array strvar.toArray(recordsarray); //creates a string String single; //sets the string to array with an index of 0 single = recordsarray[index]; //creates an array String []newstring = new String[7]; //sets new array to an array without the , newstring = single.split(","); return newstring; } }
Java Code:private void mnu1Load_Click() { try{ // instantialte the JFileChooser component JFileChooser fileChooser = new JFileChooser(); // set filters for JFileChooser fileChooser.addChoosableFileFilter(new TextFilter()); fileChooser.addChoosableFileFilter(new XMLFilter()); if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { // Get the selected file File file = fileChooser.getSelectedFile(); // Create a Scanner for the file Scanner input = new Scanner(file); // Read text from the file while (input.hasNext()) { //Reads the TxT or XML files here. String Last = String First = String Street = String City = String State = String Zip = String Phone = jfldLast.setText(Last); jfldFirst.setText(First); jfldStreet.setText(Street); jfldCity.setText(City); jfldState.setText(State); jfldZip.setText(Zip); jfldPhone.setText(Phone);*/ } // Close the file input.close(); } else { System.out.println("No file selected"); }
Similar Threads
-
Parsing lnk files in windows
By raysod in forum Advanced JavaReplies: 2Last Post: 11-05-2010, 06:39 PM -
Parsing Text Files
By coder09 in forum New To JavaReplies: 6Last Post: 02-18-2009, 12:08 PM -
Parsing MIDI files
By rsk8332 in forum Advanced JavaReplies: 1Last Post: 01-21-2008, 10:43 PM -
packages for parsing docs files
By gabriel in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks