How do I 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.kodejava.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 extended from the org.xml.sax.helpers.DefaultHandler class. The handler will listen to the event triggered by the SAXParser. This handler methods is defined by the interfaces such as the ContentHandler, ErrorHandler, DTDHandler, and EntityResolver.

For example to read the content of the XML file there are methods to listen to events such as startDocument, endDocument, startElement, endElement, etc, which defined by the ContentHandler interface.

package org.kodejava.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:

<root>
    <persons>
        <person>
            <name>Foo</name>
        </person>
        <person>
            <name>Bar</name>
        </person>
    </persons>
</root>

Our program will print the following output:

startDocument
startElement: root
characters  : 

startElement: persons
characters  : 

startElement: person
characters  : 

startElement: name
characters  : Foo
endElement
characters  : 

endElement
characters  : 

startElement: person
characters  : 

startElement: name
characters  : Bar
endElement
characters  : 

endElement
characters  : 

endElement
characters  : 

endElement
endDocument
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.