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