Handling XML binding error and warning messages

XML specifications define errors and warnings. You handle them by writing and declaring an XML error handler. The API for exceptions defines what the XML parser returns. The exception handler uses the SAX protocol.

About this task

There are three types of error in the XML specification: error, fatal error and warning.

The package ilog.rules.xml defines the error classes and exceptions returned by an XML file parser.

In most cases, you pass a schema, an XML document, and a handler object to the XML driver, which processes the XML and invokes the appropriate methods.

Diagram showing the main APIs for XML binding

A user handler that is linked to a Simple API for XML (SAX) parser is notified of any SAX event during the parsing of a schema or an XML document. SAX is the protocol that most servlets and network-oriented programs use to transmit and receive XML documents because it is the fastest and least memory-intensive mechanism that is currently available for dealing with XML documents.

Rather than building a complete representation of the XML document, the SAX parser executes a series of events as it reads the document from beginning to end. These events are passed to the event handler, which provides access to the contents of the document.

Diagram describing event handling

Procedure

To implement and declare an XML error handler:

  1. Use the IlrXmlErrorHandler interface to filter or declare new errors during XML parsing.
  2. Implement the IlrXmlErrorHandler interface and declare it to an XML driver during its initialization to activate the XML binding error handling process.

    Use the methods processWarning and or processError to send errors andwarnings to this handler.

  3. Declare and memorize notified errors and warnings by calling the IlrXmlErrorManager methods.
    Diagram describing error handling
  4. Implement a simple user error handler, using following code:
    Public class MyXmlErrorHandler
    Implements IlrXmlErrorHandler
    {    
        IlrXmlErrorManager manager = null;
        void processWarning ( IlrXmlError error ) throws 
    IlrXmlFatalErrorException
        {       
           manager.insertError ( error, this );
        }    
        void processError ( IlrXmlError error ) throws IlrXmlFatalErrorException 
        {       
           manager.insertError ( error, this );    
        }    
        ...
        void setErrorManager ( IlrXmlErrorManager manager )    
        {       
           this.manager = manager;    
        }
    }

    The handler transforms any XML warning into an error.

  5. Build a new XML driver with the previous error handler using the following code:
    // configuration setting
    IlrResources resource = new IlrResources();
    resource.put( "ilog.rules.xml.errorHandlerClass", "MyXmlErrorHandler" );
    ...
    IlrXmlHelper.createDataDriver(resource);