Client-Side Presentation Logic
609
public Vector parse(URL myURL) {
try {
parser = new XmlParser();
InputStream myInput = myURL.openStream();
parser.setHandler(this);
parser.parse(null, null, myInput, null);
} catch (Exception se) {
se.printStackTrace();
}
return myDeals;
}
Here are the handlers for the parser. The general strategy is to collect the subelement and attribute
information of the <trip> element, and add an entry to the myDeals vector whenever we reach an
endElement(</trip>) on the <trip> element.
public void attribute(String name, String val, boolean inSpec) {
if (name.compareTo("number") == 0) {
maxTripNumber = Integer.parseInt(val, 10);
curID = maxTripNumber;
} else if (name.compareTo("region") == 0) {
curRegion = Integer.parseInt(val, 10);
} else if (name.compareTo("commission") == 0) {
curCommission = Integer.parseInt(val, 10);
}
}
The charData() method is called whenever character data is parsed by the XML parser. Here, we
accumulate the character data in the lastParsedText variable.
public void charData(char[] ch, int start, int length) {
int limit = start + length;
String cdata = "";
for (int i = start; i < limit; i++) {
cdata += ch[i];
}
lastParsedText = cdata;
}
Note that for the handling in endElement() for </trip>, we:
q
Format the collected trip description as workstring
q
Create a new <TravelDeal> element
q
Add the new element to myDeals
public void endElement(String name) {
if (name.compareTo("startdate") == 0) {
curStartdate = lastParsedText;
}
if (name.compareTo("duration") == 0) {
curDuration = lastParsedText;
}