CSCI 5931A.1
XML Application Development
Spring 2002
Mid-Term Examination

Name:  _________________________________

Time allowed: one hour and 20 minutes.  Total score: 100%.

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) [25%] Write a Perl (or Java) program to read in a text file and generate an output XML file. The input text file uses a modification of the comma separated value format, storing one record per line in the format:

source,quote,rank

Furthermore, a comma is stored as "\c" and so a '\' is stored as "\\".

For example, the line:

Terry Feagin, In Visual Basic\c 5\\3 + 1 < 3,6

stores the record with:

source = "Terry Feagin"
quote = "In Visual Basic, 5\3 + 1 < 3"
rank = "6".

Your program takes the input and output file names as command line arguments.

Running:

>t1q1.pl t1q1test.txt t1q1testout.xml

with the input file t1q1test.txt:

Bun Yue,Java is good,5
Sharon Perkins,I like assembly language,6
Terry Feagin, In Visual Basic\c 5\\3 + 1 < 3,6

should generate the output xml file t1q1testout.xml in the following format exactly:

<quotes>
  <quote><body>Java is good</body><source>Bun Yue</source></quote>
  <quote><body>I like assembly language</body><source>Sharon Perkins</source></quote>
  <quote><body> In Visual Basic, 5\3 + 1 &lt; 3</body><source>Terry Feagin</source></quote>
</quotes>

(2)

(a) [7%] Is the following XML document well-formed? If not, point out all mistakes.

<?xml version="1.0"?>
<family>
<father />
<mother />
<son>Joe Giarratano<son />
<equation c='1'>a>b</equation>
</family>

(b) [8%] Is the following XML document validated with the DTD below? If not, modify the DTD so that the XML document complies to the new DTD.

XML:

<?xml version="1.0"?>
<!DOCTYPE c SYSTEM "mydtd.dtd">
<c>
<a x="_1" />
<a x="_2" />
<a />
<b y = "_1">dog</b>
<b />
</c>

mydtd.dtd:

<!ELEMENT c (a*, b?)>
<!ELEMENT a EMPTY>
<!ATTLIST a x IDREF#IMPLIED>
<!ELEMENT b (#PCDATA)>
<!ATTLIST b y ID #REQUIRED>

 

(c) [15%] Define a reasonable and minimal DTD for storing one directed graph. A directed graph G is G(N, E), where N is the set of nodes and E is the set of edges. A directed graph may be empty. A node must have a name and may has a weight. An edge may or may not have a name. An edge is an arc connecting a node to another node.

(3) [20%] Write a simple WML program, t1q3.wml, that starts with this screen:

If the anchor "start quotation" is selected and accepted:

then the micro-browser will loop through a sequence of three quotations cyclically, displaying each quotation for five seconds. The quotations are:

"I love XML."
"I love Visual Basic."
"I love Java."

If the "Home" anchor is selected at any of these screens, such as:

it will go back to the original start screen:

(4) [25%] Write a simple Java program showRootChildrenCount.java using SAX (other methods not acceptable) to count and show the number of children elements of the document root element. For example, for

my.xml

<?xml version="1.0"?>
<!DOCTYPE c SYSTEM "mydtd.dtd">
<c>
<a x="_1" />
<a x="_2" />
<a />
<b y = "_1">dog</b>
<b />
</c>

Running:

java showRootChildrenCoun my.xml

output:

Number of children elements of the document root = 5.

The skeleton of the program showRootChildrenCount.java, including the main method, is given below. You will only need to give the definitions of data members, SAX callback handlers and the utility method "printRootChildrenCount()".

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 showRootChildrenCount extends DefaultHandler
{
  public static void main(String argv[])
  {
    if (argv.length != 1) {
      System.err.println("Usage: Java showRootChildrenCount filename");
      System.exit(1);
    }

    try {
      // Use an instance of ourselves as the SAX event handler
      DefaultHandler handler = new showRootChildrenCount();
      // Use the default (non-validating) parser
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser saxParser = factory.newSAXParser();
      saxParser.parse(new File(argv [0]), handler);
      printRootChildrenCount();
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }

  // Data members.
  // ...

  // Handler methods
  // ...

  // utility method: printRootChildrenCount
  private static void printRootChildrenCount() {
    // ...
  } // printRootChildrenCount
} // showRootChildrenCount