views:

33

answers:

2

Am new to using SAX parser .Can anyone tell me how to run it .and what all are required to run it (jdk )..Can i have a sax parser that can parse both android xml and a normal xml

A: 

SAX parsers are implemented by creating a ContentHandler object which implements certain callback functions that correspond to events that happen while parsing an XML document. For example, the startDocument method is called when the parser begins parsing the document, and startElement is called when it discovers a new tag; similarly, endElement, endDocument, and error are called when the parser finds the end of a tag or document, or when an invalid sequence is discovered.

This example shows how to use a SAX parser. The key is that the MyHandler class extends the DefaultHandler class (which implements the ContentHandler interface) and overrides the empty implementations of each callback method.

Think of it this way: the Java SAXParser class knows how to parse XML documents but when it discovers things of interest it relies on some handler class to know what to do with them. The DefaultHandler class is a helper implementation which you can extend to pay attention to the interesting things.

maerics
THANKS maerics can u tell me how to parse a local xml file..then defaulthandler class not required
apoorva
A: 

You could use a ContentHandler directly (see below) instead of extending the DefaultHandler if you want. I believe this level of SAX parsing is available on the Android platform.

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.XMLReader;

public class Demo {

    public static void main(String[] args) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(new MyContentHandler());
        xr.parse("input.xml");
    }

    private static class MyContentHandler implements ContentHandler {

        public void setDocumentLocator(Locator locator) {
        }

        public void startDocument() throws SAXException {
        }

        public void endDocument() throws SAXException {
        }

        public void startPrefixMapping(String prefix, String uri)
                throws SAXException {
        }

        public void endPrefixMapping(String prefix) throws SAXException {
        }

        public void startElement(String uri, String localName, String qName,
            System.out.println("START " + qName);
        }

        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            System.out.println("END " + qName);
        }

        public void characters(char[] ch, int start, int length)
                throws SAXException {
            System.out.println(new String(ch, start, length));
        }

        public void ignorableWhitespace(char[] ch, int start, int length)
                throws SAXException {
        }

        public void processingInstruction(String target, String data)
                throws SAXException {
        }

        public void skippedEntity(String name) throws SAXException {
        }

    }

}
Blaise Doughan