Example 1

This example program demonstrates the basic structure of an RPG program utilizing RXS to compose an XML request, call a remote web service, and parse the response XML with event-based parsing.

**FREE
Ctl-Opt ActGrp(*Caller) BndDir('RXSBND') Option(*NoDebugIO) ExtBinInt(*Yes) DecEdit('0.')
  Text('Ex. 1 - Celsius SOAP XML');

/COPY QRPGLECPY,RXSCB
/COPY QRPGLETPL,CELSIUS

Dcl-S gFahrenheit Int(10);
Dcl-S gCelsius Int(10);
Dcl-S gXmlRequest Like(RXS_Var64Kv_t);
Dcl-S gXmlResponse Like(RXS_Var64Kv_t);

Dcl-Ds ComposeDS LikeDS(RXS_ComposeDS_t);
Dcl-Ds TransmitDS LikeDS(RXS_TransmitDS_t);
Dcl-Ds ParseDS LikeDS(RXS_ParseDS_t);
Dcl-Ds ErrorDS LikeDS(RXS_CatchThrowErrorDS_t);

monitor;
  gFahrenheit = 100;

  exsr compose;
  exsr transmit;
  exsr parse;

  RXS_ResetDS( ErrorDS : RXS_DS_TYPE_CATCHTHROWERROR );
  ErrorDS.MessageId = 'RXS9897';
  ErrorDS.MessageData = 'Celsius Temp: ' + %Trim ( %EditC( gCelsius : '3' ) )
                      + ' degrees' ;
  ErrorDS.ThrowToCaller = RXS_YES;
  RXS_Throw( ErrorDS );
on-error;

endmon;

*INLR = *On;
return;

begsr compose;
  RXS_ResetDS( ComposeDS : RXS_DS_TYPE_COMPOSE );
  ComposeDS.TemplateProcedure = %Paddr( Template );
  RXS_StartComposeEngine( ComposeDS );

  RXS_ComposeVariable( fahrenheit : %Char( gFahrenheit ) );
  RXS_ComposeSection( content );

  gXmlRequest = RXS_GetComposeBuffer();
endsr;

begsr transmit;
  RXS_ResetDS( TransmitDS : RXS_DS_TYPE_TRANSMIT );
  TransmitDS.URI = 'https://www.w3schools.com/xml/tempconvert.asmx';
  TransmitDS.HeaderSOAPAction
    = '"https://www.w3schools.com/xml/FahrenheitToCelsius"';
  TransmitDS.LogFile = '/tmp/celsius.txt';
  TransmitDS.HeaderContentType = 'text/xml; charset=utf-8';
  TransmitDS.RequestCcsid = RXS_CCSID_UTF8;
  // SSL Certificates should always be installed for security reasons,
  //   but for now ignore any missing certificates
  //   See this page for instructions on installing needed certificates:
  //   https://isupport.krengeltech.com/rxs/installing_certificate_authorities/
  // For example purposes only, turnoff SSL Verification.
  TransmitDS.SSLVerifyHost = RXS_NO;
  gXmlResponse = RXS_Transmit( gXmlRequest : TransmitDS );
endsr;

begsr parse;
  RXS_ResetDS( ParseDS : RXS_DS_TYPE_PARSE );
  ParseDS.GlobalHandler = %Paddr( XmlHandler );
  RXS_Parse( gXmlResponse : ParseDS );
endsr;


Dcl-Proc XmlHandler;
  Dcl-Pi *N;
    pType Char(10) Value;
    pXPath VarChar(1024) Value;
    pData Pointer Value;
    pDataLen Int(10) Value;
  End-Pi;

  Dcl-S ParsedData Like(RXS_Var1Kv_t);

  select;
    when pXPath = '/Envelope/Body/FahrenheitToCelsiusResponse'
                + '/FahrenheitToCelsiusResult/';
      monitor;
        ParsedData = RXS_STR( pData : pDataLen );
        gCelsius = %Int( ParsedData );
      on-error 105;
        gCelsius = 0;
      endmon;

  endsl;

End-Proc;


Dcl-Proc Template;
  Dcl-Pi *N;
    p Like(RXS_TEMPLATE_PARM);
  End-Pi;

  // Template RPG source was created from the actual template
  //  STMF using the CRTRPGTPL command.
  //
  // The RPG Template source is copied from QRPGLETPL by using
  //  /copy once in the D-specs and again below.

  /COPY QRPGLETPL,CELSIUS
End-Proc;
 
     H DFTACTGRP(*NO) ACTGRP(*CALLER) BNDDIR('RXSBND') OPTION(*NODEBUGIO)
     H EXTBININT(*YES) DECEDIT('0.')
     H TEXT('Fixed Format Ex. 1 - Celsius SOAP XML')

      /COPY QRPGLECPY,RXSCB
      /COPY QRPGLETPL,CELSIUS

     D Template        PR
     D  p                                  Like(RXS_TEMPLATE_PARM)

     D XmlHandler      PR
     D  pType                        10A   Value
     D  pXPath                     1024A   Value Varying
     D  pData                          *   Value
     D  pDataLen                     10I 0 Value

     D gFahrenheit     S             10I 0
     D gCelsius        S             10I 0
     D gXmlRequest     S                   Like(RXS_Var64Kv_t)
     D gXmlResponse    S                   Like(RXS_Var64Kv_t)

     D ComposeDS       DS                  LikeDS(RXS_ComposeDS_t)
     D TransmitDS      DS                  LikeDS(RXS_TransmitDS_t)
     D ParseDS         DS                  LikeDS(RXS_ParseDS_t)
     D ErrorDS         DS                  LikeDS(RXS_CatchThrowErrorDS_t)

      /free

       monitor;

         gFahrenheit = 100;

         exsr compose;

         exsr transmit;

         exsr parse;

         RXS_ResetDS( ErrorDS : RXS_DS_TYPE_CATCHTHROWERROR );
         ErrorDS.MessageId = 'RXS9897';
         ErrorDS.MessageData = 'Celsius Temp: '
           + %Trim ( %EditC( gCelsius : '3' ) + 'degrees');
         ErrorDS.ThrowToCaller = RXS_YES;
         RXS_Throw( ErrorDS );
       on-error;

       endmon;

       *INLR = *On;
       return;


       begsr compose;

         RXS_ResetDS( ComposeDS : RXS_DS_TYPE_COMPOSE );
         ComposeDS.TemplateProcedure = %Paddr(Template);
         RXS_StartComposeEngine(ComposeDS);

         RXS_ComposeVariable( fahrenheit : %Char(gFahrenheit) );
         RXS_ComposeSection( content );

         gXmlRequest = RXS_GetComposeBuffer();

       endsr;


       begsr transmit;

         RXS_ResetDS( TransmitDS : RXS_DS_TYPE_TRANSMIT );
         TransmitDS.URI =
           'https://www.w3schools.com/xml/tempconvert.asmx';
         TransmitDS.HeaderSOAPAction =
           '"https://www.w3schools.com/xml/FahrenheitToCelsius"';
         TransmitDS.LogFile = '/tmp/celsius.txt';
         TransmitDS.HeaderContentType = 'text/xml; charset=utf-8';
         TransmitDS.RequestCcsid = RXS_CCSID_UTF8;
         // SSL Certificates should always be installed for security reasons,
         //   but for now ignore any missing certificates
         //   See this page for instructions on installing needed certificates:
         //   https://isupport.krengeltech.com/rxs/installing_certificate_authorities/
         // For example purposes only, turnoff SSL Verification.
         TransmitDS.SSLVerifyHost = RXS_NO;
         gXmlResponse = RXS_Transmit( gXmlRequest : TransmitDS );

       endsr;

       begsr parse;

         RXS_ResetDS( ParseDS : RXS_DS_TYPE_PARSE );
         ParseDS.GlobalHandler = %Paddr( XmlHandler );
         RXS_Parse( gXmlResponse : ParseDS );

       endsr;

      /end-free

     P XmlHandler      B
     D                 PI
     D  pType                        10A   Value
     D  pXPath                     1024A   Value Varying
     D  pData                          *   Value
     D  pDataLen                     10I 0 Value
     D
     D ParsedData      S                   Like(RXS_Var1Kv_t)
      /free

       select;
         when pXPath = '/Envelope/Body/FahrenheitToCelsiusResponse'
                     + '/FahrenheitToCelsiusResult/';
           monitor;
             ParsedData = RXS_STR( pData : pDataLen );
             gCelsius = %Int( ParsedData );
           on-error 105;
             gCelsius = 0;
           endmon;

       endsl;

      /end-free
     P                 E

     P Template        B
     D                 PI
     D  p                                  Like(RXS_TEMPLATE_PARM)

        // Template RPG source was created from the actual template
        //  STMF using the CRTRPGTPL command.
        //
        // The RPG Template source is copied from QRPGLETPL by using
        //  /copy once in the D-specs and again below.

      /COPY QRPGLETPL,CELSIUS
     P                 E
 
      /IF NOT DEFINED(CELSIUS)
      /DEFINE CELSIUS
     D CONTENT...
     D                 C                   'CONTENT'
     D fahrenheit...
     D                 C                   'fahrenheit'
      /ELSE
      /FREE
       p  = '';
       p += '::CONTENT' + x'15';
       p += '' + x'15';
       p += '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema';
       p += '-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmln';
       p += 's:soap12="http://www.w3.org/2003/05/soap-envelope">' + x'15';
       p += '  ' + x'15';
       p += '    ' + x'15';
       p += '      ' + '.:fahrenheit:.' + '' + x'15';
       p += '    ' + x'15';
       p += '  ' + x'15';
       p += '</soap12:Envelope>' + x'15';

      /END-FREE
      /ENDIF