CSCI 5931A.1
XML Application Development
Fall 2002
Mid-Term ExaminationName: _________________________________
Time allowed: one hour and 20 minutes. Total score: 30 points.
Open: text book, lecture notes, every file I wrote and posted in my Web page and your project assignments. No other materials are allowed.
Answer all questions. Turn in both question and answer sheets. Plan your time well.
Academic honesty policy will be followed strictly. Cheating will result in a failing grade of D or below and a permanent academic record!
(1) [10 points] Complete the following program, AverageDepth.java, which reads in an XML file and outputs the average depth of the XML tree using SAX (other methods not acceptable). Only element nodes should be counted in the computation. The depth of an empty tree is defined to be -1.
For the following input file, AverageDepth.xml:
<?xml version="1.0" ?>
<root>
<a /><!-- hello -->
<b><f/></b>
</root>running
java AverageDepth AverageDepth.xml
output:
The average depth of the tree is 1.0.
This is because the depth of the four elements <root>, <a>, <b> and <f> are 0, 1, 1, and 2 respectively.
You must not write your own program. Instead, you should study the following skeleton and fill in the missing parts, which include only the event handlers and other data members you may use besides averageDepth.
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class AverageDepth extends DefaultHandler
{
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: java AverageDepth xmlfilename");
System.exit(1);
}
DefaultHandler handler = new AverageDepth();
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File(argv [0]), handler);
// Print out average depth of the XML tree.
System.out.println("The average depth of the tree is "
+ averageDepth + ".");
} catch (Throwable t) {
t.printStackTrace();
}
} // main
// Data members:
// average depth of the XML tree.
private static double averageDepth = 0.0;
//
// Define other data members and handler
// methods here.
//
} // AverageDepth.(2) (a) [3 points] Give the definition of the simple type EvenNaturalNumber in XML Schema to include only all even positive integers: 2, 4, 6, 8, 10, 12, ... Note that leading zeros ("012", "0332". etc) are not acceptable.
(b) [6 points] Design a DTD to store course and student information. There may be many courses. For each course offering, an unique id, the course number (an integer), the course section (an integer) and the course title (a string) should be stored. Each student has an unique id. The first and last names of the students should also be stored. A student may take many courses. Name the root element "enrollment".
Design your DTD to be as close to the specification above as possible. Enforce as much constraints as possible.
(c) [3 points] Consider the following CGI perl program, which is supposed to print out the local server date and time in XML format.
use strict;
my $time = localtime;
print <<__END;
<?xml version="1.0" ?>
<servertime>
The current date & time is $time
</servertime>
__END
exit 0;There are one or more errors. Correct them.
(3) The following questions are on Java DOM using JAXP.
(a) [5 points] Consider the following Java code for the method DeleteB, whie removes all B descendant elements of an element:
private static void DeleteB (org.w3c.dom.Element element) {
if (element == null) return;
NodeList childNodes = element.getChildNodes();
for (int i=0; i < childNodes.getLength(); i++) {
if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
// An element child node
Element child = (Element) childNodes.item(i);
if ("B".equals(child.getNodeName())) {
element.removeChild(child);
}
else {
DeleteB(child);
}
} // if
} // for
} // DeleteBThere is one subtle logical error. Briefly describe and correct it.
(b) [3 points] Write the Java code to print out the text content of the element a. The element a is of the name MyA, which is defined as:
<!ELEMENT MyA (#PCDATA) >