Parse an XML file using SAX
This example show you how to read / parse an xml file using the SAX (Simple API for XML) parser. In the main class (SAXDemo) we create the instance of SAXParserFactory and the SAXParser. The SAXParser.parse() method will parse the given InputStream and handle the xml document using the SAXHandler class that we created.
package org.brudvik.example.xml;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;
public class SAXDemo {
public static void main(String[] args) throws Exception {
//
// Creates a new instance of SAXParserFactory that in turn
// creates a SAXParser.
//
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
//
// The handler that will listen to the SAX event during
// the xml traversal.
//
SAXHandler handler = new SAXHandler();
InputStream data =
SAXDemo.class.getResourceAsStream("/person.xml");
parser.parse(data, handler);
}
}
The SAXHandler class inherited from the org.xml.sax.helpers.DefaultHandler class. This handler will listen to the event triggered by the SAXParser. The event such as the startDocument, endDocument, startElement, endElement, etc.
package org.brudvik.example.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXHandler extends DefaultHandler {
@Override
public void startDocument() throws SAXException {
System.out.println("startDocument");
}
@Override
public void endDocument() throws SAXException {
System.out.println("endDocument");
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
System.out.println("startElement: " + qName);
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
System.out.println("endElement");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("characters : " +
new String(ch, start, length));
}
}
Here an example of the xml file will be read by our program:

Siste kommentarer