midrange.com code scratchpad
Name:
TSTXMLSAX
Scriptlanguage:
Plain Text
Tabwidth:
2
Date:
01/26/2012 09:33:56 pm
IP:
Logged
Description:
Test XML-SAX functionality from RPG REF book Samp
Code:
  1.       * Data structure used as a parameter between
  2.       * the XML-SAX operation and the handling
  3.       * procedure.
  4.       *   - "attrName" is set by the procedure doing the
  5.       *     XML-SAX operation and used by the handling procedure
  6.       *   - "attrValue" is set by the handling procedure
  7.       *     and used by the procedure doing the XML-SAX
  8.       *     operation
  9.       *   - "haveAttr" is used internally by the handling
  10.       *     procedure
  11.      H/COPY QCPYSRC,HSPECLE
  12.  
  13.      D/COPY QCPYSRC,Dspgminf4
  14.      D   xmlRc                       10I 0 OVERLAY(PGMINF:368)
  15.  
  16.      D info            DS                  INZ
  17.      D   attrName                    20A    VARYING
  18.      D   haveAttr                      N
  19.      D   attrValue                   20A    VARYING
  20.  
  21.       * Prototype for procedure "myHandler" defining
  22.       * the communication-area parameter as being
  23.       * like data structure "info"
  24.      D myHandler       PR            10I 0
  25.      D   commArea                           LIKEDS(info)
  26.      D   event                       10I 0  VALUE
  27.      D   string                        *    VALUE
  28.      D   stringLen                   20I 0  VALUE
  29.      D   exceptionId                 10I 0  VALUE
  30.  
  31.  
  32.      D xMLDOC          S            265A   VARYING
  33.       /free
  34.         // The purpose of the following XML-SAX operation
  35.         // is to obtain the value of the first "companyname"
  36.         // attribute found in the XML document.
  37.  
  38.         // The communication area "info" is initialized with
  39.         // the name of the attribute whose value is
  40.         // to be obtained from the XML document.
  41.         attrName = 'title';
  42.         Xmldoc = '/QIBM/ProdData/xmltoolkit2/xml5_6_0/samples/Data/'
  43.                +'AddressBook.Xml';
  44.  
  45.         // Start SAX processing.  The procedure "myHandler"
  46.         // will be called for every SAX event; the first
  47.         // parameter will be the data structure "info".
  48.         xml-sax(e) %handler(myHandler : info) %xml(xmldoc :'doc=file');
  49.         // The XML-SAX operation is complete.  The
  50.         // communication area can be checked to get the
  51.         // value of the attribute.
  52.         if not %error() and attrValue <> '';
  53.           dsply (attrName + '=' + attrValue);
  54.         endif;
  55.  
  56.        *Inlr = *On;
  57.        Return;
  58.       /End-Free
  59.       // The SAX handling procedure "myHandler"
  60.      P myHandler       B
  61.      D                 PI            10I 0
  62.      D   comm                               LIKEDS(info)
  63.      D   event                       10I 0  VALUE
  64.      D   string                        *    VALUE
  65.      D   stringLen                   20I 0  VALUE
  66.      D   exceptionId                 10I 0  VALUE
  67.      D value           S          65535A    BASED(string)
  68.      D ucs2value       S          16383C    BASED(string)
  69.      D rc              S             10I 0  INZ(0)
  70.       /free
  71.  
  72.           select;
  73.  
  74.  
  75.  
  76.           // When the event is a "start document" event,
  77.           // the handler can initialize any internal
  78.           // subfields in the communication area.
  79.           when event = *XML_START_DOCUMENT;
  80.              comm.haveAttr = *OFF;
  81.  
  82.           // When the event is an "attribute name" event,
  83.           // and the value of the event is the required
  84.           // name, the internal subfield "haveAttr" is
  85.           // set to *ON.  If the next event is an
  86.           // attribute-value event, the value will be
  87.           // saved in the "attrValue" subfield.
  88.           when event = *XML_ATTR_NAME;
  89.            If %subst(value : 1 : stringLen) = comm.attrName;
  90.              comm.haveAttr = *ON;
  91.              comm.attrValue = '';
  92.            Endif;
  93.  
  94.           // When "haveAttr" is on, the data from any
  95.           // attribute-value should be saved in the "attrValue"
  96.           // string until the *XML_END_ATTR event occurs
  97.           when comm.haveAttr;
  98.              select;
  99.              when event = *XML_ATTR_CHARS
  100.              or   event = *XML_ATTR_PREDEF_REF;
  101.                 comm.attrValue +=
  102.                     %subst(value : 1 : stringLen);
  103.              when event = *XML_ATTR_UCS2_REF;
  104.                 stringLen = stringLen / 2;
  105.                 comm.attrValue +=
  106.                     %char(%subst(ucs2value : 1 : stringLen));
  107.              when event = *XML_END_ATTR;
  108.                 // We have the entire attribute value
  109.                 // so no further parsing is necessary.
  110.                 // A non-zero return value tells the
  111.                 // RPG runtime that the handler does
  112.                 // not want to continue the operation
  113.                 rc = -1;
  114.             endsl;
  115.  
  116.           endsl;
  117.  
  118.         return rc;
  119.       /end-free
  120.      P                 E
  121.  
  122.  
© 2004-2019 by midrange.com generated in 0.005s valid xhtml & css