Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Main {
    public static void main(String[] argv) throws Exception {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        SaxHandler handler = new SaxHandler();
        parser.parse("sample.xml", handler);
    }
}

class SaxHandler extends DefaultHandler {
    public void startDocument() throws SAXException {
        System.out.println("Document processing started");
    }

    public void endDocument() throws SAXException {
        System.out.println("Document processing finished");
    }

    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        if (qName.equals("order")) {
        } else if (qName.equals("date")) {
        } else {
            throw new IllegalArgumentException("Element '" + qName + "' is not allowed here");
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
    }
}