This section describes the step-by-step procedure for calling C XML encode functions. This procedure is similar to that for the other encoding methods except that some of the functions used are specific to XML.
Before an XML encode function can be called, the user must first initialize an encoding context block structure. The context block is initialized by calling rtXmlInitContext to initialize a context block structure. The user then must call the rtXmlSetEncBufPtr function to specify a message buffer to receive the encoded message. Specification of a dynamic message buffer is possible by setting the buffer address argument to null and the buffer size argument to zero.
An encode function can then be called to encode the message. If the return status indicates success, then the message will have been encoded in the given buffer. XML encoding starts from the beginning of the buffer and proceeds from low memory to high memory until the message is complete. This differs from BER where encoding was done from back-to-front. Therefore, the buffer start address is where the encoded XML message begins. If a dynamic message buffer was used, the start address of the encoded message can be obtained by calling the rtXmlEncGetMsgPtr function. Since the encoded XML message is nothing more than a null-terminated string in a memory buffer, the standard C library function strlen can be used to obtain the length.
A program fragment that could be used to encode an employee record is as follows:
#include employee.h /* include file generated by ASN1C */ int main (int argc, char** argv) { PersonnelRecord employee; OSCTXT ctxt; OSOCTET msgbuf[4096]; int stat; /* Initialize context and set encode buffer pointer */ stat = rtXmlInitContext (&ctxt); if (0 != stat) { printf ("context initialization failed\n"); rtxErrPrint (&ctxt); return stat; } rtXmlSetEncBufPtr (&ctxt, msgbuf, sizeof(msgbuf)); /* Populate variable with data to be encoded */ employee.name.givenName = “John”; ... /* Encode data */ stat = XmlEnc_PersonnelRecord_PDU (&ctxt, &employee); if (stat) == 0) { /* Note: message can be treated as a null-terminated string in memory */ printf ("encoded XML message:\n"); puts ((char*)msgbuf); printf ("\n"); ... } else error processing... } rtFreeContext (&ctxt); /* release the context pointer */