Java Reference
In-Depth Information
//work with stream and get the type of event
//we're inspecting
while (reader.hasNext()) {
//because this is Cursor, we get an integer token to
next event
eventType = reader.next();
//do different work depending on current event
switch (eventType) {
case XMLEvent.START_ELEMENT:
//save element name for later
current = reader.getName().toString();
printSkus(current, reader);
break;
case XMLEvent.CHARACTERS:
findAuthors(current, reader);
break;
}
} //end loop
out.println("Unique Authors=" + uniqueAuthors);
} catch (XMLStreamException e) {
out.println("Cannot parse: " + e);
}
}
//get the name and value of the topic's sku attribute
private void printSkus(String current, XMLStreamReader r) {
current = r.getName().toString();
if ("book".equals(current)) {
String k = r.getAttributeName(0).toString();
String v = r.getAttributeValue(0);
out.println("AttribName " + k + "=" + v);
}
}
//inspect author elements and read their values.
private void findAuthors(String current, XMLStreamReader r)
throws XMLStreamException {
if ("author".equals(current)) {
String v = r.getText().trim();
//can get whitespace value, so ignore
Search WWH ::




Custom Search