CSCI 5733.2
XML Application Development
Spring 2006
Mid-Term ExaminationName: _________________________________
Time allowed: one hour and 20 minutes. Total score: 30 points.
Open: lecture notes, every file I wrote and posted in my Web page and your project assignments, but no book.
Answer all questions. Turn in both question and answer sheets. Plan your time well.
Academic honesty policy will be followed strictly. Cheating will be pursued vigorously and will result in a failing grade of D or below, a permanent academic record and possibly other more serious penalty!
(1) [7 points] Write a simple Web service CGI-Perl program, t1bq1.pl. The program retrieves quotations stored in a text file in the same directory, quotations.txt, and serves up the XML content.
The file quotations.txt stores a quotation per line in the following format: [author]quotation. For example, the content of quotations.txt may be:
[Yue]I like burgers & pizza.
["Conan the great"]I like pizza.
[Hall]I like ice cream.
["Conan the great"]I like ice cream too.You may assume that the character ']' does not appear in the author field.
For http://dcm.cl.uh.edu/youraccount/h1bq1.pl
the XML content served should be:
<?xml version='1.0' encoding='ISO-8859-1' ?>
<quotations>
<quote author="Yue">I like burgers & pizza.</quote>
<quote author=""Conan the great"">I like pizza.</quote>
<quote author="Hall">I like ice cream.</quote>
<quote author=""Conan the great"">I like ice cream too.</quote>
</quotations>You may assume that there are no errors in the file quotations.txt.
(2) [7 points] Write a Java JAXP SAX program, NumberInteriorElements.java, (DOM and other techniques not acceptable) to read an XML file and print out the number of interior elements. An interior element is an element with at least one child element.
For example, running:
java NumberInteriorElements t1bq2.xml
should output:
Number of interior elements: 4.
for the file t1bq2.xml:
<?xml version="1.0" ?>
<root>
<a><b><c /><d><e /><f />hello</d></b><g>hi</g><h /></a>
</root>This is because there are four interior elements: <root>, <a>, <b> and <d>.
A skeleton is provided below so all you need to do are to insert code to print the result and declare data members (if needed) and SAX event handlers.
import java.util.*;
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 NumberInteriorElements extends DefaultHandler
{
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: java umberInteriorElements filename");
System.exit(1);
}
DefaultHandler handler = new NumberInteriorElements();
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{ SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File(argv [0]), handler);
} catch (Throwable t) {
t.printStackTrace();
}
// code to print result...
}
// Data members and Event handlers.
} // NumberInteriorElements
(3) [7 points] (a) Write a Java's method, lastChild, to return the last child element of an element node:private static org.w3c.dom.Element lastChild(org.w3c.dom.Element element)
If the element has no child elements, null is returned.
(b) Write a Java's method, rightmostDescendant, to return the rightmost descendant of an element:
private static org.w3c.dom.Element rightmostDescendant(org.w3c.dom.Element element)
If the element has no descendant, the element itself is returned. If the element has child element nodes, the rightmost descendant of the last child element should be returned. For example, for
<root>
<a><b>hey<c /><d><e /><f />hello</d></b><g>hi</g><h><i />hi<j>bye</j></h></a>
</root>If the element node referring to <root> is passed as the actual argument to the method, then the element node referring to <j> should be returned.
(4) [3 points] True or false. No justification is necessary. Just answer 'T' or 'F' in the question paper.
(a) In XML Schema, simpleType can be used as the data type definition of both elements and attributes.
(b) More than one XML schema can be used within the same XML document.
(c) If defined, a parameter entity can be used inside an XML document.(5) (a) [3 points] For the following requirements, state whether they can be satisfied completely by DTD. If yes, give the exact element declaration. If no, give the best approximate declaration.
Requirements: The element <a> contains one ore more of elements <b> and <c> in any order. (That is <a> should have at least one <b> and at least one <c>.) The element <a> may also contain text string directly.
(b) [3 points] Define the SimpleType AccountAmount in XML Schema to accept numbers that may have a pair of parenthesis enclosing them (to indicate deficient). A decimal point is also acceptable, but it must be followed by at least one digit. For example, the followings are acceptable.
0
1
12.34
0.1234
(123)
(12,345)The followings are not acceptable:
a12
123.
(12.345
012