Java Reference
In-Depth Information
package com.sc.ch02.stax;
import static java.lang.System.out;
import java.io.InputStream;
import java.util.Set;
import java.util.TreeSet;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
public class StaxCursor {
private static final String db = "/ch02/Catalog.xml";
//we'll hold values here as we find them
private Set<String> uniqueAuthors;
public static void main(String... args) {
StaxCursor p = new StaxCursor();
p.find();
}
//constructor
public StaxCursor() {
uniqueAuthors = new TreeSet<String>();
}
//parse the document and offload work to helpers
public void find() {
XMLInputFactory xif = XMLInputFactory.newInstance();
//forward-only, most efficient way to read
XMLStreamReader reader = null;
//get ahold of the file
final InputStream is =
StaxCursor.class.getResourceAsStream(db);
//whether current event represents elem, attrib, etc
int eventType;
String current = "";
try {
//create the reader from the stream
reader = xif.createXMLStreamReader(is);
Search WWH ::




Custom Search