TOC PREV NEXT INDEX


Generated XER Decode Methods



The code generated to decode XML messages is different than that of the other encoding rules. This is because off-the-shelf XML parser software is used to parse the XML documents to be decoded. This software contains a common interface known as the Simple API for XML (or SAX) that is a de-facto standard that is supported by most parsers. ASN1C generates an implementation of the content handler interface defined by this standard. This implementation receives the parsed XML data and uses it to populate the structures generated by the compiler.

The default XML parser used for Java is the XERCES parser developed by the Apache Software Foundation (http://xml.apache.org). This is open source software and implementations of the parser are available for both C++ and Java. As mentioned, since SAX is a de-facto standard, it should be a relatively straightforward process to use the generated handlers with any other 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:




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;
 
      }
 
   }
 
}
 


Figure 10 - XER Decoding Example

Objective Systems, Inc.

102 Pickering Way, Suite #506
Exton, Pennsylvania 19341
http://www.obj-sys.com
Phone: (484) 875-9841
Toll-free: (877) 307-6855 (US only)
Fax: (484) 875-9830
info@obj-sys.com
TOC PREV NEXT INDEX

Copyright © 1997-2004 Objective Systems,Inc.
All Rights Reserved.


This document may be distributed in any form, electronic
or otherwise, provided that it is distributed in its entirety
and that the copyright and this notice are included.