Example 2

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 the DOM parser.

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

/COPY QRPGLECPY,RXSCB
/COPY QRPGLETPL,CELSIUS

Dcl-S gData Like(RXS_Var1Kv_t);
Dcl-S gCelsius Int(10);
Dcl-S gFahrenheit Int(10);
Dcl-S gXmlRequest Like(RXS_Var64Kv_t);
Dcl-S gXmlResponse Like(RXS_Var64Kv_t);
Dcl-S gXPath Like(RXS_Var8Kv_t);

Dcl-Ds ComposeDs LikeDS(RXS_ComposeDS_t);
Dcl-Ds TransmitDS LikeDS(RXS_TransmitDS_t);
Dcl-Ds RootDomDS LikeDS(RXS_ParseDomDS_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.katointegrations.com/rxs/installing_certificate_authorities/
  // For example purposes only, turnoff SSL Verification.
  TransmitDS.SSLVerifyHost = RXS_NO;
  gXmlResponse = RXS_Transmit( gXmlRequest : TransmitDS );
endsr;

begsr parse;
  RootDomDS = RXS_OpenDom( gXmlResponse );

  gXPath = RXS_XPath( '/*:Envelope/*:Body/*:FahrenheitToCelsiusResponse'
                    + '/*:FahrenheitToCelsiusResult' );

  gData = RXS_ParseDomToText( gXPath : RootDomDS );

  RXS_CloseDom( RootDomDS );

  gCelsius = %Int( gData );
endsr;


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. 2 - Celsius SOAP XML DOM')

      /COPY QRPGLECPY,RXSCB
      /COPY QRPGLETPL,CELSIUS

     D Template        PR
     D  p                                  Like(RXS_TEMPLATE_PARM)

     D gData           S                   Like(RXS_Var1Kv_t)
     D gCelsius        S             10I 0
     D gFahrenheit     S             10I 0
     D gXmlRequest     S                   Like(RXS_Var64Kv_t)
     D gXmlResponse    S                   Like(RXS_Var64Kv_t)
     D gXPath          S                   Like(RXS_Var8Kv_t)

     D ComposeDS       DS                  LikeDS(RXS_ComposeDS_t)
     D TransmitDS      DS                  LikeDS(RXS_TransmitDS_t)
     D RootDomDS       DS                  LikeDS(RXS_ParseDomDS_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;

         RootDomDS = RXS_OpenDom( gXmlResponse );

         gXPath =
           RXS_XPath('/*:Envelope/*:Body/*:FahrenheitToCelsiusResponse' +
                     '/*:FahrenheitToCelsiusResult' );

         gData = RXS_ParseDomToText( gXPath : RootDomDS );

         RXS_CloseDom( RootDomDS );

         gCelsius = %Int( gData );

       endsr;

      /end-free


     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