capint
8/28/2017 - 5:34 AM

SAX >> Producer and consumer

SAX >> Producer and consumer

import java.io.IOException;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class Skeleton {
  public static void main (String argv [])
  {
    //*** PRODUCER
    XMLReader      producer;
    //*** CONSUMER
    DefaultHandler  consumer;
    // Get an instance of the default XML parser class
    try {
      producer = XMLReaderFactory.createXMLReader ();
    } catch (SAXException e) {
      System.err.println (
      "Can’t get parser, check configuration: "
      + e.getMessage ());
      return;
    }
    // Set up the consumer
    try {
      // Get a consumer for all the parser events
      consumer = new DefaultHandler ();
      // Connect the most important standard handler
      producer.setContentHandler (consumer);
      // Arrange error handling
      producer.setErrorHandler (consumer);
    } catch (Exception e) {
      // Consumer setup can uncover errors,
      // though this simple one shouldn’t
      System.err.println (
      "Can’t set up consumers:"
      + e.getMessage ());
      return;
    }
    // Do the parse!
    try {
      producer.parse (argv [0]);
    } catch (IOException e) {
      System.err.println ("I/O error: ");
      e.printStackTrace ();
    } catch (SAXException e) {
      System.err.println ("Parsing error: ");
      e.printStackTrace ();
    }
  }
}