Example 2: An XML Converter Class

The ASN1C XML Encoding Rules (XER) encode and decode capabilities were presented in an earlier section of this document. An alternate way to create an XML document from ASN.1 data is through the event handler interface.

It turns out that with event handlers, this conversion is fairly easy. As the handler events fire, all of the required symbolic data is passed out to generate an XML document. The programmer is free to massage this data any way he or she wants to comply with whatever DTD or XML Schema is in use.

The ToXML sample program demonstrates the conversion of ASN.1 data to XML using event handlers. The sample is not intended to be a robust implementation – it is merely designed to provide guidance in how one would go about doing this transformation.

The sample program can be found in the java/sample_ber/ToXML subdirectory within the ASN1C installation. The complete class definition for the XMLHandler class is as follows:

   class XMLHandler implements Asn1NamedEventHandler {
      protected String mVarName;
      protected int mIndentSpaces = 0;

   public XMLHandler (String varName) {
      mVarName = varName;
      System.out.println ("<" + mVarName + ">");
      mIndentSpaces += 3;
   }

   public void startElement (String name, int index) {
      indent();
      System.out.println ("<" + name + ">");
      mIndentSpaces += 3;
   }

   public void endElement (String name, int index) {
      mIndentSpaces -= 3;
      indent ();
      System.out.println ("</" + name + ">");
   }

   public void characters (String svalue, short typeCode) {
      indent ();
      String typeName = new String (Asn1Type.getTypeName(typeCode));
      typeName.replace (' ', '_');
      System.out.print ("<" + typeName + ">");
      System.out.print (svalue);
      System.out.println ("</" + typeName + ">");
   }

   public void finished () {
      System.out.println ("</" + mVarName + ">");
   }

   private void indent () {
      for (int i = 0; i < mIndentSpaces; i++)
         System.out.print (" ");
      }
   }

This is very similar to the PrintHandler class defined earlier. The startElement method simply opens an XML element block:

   public void startElement (String name, int index) {
      indent();
      System.out.println ("<" + name + ">");
      mIndentSpaces += 3;
   }

The endElement method closes it:

   public void endElement (String name, int index) {
      mIndentSpaces -= 3;
      indent ();
      System.out.println ("</" + name + ">");
   }

The characters method outputs the data with a type wrapper:

   public void characters (String svalue, short typeCode) {
      indent ();
      String typeName = new String (Asn1Type.getTypeName(typeCode));
      typeName.replace (' ', '_');
      System.out.print ("<:" + typeName + ">");
      System.out.print (svalue);
      System.out.println ("</:" + typeName + ">");
   }

This illustrates the use of the typeCode argument for obtaining information on the ASN.1 type of the data. Note that this is a simplified version of an XER formatting method. A true implementation would need to do some massaging of the stringified data to fit the XER rules which, in general, do not follow the ASN.1 value formatting rules. The implementation would also need some logic to check if the type wrapper should be output or not; it is not always done in certain cases.

Finally note the constructor and finished method. The constructor prints out the outer-level wrapper tag. Since Java does not have destructors, a finished method is defined to terminate this tag. This method must be called manually from within the application program after the Java decode method. See the Reader.java program to see how this is done.

Object registration is done as before in the PrintHandler example. The only difference is that an object of the XMLHandler class is created instead of the PrintHandler class.

When compiled and executed, the output from the Reader program looks like this:

   <PersonnelRecord>
      <name>
         <givenName>
            <IA5String>'John'</IA5String>
         </givenName>
         <initial>
            <IA5String>'P'</IA5String>
         </initial>
         <familyName>
            <IA5String>'Smith'</IA5String>
        </familyName>
      </name>
      <number>
         <INTEGER>51</INTEGER>
      </number>
      <title>
         <IA5String>'Director'</IA5String>
      </title>
      <dateOfHire>
         <IA5String>'19710917'</IA5String>
      </dateOfHire>
      <nameOfSpouse>
         <givenName>
            <IA5String>'Mary'</IA5String>
         </givenName>
         <initial>
            <IA5String>'T'</IA5String>
         </initial>
         <familyName>
            <IA5String>'Smith'</IA5String>
         </familyName>
      </nameOfSpouse>
      <children>
         <element>
            <name>
               <givenName>
                  <IA5String>'Ralph'</IA5String>
               </givenName>
               <initial>
                  <IA5String>'T'</IA5String>
               </initial>
               <familyName>
                  <IA5String>'Smith'</IA5String>
               </familyName>
            </name>
            <dateOfBirth>
               <IA5String>'19571111'</IA5String>
            </dateOfBirth>
         </element>
         <element>
            <name>
               <givenName>
                  <IA5String>'Susan'</IA5String>
               </givenName>
               <initial>
                  <IA5String>'B'</IA5String>
               </initial>
               <familyName>
                  <IA5String>'Jones'</IA5String>
               </familyName>
            </name>
            <dateOfBirth>
               <IA5String>'19590717'</IA5String>
            </dateOfBirth>
         </element>
      </children>
   </PersonnelRecord>

Add an XML document header and you should be able to display this data in XML-enabled browser.