Example 3

This example program demonstrates the basic structure of an RPG program utilizing the RXS DOM parser to parse an XML document with multiple repeating elements.

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

/COPY QRPGLECPY,RXSCB

Dcl-Ds gRootDomDS LikeDS(RXS_ParseDomDS_t);
Dcl-Ds gBookDomDS LikeDS(RXS_ParseDomDS_t);
Dcl-Ds gReviewDomDS LikeDS(RXS_ParseDomDS_t);

Dcl-S gXml Like(RXS_Var64Kv_t);
Dcl-S gData Like(RXS_Var64Kv_t);
Dcl-S gXPath Like(RXS_Var8Kv_t);
Dcl-S x Uns(10);
Dcl-S y Uns(10);

monitor;
  gXml = '<?xml version="1.0" encoding="UTF-8"?>'
       + '<bookstore xmlns:test="testnamespace">'
       + '<book category="children">'
       + '<title lang="en"><![CDATA[Charlotte''s Web]]></title>'
       + '<author>E. B. White</author>'
       + '<year>1952</year>'
       + '<price>5.99</price>'
       + '<review><text>What a great book!</text></review>'
       + '<review><text>Highly recommended.</text></review>'
       + '</book>'
       + '<book category="cooking">'
       + '<title lang="en">Everyday Italian</title>'
       + '<author>Giada De Laurentiis</author>'
       + '<year>2005</year>'
       + '<price>30.00</price>'
       + '</book>'
       + '<book category="children">'
       + '<title lang="en">Harry Potter</title>'
       + '<author>J K. Rowling</author>'
       + '<year>2005</year>'
       + '<price>29.99</price>'
       + '</book>'
       + '</bookstore>';

  gRootDomDS = RXS_OpenDom( gXml );

  gXPath = RXS_XPath( '/*:bookstore/*:book' );
  gBookDomDS = RXS_ParseDomToDom( gXPath : gRootDomDS );

  for x = 1 to gBookDomDS.NodeCount;
    gXPath = RXS_XPath( '*:book[%u]/*:title' : x );
    gData = RXS_ParseDomToText( gXPath : gBookDomDS );
    RXS_JobLog( 'Title: %s' : gData );

    gXPath = RXS_XPath( '*:book[%u]/*:review' : x );
    gReviewDomDS = RXS_ParseDomToDom( gXPath : gBookDomDS );

    for y = 1 to gReviewDomDS.NodeCount;
      gXPath = RXS_XPath( '*:review[%u]/*:text' : y );
      gData = RXS_ParseDomToText( gXPath : gReviewDomDS );
      RXS_JobLog( 'Review: %s' : gData );
    endfor;
  endfor;

  RXS_CloseDom( gRootDomDS );

on-error;

endmon;

*INLR = *On;
return;
 
     H DFTACTGRP(*NO) ACTGRP(*CALLER) BNDDIR('RXSBND') OPTION(*NODEBUGIO)
     H EXTBININT(*YES) DECEDIT('0.')
     H TEXT('Fixed Format Ex. 3 - XML DOM Parsing')

      /COPY QRPGLECPY,RXSCB

     D gRootDomDS      DS                  LikeDS(RXS_ParseDomDS_t)
     D gBookDomDS      DS                  LikeDS(RXS_ParseDomDS_t)
     D gReviewDomDS    DS                  LikeDS(RXS_ParseDomDS_t)

     D gXml            S                   Like(RXS_Var64Kv_t)
     D gData           S                   Like(RXS_Var64Kv_t)
     D gXPath          S                   Like(RXS_Var8Kv_t)
     D x               S             10U 0
     D y               S             10U 0

      /free

       monitor;

         gXml = '<?xml version="1.0" encoding="UTF-8"?>'
              + '<bookstore xmlns:test="testnamespace">'
              + '<book category="children">'
              + '<title lang="en"><![CDATA[Charlotte''s Web]]></title>'
              + '<author>E. B. White</author>'
              + '<year>1952</year>'
              + '<price>5.99</price>'
              + '<review><text>What a great book!</text></review>'
              + '<review><text>Highly recommended.</text></review>'
              + '</book>'
              + '<book category="cooking">'
              + '<title lang="en">Everyday Italian</title>'
              + '<author>Giada De Laurentiis</author>'
              + '<year>2005</year>'
              + '<price>30.00</price>'
              + '</book>'
              + '<book category="children">'
              + '<title lang="en">Harry Potter</title>'
              + '<author>J K. Rowling</author>'
              + '<year>2005</year>'
              + '<price>29.99</price>'
              + '</book>'
              + '</bookstore>';

         gRootDomDS = RXS_OpenDom(  gXml );

         gXPath = RXS_XPath( '/*:bookstore/*:book' );
         gBookDomDS = RXS_ParseDomToDom( gXPath : gRootDomDS );

         for x = 1 to gBookDomDS.NodeCount;

           gXPath = RXS_XPath( '*:book[%u]/*:title' : x );
           gData = RXS_ParseDomToText( gXPath : gBookDomDS );
           RXS_JobLog( 'Title: %s' : gData );

           gXPath = RXS_XPath( '*:book[%u]/*:review' : x );
           gReviewDomDS = RXS_ParseDomToDom( gXPath : gBookDomDS );

           for y = 1 to gReviewDomDS.NodeCount;

             gXPath = RXS_XPath( '*:review[%u]/*:text' : y );
             gData = RXS_ParseDomToText( gXPath : gReviewDomDS );
             RXS_JobLog( 'Review: %s' : gData );

           endfor;

         endfor;

         RXS_CloseDom( gRootDomDS );

       on-error;

       endmon;

       *INLR = *On;
       return;

      /end-free