Procedure for Using the Generated C++ Encode Method

NOTE: As of version 1.1, C++ exceptions are no longer used. Therefore, any application programs that used only try/catch blocks to detect errors will not work properly. The return status code from the encode method is the only mechanism used to report error conditions.

The procedure to encode an XML message using the generated C++ encode method is as follows:

  1. Create an instance of the generated type class to hold the data to be encoded.

  2. Create an instance of an output stream or message buffer object to which the encoded XML message will be written.

  3. Create an instance of the generated global element control class to link the generated type class instance with the message buffer or stream instance.

  4. Populate the generated type class instance created in step 2 with data to be encoded.

  5. Invoke the control class encode method.

  6. If encoding was successful (indicated by return status equal to zero), the start-of-message pointer can be obtained by calling the message buffer getMsgPtr method (note: this assumes encoding using a message buffer was done; if a stream was used, the message has already been written to the target).

  7. If encoding failed, the message buffer or stream printErrorInfo method can be invoked to print the reason for failure.

A program fragment that uses this procedure to encode an employee record is as follows:

   #include "rtxmlsrc/rtXmlCppMsgBuf.h"
   #include "employee.h"

   int main (int argc, char** argv)
   {
      int i, stat;
      const char* filename = "message.xml";
      OSBOOL trace = TRUE, verbose = FALSE;
      const OSOCTET* msgptr;

      // Create buffer and control objects

      PersonnelRecord value;
      OSXMLEncodeBuffer buffer;
      personnelRecord_CC pdu (buffer, value);

      if (verbose)
         rtxSetDiag (pdu.getCtxtPtr(), 1);

      // Populate structure of generated type

      ... logic to populate structure here ...

      // Encode

      stat = pdu.encode();

      if (0 == stat) {
         if (trace) {
            msgptr = buffer.getMsgPtr();
            printf ("encoded XML message:\n");
            printf ((const char*)msgptr);
            printf ("\n");
         }
      }

      else {
         printf ("Encoding failed\n");
         buffer.printErrorInfo();
         return stat;
     }

      // Write the encoded message out to the output file

      buffer.write (filename);