DOMRECUR

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


H BNDDIR('RXSBND') DFTACTGRP(*NO) ACTGRP(*CALLER)
 /copy qrpglecpy,rxscb
D gRootDomDS      DS                  LikeDS(RXS_ParseDomDS_t) Inz
D gBookDomDS      DS                  LikeDS(RXS_ParseDomDS_t) Inz
D gReviewDomDS    DS                  LikeDS(RXS_ParseDomDS_t) Inz
D gXml            S          65535A   Varying(4)
D gData           S          65535A   Varying(4)
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;
  
 /end-free