April 7, 2011

SAX Parser in Java

The code example below is of a SAX Parser in Java that is a validating parser (validation by DTD but not by W3C Schema), but not a namespace-aware parser.

To make the parser namespace-aware, add any one of the following lines of code,

factory.setFeature("http://xml.org/sax/features/namespaces", true);

OR

factory.setNamespaceAware(true);


To make the parser a validating parser by W3C Schema, add the following code,

//set the validation to be against W3C XML Schema
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");

OR

//set the schema against which the validation is to be done
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", new File("myschema.xsd"));

SAX Parser code:

package info.icontraining.parsers;

import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;

public class MySAXParser extends DefaultHandler implements LexicalHandler {

   private StringBuffer textBuffer;
   private Locator locator;

   public static void main(String[] args) {

      MySAXParser handler = new MySAXParser();
      SAXParserFactory factory = SAXParserFactory.newInstance();

      try {
         factory.setFeature("http://xml.org/sax/features/validation", true);
   
         SAXParser saxParser = factory.newSAXParser();
         saxParser.getXMLReader().setErrorHandler(handler);
         saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
         saxParser.parse( new File("FileName.xml"), handler );
      }
      catch (Throwable t) {
         t.printStackTrace();
      }
   }

   public void startDocument() throws SAXException {
      System.out.println("START DOCUMENT");
   }

   public void endDocument() throws SAXException { 
      System.out.println("END DOCUMENT");
   }

   public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException {

      System.out.print("ELEMENT: ");
      String eName = sName;

      if ("".equals(eName)) 
         eName = qName;
      System.out.print("<"+eName);
  
      if (attrs != null) {
         for (int i = 0; i < attrs.getLength(); i++) {
            System.out.println();
            String aName = attrs.getLocalName(i);
            if ("".equals(aName)) 
               aName = attrs.getQName(i);
            System.out.println(" ATTR: " + aName + "\t\"" 
                 + attrs.getValue(i) + "\"");
         }
      }
  
      if (attrs.getLength() > 0) 
         System.out.println();
      System.out.print(">");
      System.out.println();
   }

   public void endElement(String namespaceURI, String sName, String qName) {

      System.out.print("END_ELEMENT: ");
      String eName = sName; // element name

      if ("".equals(eName)) 
         eName = qName;

      System.out.println("</"+eName+">");
   }

   public void characters(char buf[], int offset, int len) throws SAXException {
  
      String s = new String(buf, offset, len);

      if (textBuffer == null) {
         textBuffer = new StringBuffer(s);
      } else {
         textBuffer.append(s);
      }
  
      if (textBuffer == null) 
         return;  

      System.out.print("CHARACTERS: ");
      String str = ""+textBuffer;
      System.out.println(str);
      textBuffer = null;
   }

   public void setDocumentLocator(Locator l) {
      locator = l;
   }

   public void error(SAXParseException exception)  {
      System.out.println("My Error: " + exception.getLineNumber() +  
          ":" + exception.getMessage());
   }
 
   public void fatalError(SAXParseException exception) {
      System.out.println("My Fatal Error: " + exception.getLineNumber() +  
          ": " + exception.getMessage());
   }

   public void warning(SAXParseException exception) {
      System.out.println("My Warning: " + exception.getLineNumber() +  
          ": " + exception.getMessage());
   }

   public void comment(char[] ch, int start, int length) throws SAXException {
      String text = new String(ch, start, length);
      System.out.println("COMMENT: "+text);
   }

   public void endCDATA() throws SAXException {
      System.out.println("CDATA Section ended");
   }

   public void endDTD() throws SAXException {
      System.out.println("DTD Ended");
   }

   public void endEntity(String name) throws SAXException {
      System.out.println("End Entity: " + name);
   }

   public void startCDATA() throws SAXException {
      System.out.println("CDATA section started");
   }

   public void startDTD(String name, String publicId, String systemId) throws SAXException {
      System.out.println("DTD Started");
   }

   public void startEntity(String name) throws SAXException {
      System.out.println("Start Entity: " + name);
   }
}

1 comment:

  1. It would be nice if SAX and DOM examples had an example xml included. This way we could use it right away.

    ReplyDelete