I have a java file which parses an XML file using SAX which seems to work fine.
The following is my SAX.java file:
public class SAX extends DefaultHandler{ private final List<Student> studentList = new ArrayList<Student>(); private String tempVal; private Student tempStudent; public void runExample(){ parseDocument(); outputList(); } private void parseDocument(){ try { // get a factory object SAXParserFactory spf = SAXParserFactory.newInstance(); // get an instance of the parser SAXParser sp = spf.newSAXParser(); // parse the XML file and register this // class for callbacks sp.parse("Students.xml", this); } catch (Exception ex) { ex.printStackTrace(); } } private void outputList(){ for(Student student : studentList){ System.out.println(student); } } // the event handlers.... @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // reset tempVal = ""; if(qName.equalsIgnoreCase("Student")){ // create a new Employee object tempStudent = new Student(); tempStudent.setTitle(attributes.getValue("Title")); } // System.out.println( // "startElement::qName is "+qName); } @Override public void characters(char []ch, int start, int length) throws SAXException{ tempVal = new String(ch, start, length); // System.out.println("tempVal is "+tempVal); } @Override public void endElement(String uri, String localname, String qName) throws SAXException { if(qName.equalsIgnoreCase("Student")){ studentList.add(tempStudent); } else if(qName.equalsIgnoreCase("Name")){ tempStudent.setName(tempVal); } else if(qName.equalsIgnoreCase("Age")){ tempStudent.setAge(Integer.parseInt(tempVal)); } else if(qName.equalsIgnoreCase("College")){ tempStudent.setCollege(tempVal); } else if(qName.equalsIgnoreCase("School")) { tempStudent.setSchool(tempVal); } } public static void main(String []args){ SAX spe = new SAX(); spe.runExample(); } } However, I have been asked to present this in a GUI. When a particular radio button is clicked and user clicks parse the XML file will be parsed using SAX and the results will be shown in a text box. I have been given the GUI, it is already coded, my issue is I have limited knowledge of GUI's and I do not have a clue how to integrate the two of them.
public void actionPerformed(ActionEvent e) { else if (e.getSource() == parseButton){ if(saxRadioButton.isSelected()){ // do SAX stuff } I'm just looking for someone to point me in the right direction here. Should I be making the SAX file separately or should I be putting straight into the If statement. I'm completely lost.
No comments:
Post a Comment