CSCI A348/548
Final Exam

Spring 2001 (Second semester 2000-2001)


This is your A348/A548 final exam.

  1. Make a copy of this document, and study it thoroughly.
  2. Then post the answers on your web site by 9:15 pm on May 3, 2001.
  3. I will be grading it Friday and Saturday, May 4-5.
  4. I will turn final grades in to the Office of the Registrar on Sunday.

This exam contains questions and miniprojects. Include the answers here, with links to what the projects ask you to do. Good luck and let me know if you have any questions!


1. What is SGML? Are SGML, HTML, and XML related at all?

Include your answers here in your document, and write as much as you need to. Suppose I have a particular point I want to make by sending you to a URL, then please index it in your answer visibly, like
I do it here
Anyway, you get the idea of how you need to include your answers here.

2. Draw a tree diagram of this XML document (that illustrates its structure):

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <greeting>Hello from XML</greeting> 
  <message>
    Welcome to the wild and wooly world of XML
  </message>
</document>
The colors are supposed to help you.
If you can't draw the tree, describe it.

Here's how I draw a tree in ASCII:

/u/dgerman/apache/apache_1.3.14/htdocs/index.html
          |      |             |
          |      |             /cgi-bin/pictures
          |      |             |       /calculator
          |      |             |       /printenv
          |      |             |
          |      |             /logs/httpd.pid
          |      |             |    /access_log
          |      |             |    /error_log
          |      |             |
          |      |             /conf/httpd.conf
          |      |
          |      /jakarta-tomcat-3.2.1
          |
          /project/books.txt 
Again, I hope you get the idea.

3. Create an final directory under your Apache web server's htdocs. Add the XML file above (please name it one.xml) and add the following stylesheet defined below:

greeting {display: block; font-size: 36pt; color: #0066CC; text-align: center}
message  {display: block; font-size: 18pt; color: #DD0000}
Also add this line to one.xml:
<?xml-stylesheet type="text/css" href="one.css"?>
Then index your document here and use Internet Explorer 5 to view it. (Here's mine).

4. Explain what a DTD is and how the one below works.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="one.css"?>
<!DOCTYPE DOCUMENT [
  <!ELEMENT document (greeting, message)>
  <!ELEMENT greeting (#PCDATA)>
  <!ELEMENT message (#PCDATA)>
]>
<document>
  <greeting>Hello from XML</greeting>
  <message>
    Welcome to the wild and wooly world of XML
  </message>
</document>

Thanks to GJ Lee and Chris Neumeyer for catching the typo and pointing out that the DTD should be in the same case as the XML document.

5. Parsing XML with JavaScript.

Place this into a file next to one.xml developed above.

<html>
  <head>
    <title>
      Finding element values in an XML document
    </title>
    <xml id="firstxml" src="one.xml"></xml>
    <script language="javascript">
      function getData() {
        xmldoc = document.all("firstxml").XMLDocument;
        nodeDoc = xmldoc.documentElement;
        nodeGreeting = nodeDoc.firstChild;
        outputMessage = "Greeting: " + nodeGreeting.firstChild.nodeValue;
        message.innerHTML = outputMessage; 
      }
    </script>
  </head>

  <body bgcolor=white>
    <center>
      <h1>Finding element values in an XML document</h1>
      <div id="message"></div>
      <p> 
      <input type="button" value="Parse XML" onclick="getData()">
    </center>
  </body>
</html> 
Load it into Internet Explorer 5. What does it do, and how?

Here's my implementation (use Internet Explorer 5, please.)

6. Parsing XML with Java.

Consider the following Java file:

import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
public class FirstParser {
    public static void main(String[] args) {
	try {
	    DOMParser parser = new DOMParser(); 
	    parser.parse(args[0]); 
	    Document doc = parser.getDocument(); 
	    NodeList nodeList = doc.getElementsByTagName("customer"); 
	    System.out.println(args[0] + " has " + 
			       nodeList.getLength() + 
			       "  elements. "); 
	} catch (Exception e) {
	    e.printStackTrace(System.err); 
	} 
    } 
}
What does it do and how?

Run it on the following file (customer.xml):

<?xml version="1.0"?>
<document>
  <customer> One   </customer>
  <customer> Two   </customer>
  <customer> Three </customer>
  <customer> Four  </customer>
  <customer> Five  </customer>
  <customer> Six   </customer>
</document>
Run it also on this file (customer1.xml):
<?xml version="1.0"?>
<document>
  <customer> 
    <item> One   </item> 
    <item> Two   </item> 
    <item> Three </item> 
    <item> Four  </item> 
  </customer>
  <customer> 
    <item> One   </item> 
  </customer>
  <customer> 
    <item> One   </item> 
    <item> Two   </item> 
    <item> Three </item> 
  </customer>
  <customer> 
    <item> One   </item> 
    <item> Two   </item> 
    <item> Three </item> 
    <item> Four  </item> 
  </customer>
  <customer> 
    <item> One   </item> 
    <item> Two   </item> 
    <item> Three </item> 
    <item> Four  </item> 
  </customer>
  <customer> 
    <item> One   </item> 
    <item> Two   </item> 
  </customer>
</document>
What's the output?

7. Traversing the entire document with Java

Consider the following Java program:

import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class IndentingParser {
    static String displayStrings[] = new String[1000];
    static int numberDisplayLines = 0; 
    public static void displayDocument(String uri) {
	try {
	    DOMParser parser = new DOMParser(); 
	    parser.parse(uri); 
	    Document document = parser.getDocument(); 
	    display(document, " "); 
	} catch (Exception e) {
	    e.printStackTrace(System.err); 
	}
    }
    public static void display(Node node, String indent) {
	if (node == null) { return; } 
	int type = node.getNodeType(); 
	switch (type) {
	    case Node.DOCUMENT_NODE: {
		displayStrings[numberDisplayLines] = indent; 
		displayStrings[numberDisplayLines] += 
		    "<?xml version=\"1.0\" encoding=\"" + "UTF-8" + "\"?>"; 
		numberDisplayLines++;
		display(((Document)node).getDocumentElement(), ""); 
		break; 
	    }
	    case Node.ELEMENT_NODE: {
		displayStrings[numberDisplayLines] = indent; 
		displayStrings[numberDisplayLines] += "<"; 
		displayStrings[numberDisplayLines] += node.getNodeName(); 
		int length = (node.getAttributes() != null) ?
		    node.getAttributes().getLength() : 0; 
		Attr attributes[] = new Attr[length];
		for (int loopIndex = 0; loopIndex < length; loopIndex++) {
		    attributes[loopIndex] = 
			(Attr)node.getAttributes().item(loopIndex);
		}
		for (int loopIndex = 0; loopIndex < attributes.length; 
                                        loopIndex++) {
		    Attr attribute = attributes[loopIndex];
		    displayStrings[numberDisplayLines] += " "; 
		    displayStrings[numberDisplayLines] += 
                        attribute.getNodeName(); 
		    displayStrings[numberDisplayLines] += "=\""; 
		    displayStrings[numberDisplayLines] += 
                        attribute.getNodeValue(); 
		    displayStrings[numberDisplayLines] += "\""; 
		}
		displayStrings[numberDisplayLines] += ">"; 
		numberDisplayLines++; 
		NodeList childNodes = node.getChildNodes(); 
		if (childNodes != null) {
		    length = childNodes.getLength(); 
		    indent += "    "; 
		    for (int loopIndex = 0; loopIndex < length; loopIndex++) {
			display(childNodes.item(loopIndex), indent); 
		    }
		}
		break; 
	    }
	    case Node.CDATA_SECTION_NODE: {
		displayStrings[numberDisplayLines] = indent; 
		displayStrings[numberDisplayLines] = "<![CDATA["; 
		displayStrings[numberDisplayLines] += node.getNodeValue(); 
		displayStrings[numberDisplayLines] += "]]>"; 
		numberDisplayLines++; 
		break; 
	    }
	    case Node.TEXT_NODE: {
		displayStrings[numberDisplayLines] = indent; 
		String newText = node.getNodeValue().trim(); 
		if (newText.indexOf("\n") < 0 && newText.length() > 0) {
		    displayStrings[numberDisplayLines] += newText; 
		    numberDisplayLines++; 
		}
		break; 
	    }
	    case Node.PROCESSING_INSTRUCTION_NODE: {
		displayStrings[numberDisplayLines] = indent; 
		displayStrings[numberDisplayLines] += "<?"; 
		displayStrings[numberDisplayLines] += node.getNodeName(); 
		String text = node.getNodeValue(); 
		if (text != null && text.length() > 0) {
		    displayStrings[numberDisplayLines] += text; 
		}
		displayStrings[numberDisplayLines] += "?>"; 
		numberDisplayLines++;
		break;
	    }
	} /* switch */ 
	if (type == Node.ELEMENT_NODE) {
	    displayStrings[numberDisplayLines] = 
		indent.substring(0, indent.length() - 4);
	    displayStrings[numberDisplayLines] += "</";
	    displayStrings[numberDisplayLines] += node.getNodeName(); 
	    displayStrings[numberDisplayLines] += ">"; 
	    numberDisplayLines++; 
	    indent+= "    "; 
	}
    }

    public static void main(String[] args) {
	displayDocument(args[0]);
	for (int loopIndex = 0; loopIndex < numberDisplayLines; loopIndex++) {
	    System.out.println(displayStrings[loopIndex]);
	}
    }

}
What does it do and how?

Run it on customer.xml and customer1.xml to check your answer.

8. Transforming XML data files into graphical output.

Consider the following Java program:

import java.awt.*;
import java.awt.event.*;
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class Circles {
    static int numberFigures = 0; 
    static int x[] = new int[100]; 
    static int y[] = new int[100];
    static int radius[] = new int[100];

    public static void displayDocument(String uri) {
	try {
	    DOMParser parser = new DOMParser(); 
	    parser.parse(uri); 
	    Document document = parser.getDocument(); 

	    display(document); 

	} catch (Exception e) {
	    e.printStackTrace(System.err); 
	}
    }

    public static void display(Node node) {
	if (node == null) { return; } 
	int type = node.getNodeType(); 
	if (node.getNodeType() == Node.DOCUMENT_NODE) {
	    display(((Document)node).getDocumentElement()); 
	}
	if (node.getNodeType() == Node.ELEMENT_NODE) {
	    if (node.getNodeName().equals("CIRCLE")) {
		NamedNodeMap attrs = node.getAttributes(); 
		x[numberFigures] = Integer.parseInt(
                       (String)attrs.getNamedItem("X").getNodeValue());
		y[numberFigures] = Integer.parseInt(
                       (String)attrs.getNamedItem("Y").getNodeValue());
		radius[numberFigures] = Integer.parseInt(
                       (String)attrs.getNamedItem("RADIUS").getNodeValue());
		numberFigures++; 
	    }
	    NodeList childNodes = node.getChildNodes(); 
	    if (childNodes != null) {
		int length = childNodes.getLength(); 
		for (int loopIndex = 0; loopIndex < length; loopIndex++) {
		    display(childNodes.item(loopIndex)); 
		}
	    }
	}
    }

    public static void main(String[] args) {
	displayDocument(args[0]); 
	System.out.println("Creating frame..." + numberFigures); 
	AppFrame f = new AppFrame(numberFigures, x, y, radius); 
	f.setSize(400, 400); 
	f.addWindowListener(
			    new WindowAdapter() {
				    public void windowClosing(WindowEvent e) {
					System.exit(0); 
				    }
				}
			    );
	f.show(); 
	System.out.println("Showing frame..."); 
    }
} 

class AppFrame extends Frame {
    int numberFigures; 
    int[] xValues; 
    int[] yValues; 
    int[] radiusValues; 
    public AppFrame(int number, int[] x, int[] y, int[] radius) {
	numberFigures = number; 
	xValues = x;
	yValues = y;
	radiusValues = radius; 
    }  
    public void paint(Graphics g) {
	for (int loopIndex = 0; loopIndex < numberFigures; loopIndex++) {
	    g.drawOval(xValues[loopIndex], 
		       yValues[loopIndex], 
		       radiusValues[loopIndex],
		       radiusValues[loopIndex]); 
	}
    } 
} 
What do you think it is doing and how?

Run it on the following input to check your answer:

<?xml version="1.0"?>
<!DOCTYPE DOCUMENT 
 [
   <!ELEMENT DOCUMENT (CIRCLE|ELLIPSE)*>
   <!ELEMENT CIRCLE EMPTY>
   <!ELEMENT ELLIPSE EMPTY>
   <!ATTLIST CIRCLE 
       X CDATA #IMPLIED
       Y CDATA #IMPLIED
       RADIUS CDATA #IMPLIED>
   <!ATTLIST ELLIPSE
       X CDATA #IMPLIED
       Y CDATA #IMPLIED
       WIDTH CDATA #IMPLIED
       HEIGHT CDATA #IMPLIED>
 ]>

<DOCUMENT>
  <CIRCLE X='200' Y='160' RADIUS='50' />
  <CIRCLE X='170' Y='100' RADIUS='15' />
  <CIRCLE X= '80' Y='200' RADIUS='45' />
  <CIRCLE X='200' Y='140' RADIUS='35' />
  <CIRCLE X='130' Y='240' RADIUS='25' />
  <CIRCLE X='270' Y='300' RADIUS='45' />
  <CIRCLE X='210' Y='240' RADIUS='25' />
  <CIRCLE X= '60' Y='160' RADIUS='35' />
  <CIRCLE X='160' Y='260' RADIUS='55' />
</DOCUMENT>
These are the only questions on the final.

I will post a bonus section as soon as I can, which will actually try to help with this final as well.


Last updated on April 29, 2001 by Adrian German for A348/A548