java.lang.Object
com.aquima.interactions.foundation.xml.parsing.XmlSAXParser

public class XmlSAXParser extends Object
SAX2 Parser with error handling in place (validating or not).

This is a convenience wrapper around the JAXP SAXParser, which in turn is the pluggable XML API for any compatible SAX parser. Notice that this is a synchronous parser that may be used by one thread only! It is fine to reuse the parser after parsing has finished.

The parser doesn't use Namespaces, and doesn't use Schemas!

This file uses the standard JAXP and SAX2 APIs (javax.xml.*, org.w3c.*).

Developed using JAXP 1.1, older JAXP versions will not work. JDK 1.4 comes with a JAXP parser, or use a recent Apache Xerces parser.

 EXAMPLE:
 class MyHandler extends DefaultContentHandler
 {
     StringBuffer charBuf = new StringBuffer();
 
     public void startElement(String namespaceURI, String localName, String qName, Attributes attrs)
     throws org.xml.sax.SAXException
     {
         charBuf.setLength(0); // reset char buffer; a new xml element starts
     }
 
     public void endElement(String namespaceURI, String localName, String qName) throws org.xml.sax.SAXException
     {
         System.out.println("ELEMENT: " + qName);
         System.out.println("  VALUE: " + charBuf.toString());
         charBuf.setLength(0); // reset char buffer
     }
 
     public void characters(char[] chars, int start, int length) throws org.xml.sax.SAXException
     {
         // collect all characters (can be called multiple times per element!)
         charBuf.append(chars, start, length);
     }
 };
 
 XMLSAXParser sxparser = null;
 try
 {
     MyHandler handler = new MyHandler();
     sxparser = new XMLSAXParser(false);
     sxparser.setContentHandler(handler);
     sxparser.parse("<?xml version=\"1.0\"?><rootje><elt1>Dit is element 1\nmet een
     newline</elt1><elt2>Dit is element 2\nmet een newline</elt2><leeg/></rootje>");
 }
 catch(SAXException ex)
 {
     System.out.println("parsing error: " + ex);
     System.out.println(sxparser.getErrors());
 }
 
Since:
5.0
Author:
IdJ
  • Constructor Details

    • XmlSAXParser

      public XmlSAXParser()
      Create a non-validating SAX parser (that doesn't use Namespaces and doesn't use Schemas).
  • Method Details

    • getErrors

      public String[] getErrors()
      Get a list of parse errors, if any.
      Returns:
      array of errors, may be empty, but never null.
    • setContentHandler

      public void setContentHandler(ContentHandler handler)
      Set a SAX2 ContentHandler to handle the content events for this parser.
      Parameters:
      handler - The content handler that should be used.
    • setEntityResolver

      public void setEntityResolver(EntityResolver er)
      Set a SAX2 Entity Resolver to handle system entities.
      Parameters:
      er - The entity resolver that should be used.
    • setValidating

      public void setValidating(boolean validating)
      enable or disable DTD-validating. Default=false.
      Parameters:
      validating - Boolean indicating if the parser should validate the XML.
    • setNamespaceAware

      public void setNamespaceAware(boolean aware)
      enable or disable Namespace-awareness. Default=false.
      Parameters:
      aware - Boolean indicating if the parser should consider name spaces.
    • parse

      public void parse(String doc) throws SAXException
      Parses the given XML doc. You must have set a ContentHandler to process the actual content, otherwise nothing serious is done. You may call this again for a new document after parsing has finished.
      Parameters:
      doc - an XML document as a String
      Throws:
      SAXException - in case of XML errors
      See Also:
    • parse

      public void parse(InputSource source) throws SAXException, IOException
      Parses the given SAX2 input source. You must have set a ContentHandler to process the actual content, otherwise nothing serious is done. You may call this again for a new document after parsing has finished.
      Parameters:
      source - a SAX2 input source from which the XML is read
      Throws:
      SAXException - in case of XML errors
      IOException - when the source can't be read
      See Also:
    • getParseTime

      public long getParseTime()
      Get the time in milliseconds it took for the parse method to run.
      Returns:
      The parse time in milliseconds.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • cleanup

      public void cleanup()
      Clean up internal storage (such as the errorHandler's list of errors).