Alright, so I'm suppose to be using SAXparser to parse a XML file and output certain parts

The assignment asks us to use to classes Course.java and TextBook.java to which we are suppose to create the Course and Textbook objects described in the XML file, making sure they refer to each other appropriately.

Also we have to write a small Main class to
- Asks the user for the name of the input file
- Parses the file by calling the SAXParser framework appropriately
- Asks the user for a course number
- Outputs the information available through that course object's toString method (i.e., its number, the books it uses, and its prerequisites together with their number, prereqs, and books)

Here is the XML file

<xml version="1.0" encoding="UTF-8"> 
<course number="CSC212"> 
	<textbook name="BookFor212"/> 
</course> 
<course number="CSC241"> 
        <prereq number="CSC212"/> 
	<textbook name="BookFor241"/> 
	<textbook name="AnotherBookFor241"/> 
</course> 
<course number="CSC455"> 
        <prereq number="CSC241"/> 
        <prereq number="CSC365"/> 
	<textbook name="BookFor455"/> 
</course> 
<course number="CSC365"> 
        <prereq number="CSC241"/> 
	<textbook name="BookFor365"/> 
	<textbook name="AnotherBookFor365"/> 
	<textbook name="YetAnotherBookFor365"/> 
</course> 
</xml>

Here is the Course.java file

public class Course {
  private String number;
  private Course[] prereqs = new Course[5];
  private int pCounter = 0;
  private Textbook[] books = new Textbook[5];
  private int tCounter = 0;

  public Course(String n) {
    number = n;
  }

  public String getNumber() {
    return number;
  }

  public void addPrereq(Course prereq) {
    prereqs[pCounter++] = prereq;
  }

  public void addTextbook(Textbook book) {
    books[tCounter++] = book;
  }

  public String toString() {
    String retVal = "Course: " + number + (tCounter == 0 ? ", No books." : ", textbooks: ");
    for (int i = 0; i < tCounter; i++)
      retVal += books[i].toString() + " ";
    retVal += "\n" + (pCounter == 0 ? "No prerequisites.\n" : "Prerequisites:\n");
    for (int i = 0; i < pCounter; i++)
      retVal += prereqs[i].toString();
    return retVal + "End of course " + number + ".\n";
  }
}

and lastly the Textbook.java file

public class Textbook {
  private String name;

  public Textbook(String n) {
    name = n;
  }
  
  public String toString() {
    return name;
  }
}

Now here is my Main class so far

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.Scanner;
import java.io.*;
 
public class Main {
  public static void main(String[] a) {
	  Scanner scanner = new Scanner(System.in);
	  System.out.print("Please enter the name of the input file: ");
      String XmlFile = scanner.nextLine();
      File file = new File(XmlFile);
      System.out.print("Please enter a course number: ");
      String number = scanner.nextLine();
      MyHandler mh;
    
    try {
      if (file.exists()){
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser saxParser = factory.newSAXParser();
      mh = new MyHandler(); 
      saxParser.parse(XmlFile, mh);
      
      
      } 
       else {
        System.out.println("Sorry, the file was not found!");
      }
    }
    catch (Exception whatever) {
      throw new RuntimeException(whatever);
    }

  }


}

My MyHandler.java file

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;

public class MyHandler extends DefaultHandler {

	
	
  public void startElement(String s1, String s2, String tagName, Attributes attr) {
if (tagName.equals("course")) {
            int length = attr.getLength();
            for (int i=0; i<length; i++) {
                String value = attr.getValue(i);
                System.out.println(value);            
            }
        } 
	
		}
  


}

Now here is where i'm having problems actually having problems. I am having trouble taking the user input for which course they need to use and looking for it after I parse the XML file. I think I also am having trouble looking through the nested tags correctly. Also, if anyone can better explain how to use the toString method. I've never used it before now and have no idea how to show output using it.

Many thanks for any help in the right direction

Hmm... Do you have to implement your code based on the given Course class? Are you allowed to modify the Course class or you have to use it as is? The reason is that I believe the Course class toString() design is weird. To me, it is silly to print out the prerequisite courses' prerequisite & text books in toString().

Anyway, using toString() is to get a class instance object current information. You can simply call it via System.out, or you could explicitly call .toString() of a class object. If you do not override the toString() method, the method of its parent class (the class it is extended from) will be called. If the class does not inherit from any class or any of its parent class has not overridden toString(), the method of Object class, which is the parent of all classes, will be called. You could get a useless info (class@hexadecimal) from calling the original toString() from Object class.

You could see a simple code of how to use SAXParser here.

Yes, we have to implement our code based on the given Course class. My teacher never told the class explicitly whether or not we could modify the Course class. I read over a couple of articles on the toString method and I have a better grasp on it. However my main problem is still how I take the user input from the Main class and use it with the MyHandler class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.