CSCI 5733.01
XML Application Development
Summer 2004
Suggested Solution to Mid-Term Examination(1) For example,
// Handler methods
public void startElement(String namespaceURI,
String lName, // local name
String qName, // qualified name
Attributes attrs)
throws SAXException
{ text.setLength(0);
} // startElement
public void endElement(String namespaceURI,
String lName, // simple name
String qName // qualified name
)
throws SAXException
{
String eName = lName; // element name
if ("".equals(eName)) eName = qName; // namespaceAware = false
if ("name".equals(eName))
{ if (! "".equals(csv)) csv += "\n";
csv += "\"" + getText().toString().replaceAll("\"", "\"\"") + "\"";
}
else if ("book".equals(eName))
{ csv += ",\"" + getText().toString().replaceAll("\"", "\"\"") + "\"";
}
} // endElement
(2) (a) For example,
use LWP::Simple;
use strict;
my $url = "http://dcm.uhcl.edu/yue/courses/xml/Summer2004/test/t1bq2a.xml";
my $urlContents = get($url);
# Remove everything before the root element.
$urlContents =~ /(<[a-zA-Z:_].*)$/s;
$urlContents = $1;
# Remove all element tags. Assume no CDATA sections.
$urlContents =~ s/<.*?>//gs;
$urlContents = xmlDecode($urlContents);
# print HTTP header
print "Content-type: text/plain\n\n";
print $urlContents;
exit 0;
sub xmlDecode {
my $result = shift;
$result =~ s/</</gsi;
$result =~ s/>/>/gsi;
$result =~ s/"/"/gsi;
$result =~ s/'/'/gsi;
$result =~ s/&/&/gsi;
$result;
} # xmldecode
(3)
(a) False
(b) True
(c) False
(d) False
(e) False(4)
(a) Exactly two errors: <d> must be declared and <a> must contain <c> after its child <b>.
(b) For example,
<xsd:simpleType name="OddNumber">
<xsd:restriction base="xsd:string">
<xsd:pattern value="0|4|8|[2468][048]|[13579][26]|([1-9]+[0-9]*([02468][048]|[13579][26]))" / >
</xsd:restriction>
< /xsd:simpleType>This is also acceptable:
<xsd:pattern value="0|4|8|[0-9]*([02468][048]|[13579][26]))" / >
(c) For example,
Node child = a.getFirstChild();
while (child != null) {
if (Node.TEXT_NOTE == child.getNodeType()) print child.getNodeValue();
child.getNextSibling();
}