// SAXExample.java
// Example of using SAX for XML parsing
// Bill MacCartney
// 11 October 2004
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
/**
This is a simple class that processes an XML file using a SAX
parser. Intended to run on a data file like this:
this is before the first dot
and it continues on multiple lines
flip is on
flip is off
*/
public class SAXExample extends DefaultHandler {
/** XML tag strings */
public final String DOTS = "dots";
public final String DOT = "dot";
public final String X = "x";
public final String Y = "y";
public final String FLIP = "flip";
/** Data model */
private class Dot {
int x;
int y;
public Dot(int x, int y) { this.x = x; this.y = y; }
public String toString() { return "(" + x + ", " + y + ")"; }
}
public List dotList = new ArrayList();
/** State variables */
private int x;
private int y;
private boolean flip;
/** Constructor: initialize state */
public SAXExample() {
clear();
}
/** Clear state. */
public void clear() {
x = -1;
y = -1;
flip = false;
}
/** Read XML from input stream and parse, generating SAX events */
public void readXML(InputStream inStream) {
try {
clear();
// SAX 1 approach:
// SAXParserFactory factory = SAXParserFactory.newInstance();
// SAXParser saxParser = factory.newSAXParser();
// saxParser.parse(stream, this);
// SAX 2 approach:
System.setProperty("org.xml.sax.driver", "org.apache.crimson.parser.XMLReaderImpl");
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(this);
reader.parse(new InputSource(new InputStreamReader(inStream)));
} catch (Exception e) {
e.printStackTrace();
}
}
// SAX ContentHandler methods =====================================
/* Receive notice of start of document. */
public void startDocument() throws SAXException {
System.out.println("startDocument");
}
/* Receive notice of end of document. */
public void endDocument() throws SAXException {
System.out.println("endDocument");
}
/* Receive notice of start of XML element. */
public void startElement (String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
System.out.println("startElement: " + qName +
" (" + atts.getLength() +
" attributes)");
if (qName.equals(DOT)) {
x = Integer.parseInt(atts.getValue(X));
y = Integer.parseInt(atts.getValue(Y));
if (flip) {
int temp = x; x = y; y = temp;
}
dotList.add(new Dot(x, y)); // add to data model
} else if (qName.equals(FLIP)) {
flip = true;
}
}
/* Receive notice of end of XML element. */
public void endElement(String namespaceURI,
String localName,
String qName)
throws SAXException {
System.out.println("endElement: " + qName);
if (qName.equals(FLIP)) {
flip = false;
}
}
/* Receive notice of character data (text not in an XML element). */
public void characters (char[] ch, int start, int length)
throws SAXException {
String s = new String(ch, start, length);
s = s.trim();
if (! s.equals(""))
System.out.println("characters: " + s);
}
// end SAX ContentHandler methods =================================
/** Test by running "java SAXExample " */
public static void main (String[] args) {
if (args.length != 1) {
System.err.println ("Usage: cmd filename");
System.exit(1);
}
try {
SAXExample example = new SAXExample();
InputStream in = new BufferedInputStream(new FileInputStream(new File(args[0])));
example.readXML(in);
System.out.println("\nFinished parsing input. Got the following dots:");
System.out.println(example.dotList);
} catch (Throwable t) {
t.printStackTrace ();
}
}
}