CSCI 5733.1
XML Application Development
Spring 2005
Suggested Solution to Mid-Term Examination(1) For example,
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 AverageNumberOfAttributes extends DefaultHandler
{
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: java AverageNumberOfAttributes xmlfilename");
System.exit(1);
}
// Use an instance of ourselves as the SAX event handler
DefaultHandler handler = new AverageNumberOfAttributes();
// 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 number of attributes of the XML file is "
+ ((AverageNumberOfAttributes) handler).getAverageNumberOfAttributes() + ".");
} catch (Throwable t) {
t.printStackTrace();
}
} // main
// Data members:
// Number of elements of the tree so far.
private int numElements = 0;
// Number of attributes of the tree so far.
private int numAttributes = 0;
// Average number of attributes
private double averageNumberOfAttributes = 0.0;
// Handler methods
public void startElement(String namespaceURI,
String lName,
String qName,
Attributes attrs)
throws SAXException
{ numAttributes += attrs.getLength();
numElements++;
} // startElement
private double getAverageNumberOfAttributes() {
return averageNumberOfAttributes;
} // getAverageNumberOfAttributes
public void endDocument()
throws SAXException
{ averageNumberOfAttributes = (double) numAttributes / (double) numElements;
} // endDocument
} // AverageNumberOfAttributes.
(2) For example,
<!ELEMENT ArtCollection (Artist*, Artifact*)>
<!ELEMENT Artist (LastName, FirstName)>
<!ATTLIST Artist SSN ID #REQUIRED>
<!ELEMENT LastName (#PCDATA)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT Artifact (SecondaryArtist*)>
<!ATTLIST Artifact
PrimaryArtistSSN IDREF #REQUIRED
Title CDATA #REQUIRED
AcquisitionDate CDATA #REQUIRED
AcquisitionPrice NMTOKEN #REQUIRED
SalePrice NMTOKEN #REQUIRED>
<!ELEMENT SecondaryArtist EMPTY>
<!ATTLIST SecondaryArtist SSN IDREF #REQUIRED>
Assume that SSN is stored with a leading _.
(3)
(a) False
(b) True
(c) True
(d) False
(e) False(4)
(a) For example:
NodeList children = a.getChildNodes();
for (int i=0; i<children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.TEXT_NODE) System.out.print(children.item(i).getNodeValue());
}(b) For example,
<xsd:simpleType name="positiveIntegerWithComma">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[1-9]\d*|[1-9]\d{0,2}(,\d{3})*"/>
</xsd:restriction>
</xsd:simpleType>(c) For example,
use strict;
my $time = localtime;
print <<__END;
Content-type: text/xml
<?xml version="1.0" ?>
<servertime>
Date and time: <$time>
</servertime>
__END
exit 0;