Java Reference
In-Depth Information
} catch (FileNotFoundException fnfe) {
out.println("Cannot find source: " + fnfe);
} catch (XMLStreamException e) {
out.println("Cannot parse: " + e);
}
}
private void findHighPrices(XMLEventReader reader,
XMLEvent event) throws XMLStreamException {
String currentElem = event.asStartElement().toString();
// save off the title so we can match the price with it
if ("<title>".equals(currentElem)) {
lastTitle = reader.getElementText();
}
// get the current price and add to collection if high
if ("<price>".equals(currentElem)) {
double price;
try {
price = Double.parseDouble(reader
.getElementText());
if (price > 10.0D) {
expensiveBooks.put(lastTitle, price);
}
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
} catch (XMLStreamException xse) {
xse.printStackTrace();
}
}
}
}
/**
* Get only start elements for efficiency. If we returned only
* attributes, for example, we wouldn't be able to read the data
* we're interested in here (title and price values).
*/
class StartElementEventFilter implements EventFilter {
// only req'd method to implement
public boolean accept(XMLEvent event) {
return event.isStartElement();
Search WWH ::




Custom Search