Generated Decode Methods when Using the SAX parser

As a SAX parser parses XML, it invokes callbacks on a content handler interface to notify any interested parties of the data it has parsed. ASN1C generates an implementation of the content handler interface as an inner class of each class generated for an ASN.1 production. This content handler populates the fields of the data class as the XML is parsed.

The default SAX parser used is the XERCES parser developed by the Apache Software Foundation ( http://xml.apache.org ). Since SAX is a de-facto standard, it should be relatively straightforward to use the generated handlers with any other implementation of SAX parser.

A diagram showing the components used in the XML decode process is as follows:

Step 1: Generate code:

Step 2: Build Application:

ASN1C generates code to implement the following methods defined in SAX content handler interface:

   startElement
           
           characters
           
           endElement

The interface defines other methods that can be implemented as well, but these are sufficient to decode XER encoded data. These methods are added to an inner SAX handler class generated for each ASN.1 production.

The procedure to invoke the generated decode method is similar to that for the other encoding rules. It is as follows:

  1. Instantiate an XMLReader object. The XML parser interface should provide a factory method for creating an object of this type for any vendor-specific XML parser implementation.

  2. Instantiate a generated Java <ProdName> object to hold the decoded message data.

  3. Invoke the <ProdName> object decode method passing the reader created in step 1 and the URI of the XML document to be parsed. This method initiates and invokes the XML parser's parse method to parse the document. This, in turn, invokes the generated SAX handler methods.

  4. Methods within the <ProdName> object can now be used to access the decoded data. The member variables that were declared to be public can be accessed directly.

  5. Error handling is accomplished using a try-catch block to catch SAX exceptions.

A program fragment that could be used to decode an employee record is as follows:

   public class Reader {
     public static void main (String args[]) {
        String filename = "employee.xml";
        String vendorParserClass =
        "org.apache.xerces.parsers.SAXParser";
        
        try {
           // Create an XML reader object
           
           XMLReader reader =
           XMLReaderFactory.createXMLReader (vendorParserClass);
           
           // Read and decode the message
           
           PersonnelRecord personnelRecord = new PersonnelRecord ();
           personnelRecord.decode (reader, filename);
           if (trace) {
              System.out.println ("Decode was successful");
              personnelRecord.print (System.out, "personnelRecord", 0);
           }
        }
        catch (Exception e) {
           System.out.println (e.getMessage());
           e.printStackTrace();
           return;
        }
     }
   }