XML for RPG and Procedural Languages Documentation

Readme
Installation
API Docs
Samples
Programming
License

RPG: SAXCount

The Code: qrpglesrc.SAXCount

    The lines of code that correspond to XML parser initialization, use and clean-up are displayed in blue.


      *   NOTE, there are some code page assumptions in this file that might
      *          not reflect your system environment (CCSID 37)
      * the COPY line for the header file
      /COPY XML4PR400/QRPGLESRC,XML4PR400
     Dtrue             C                   1
     Dfalse            C                   0
     Dparms            S            256A   DIM(10)
     DnumParms         S             10I 0
     Di                S             10I 0

     DaddParm          PR
     Dparm                                 like(parms) const
     Dmsg              S             52A
     DvalScheme        S             10I 0 INZ(Qxml_VALAU)
     DdoNamespaces     S             10I 0 INZ(false)
     DdoSchema         S             10I 0 INZ(false)
     DschemaFullcheck  S             10I 0 INZ(false)
     DInd              S             10I 0 INZ(0)

     DPRTDOC           PR
     DPRERR1           PR
     DPRERR2           PR
     DCOUNTELM         PR
     D  NAME@                          *   VALUE
     D  ATTR@                          *   VALUE
     DSTARTDOC         PR
     DCHARCNT          PR
     D  CHar@                          *   VALUE
     D  CHarlen                      10I 0 VALUE
     DSETLOC           PR
     D  DLOC@                          *   VALUE
     DSAXWARN          PR
     D  SAXE@                          *   VALUE
     DSAXERR           PR
     D  SAXE@                          *   VALUE
     DSAXFATERR        PR
     D  SAXE@                          *   VALUE
     DENVDATA@         S               *   INZ(%ADDR(Qxml_DOMEXCDATA))
     DPRSDATA@         S               *   INZ(%ADDR(Qxml_SAXEXCDATA))
      * Set up pointer to the SAX API callback routines
     DCOUNTHDL@        S               *   PROCPTR
     D                                     INZ(%PADDR('COUNTELM'))
     DSTARTHDL@        S               *   PROCPTR
     D                                     INZ(%PADDR('STARTDOC'))
     DCHARCHDL@        S               *   PROCPTR
     D                                     INZ(%PADDR('CHARCNT'))
     DSETLHDL@         S               *   PROCPTR
     D                                     INZ(%PADDR('SETLOC'))
     DWARNHDL@         S               *   PROCPTR
     D                                     INZ(%PADDR('SAXWARN'))
     DERRHDL@          S               *   PROCPTR
     D                                     INZ(%PADDR('SAXERR'))
     DFATHDL@          S               *   PROCPTR
     D                                     INZ(%PADDR('SAXFATERR'))
     DSAXParse@        S               *
     DDOCHNDLR@        S               *
     DERRHNDLR@        S               *
     DDomDoc@          S               *
     DPID@             S               *
     DDomNodeList@     S               *
      * Modify the following with a valid XML file name
     DXmlFile          S             90A   INZ('/MyXMLFile.xml')
     Dtransmsg         S             90A
     DXmlFile@         S               *
     DElemCount        S             10I 0 INZ(0)
     DAttrCount        S             10I 0 INZ(0)
     DCharCount        S             10I 0 INZ(0)
     DTotalAttr        S             10I 0 INZ(0)
     DStartCnt         S             10I 0 INZ(0)
     DCOL              S             10I 0 INZ(0)
     DLINE             S             10I 0 INZ(0)
     DMSG@             S               *
     DBYTESAVAL        S             10I 0
     DBYTESPROV        S             10I 0 INZ(90)
     DTRANSMSG@        S               *   INZ(%ADDR(transmsg))
     Dvaltstr          S              3A   INZ('-n')
     DOUTSTR@          S               *
      *  Allocate local string areas                                  *
     C                   ALLOC     256           XmlFile@
     C                   ALLOC     256           OUTSTR@
      *  Set up the parameters list
     C     *ENTRY        PLIST
     C                   PARM                    PARM1           256
     C                   PARM                    PARM2           256
     C                   PARM                    PARM3           256
     C                   PARM                    PARM4           256
     C                   PARM                    PARM5           256
      *  Make sure at least one parameter was passed
     C                   IF        %PARMS < 1
     C                   EVAL      %str(OUTSTR@:256)='You must specify the '
     C                                    + 'name of the XML file you want to '
     C                                    + 'parse as a parameter. ' + x'25'
     C                                    + '-v=xxxx  Validation scheme '
     C                                    + '[always | never | auto*]' + x'25'
     C                   CALLP     QxmlGenPrint(OUTSTR@:0)
     C                   EVAL      %str(OUTSTR@:256)=
     C                                      '-n       '
     C                                    + 'Enable name space processing. '
     C                                    + 'Defaults to off.' + x'25'
     C                                    + '-s       '
     C                                    + 'Enable schema processing. '
     C                                    + 'Defaults to off.' + x'25'
     C                                    + '-f       '
     C                                    + 'Enable full schema constraint '
     C                                    + 'checking.  Defaults to off.'
     C                                    + x'25'
     C                                    + '* = Default if not provided '
     C                                    + 'explicitly' + x'25'
     C                   CALLP     QxmlGenPrint(OUTSTR@:0)
     C                   RETURN
     C                   ENDIF
      *  copy paramters into an array for easier processing
     C                   eval      ind = 0
     C                   eval      numParms = %parms-1

     C                   if        %parms > 0
     C                   callp     addParm (parm1)
     C                   endif
     C                   if        %parms > 1
     C                   callp     addParm (parm2)
     C                   endif
     C                   if        %parms > 2
     C                   callp     addParm (parm3)
     C                   endif
     C                   if        %parms > 3
     C                   callp     addParm (parm4)
     C                   endif
     C                   if        %parms > 4
     C                   callp     addParm (parm5)
     C                   endif
      *  loop thru the parameters and validate them.
     C                   FOR       ind = 1 to numParms
     C                   EVAL      VALTSTR = %SUBST(parms(ind):1:2)
     C                   if        valtstr = '-v'
     C                   if        %SUBST(parms(ind):4:5) = 'never'
     C                   EVAL      valScheme = Qxml_VALNO
     C                   else
     C                   if        %SUBST(parms(ind):4:4) = 'auto'
     C                   EVAL      valScheme = Qxml_VALAU
     C                   else
     C                   if        %SUBST(parms(ind):4:6) = 'always'
     C                   EVAL      valScheme = Qxml_VALAL
     C                   else
     C                   EVAL      %str(OUTSTR@:256) = 'unknown -v value'
     C                                               + x'25'+x'00'
     C                   CALLP     QxmlGenPrint(OUTSTR@:0)
     C                   endif
     C                   endif
     C                   endif
     C                   else
     C                   if        valtstr = '-n'
     C                   EVAL      doNamespaces = true
     C                   else
     C                   if        valtstr = '-s'
     C                   EVAL      doSchema = true
     C                   else
     C                   if        valtstr = '-f'
     C                   EVAL      schemaFullCheck = true
     C                   endif
     C                   endif
     C                   endif
     C                   endif
     C                   endfor

      *  extract file name
     C                   EVAL      %str(XmlFile@:256) = parms(ind)
     C                   EVAL      XmlFile = %str(XmlFile@)
      *  Initialize XML environment, provide pointer to DOM exception area
     C                   CALLP     QxmlInit(ENVDATA@)
      *  Create a SAX Parser object, set various validation options
     C                   EVAL      SAXParse@ = QxmlSAXPARSER_new
     C                   CALLP     QxmlSAXParser_setValidationScheme
     C                                 (SAXParse@:
     C                                  valScheme)
     C                   CALLP     QxmlSAXParser_setDoNamespaces
     C                                 (SAXParse@:
     C                                  doNamespaces)
     C                   CALLP     QxmlSAXParser_setDoSchema
     C                                 (SAXParse@:
     C                                  doSchema)
     C                   CALLP     QxmlDOMParser_setValidationSchema...
     C                                 FullChecking
     C                                 (SAXParse@:
     C                                  schemaFullCheck)
      *  Create a document and error handler and register with parser
     C                   EVAL      DOCHNDLR@ = QxmlDocumentHandler_new
     C                   EVAL      ERRHNDLR@ = QxmlErrorHandler_new
     C                   CALLP     QxmlSAXParser_setDocumentHandler
     C                                 (SAXParse@:
     C                                  DOCHNDLR@)
     C                   CALLP     QxmlSAXParser_setErrorHandler
     C                                 (SAXParse@:
     C                                  ERRHNDLR@)
      *  Register the callback routines based on specific SAX Document
      *  and Error handler events (such as STARTDOUCMENT, CHARACTERS, etc)
     C                   CALLP     QxmlDocumentHandler_setCallback
     C                                 (DOCHNDLR@:
     C                                  Qxml_STARTELEMENT:
     C                                  COUNTHDL@)
     C                   CALLP     QxmlDocumentHandler_setCallback
     C                                 (DOCHNDLR@:
     C                                  Qxml_STARTDOCUMENT:
     C                                  STARTHDL@)
     C                   CALLP     QxmlDocumentHandler_setCallback
     C                                 (DOCHNDLR@:
     C                                  Qxml_CHARACTERS:
     C                                  CHARCHDL@)
     C                   CALLP     QxmlDocumentHandler_setCallback
     C                                 (DOCHNDLR@:
     C                                  Qxml_SETDOCLOCATOR:
     C                                  SETLHDL@)
     C                   CALLP     QxmlErrorHandler_setCallback
     C                                 (ERRHNDLR@:
     C                                  Qxml_WARNINGHNDLR:
     C                                  WARNHDL@)
     C                   CALLP     QxmlErrorHandler_setCallback
     C                                 (ERRHNDLR@:
     C                                  Qxml_ERRORHNDLR:
     C                                  ERRHDL@)
     C                   CALLP     QxmlErrorHandler_setCallback
     C                                 (ERRHNDLR@:
     C                                  Qxml_FATALERRORHNDLR:
     C                                  FATHDL@)
      *  Call parser providing XML file name
      *  Note CCSIDd of 37 reflects assumed input CCSID of XML file name
     C                   CALLP     QxmlSAXParser_parse_systemid
     C                                  (SAXParse@:
     C                                   XmlFile@:
     C                                   Qxml_CCSID37:
     C                                   0)
     C                   CALLP     QxmlDocumentHandler_delete(DOCHNDLR@)
     C                   CALLP     QxmlErrorHandler_delete(ERRHNDLR@)
     C                   CALLP     QxmlSAXPARSER_delete(SAXParse@)
     C                   CALLP     QxmlTerm
     C                   DEALLOC                 XmlFile@
      *  Print out a detail line
     C                   EVAL      %str(OUTSTR@:256)='Elements '
     C                                              +%editc(ElemCount:'Z')
     C                                    + ',Attributes '
     C                                    + %editc(TotalAttr:'Z')
     C                                    + ',Characters '
     C                                    + %editc(CharCount:'Z')
     C                                    + x'25' + x'00'
     C                   CALLP     QxmlGenPrint(OUTSTR@:0)
     C                   EVAL      %str(OUTSTR@:256)= 'For File '+XmlFile
     C                                     +x'25'+x'00'
     C                   CALLP     QxmlGenPrint(OUTSTR@:0)
     C                   RETURN
     PPRERR1           B
     DPRERR1           PI
     C                   EVAL      %str(OUTSTR@:256)='Error: '
     C                                    +%trimr(transmsg)+x'00'
     C                   CALLP     QxmlGenPrint(OUTSTR@:0)
     C                   EVAL      %str(OUTSTR@:256)=x'2500'
     C                   CALLP     QxmlGenPrint(OUTSTR@:0)
     C                   RETURN
     PPRERR1           E
     PPRERR2           B
     DPRERR2           PI
     C                   EVAL      %str(OUTSTR@:256) = 'Line #: '
     C                                    +%editc(LINE:'Z')
     C                                    + ' Column #: '
     C                                    +%editc(COL:'Z')
     C                                    + x'25' + x'00'
     C                   CALLP     QxmlGenPrint(OUTSTR@:0)
     C                   RETURN
     PPRERR2           E
     PCOUNTELM         B
     DCOUNTELM         PI
     D  NAME@                          *   VALUE
     D  ATTR@                          *   VALUE
     C                   EVAL      Elemcount = Elemcount+1
     C                   EVAL      Attrcount =
     C                             QxmlAttributeList_getLength(ATTR@)
     C                   EVAL      TotalAttr = TotalAttr+Attrcount
     PCOUNTELM         E
     PSTARTDOC         B
     DSTARTDOC         PI
     C                   EVAL      StartCnt  = StartCnt +1
     PSTARTDOC         E
     PCHARCNT          B
     DCHARCNT          PI
     D  CHar@                          *   VALUE
     D  CHarlen                      10I 0 VALUE
     C                   EVAL      CharCount = CharCount +Charlen
     PCHARCNT          E
     PSETLOC           B
     DSETLOC           PI
     D  DLOC@                          *   VALUE
     C                   EVAL      PID@ = QxmlLOCATOR_getPublicId
     C                                              (DLOC@)
     PSETLOC           E
     PSAXWARN          B
     DSAXWARN          PI
     D  SAXE@                          *   VALUE
     C                   EVAL      LINE=QxmlSAXParseException_getLineNumber
     C                                      (SAXE@)
     C                   EVAL      COL =QxmlSAXParseException_getColumn...
     C                                       Number(SAXE@)
     C                   EVAL      MSG@= QxmlSAXException_getMessage
     C                                      (SAXE@)
     C                   CALLP     QxmlTranscode(MSG@:
     C                                       Qxml_UNICODE:
     C                                       TRANSMSG@:
     C                                       %ADDR(BYTESPROV):
     C                                       %ADDR(BYTESAVAL):
     C                                       Qxml_CCSID37)
     C                   CALLP     PRERR2
     C                   CALLP     PRERR1
     PSAXWARN          E
     PSAXERR           B
     DSAXERR           PI
     D  SAXE@                          *   VALUE
     C                   EVAL      LINE=QxmlSAXParseException_getLineNumber
     C                                       (SAXE@)
     C                   EVAL      COL =QxmlSAXParseException_getColumn...
     C                                       Number(SAXE@)
     C                   EVAL      MSG@= QxmlSAXException_getMessage
     C                                      (SAXE@)
     C                   CALLP     QxmlTRANSCODE(MSG@:
     C                                       Qxml_UNICODE:
     C                                       TRANSMSG@:
     C                                       %ADDR(BYTESPROV):
     C                                       %ADDR(BYTESAVAL):
     C                                       Qxml_CCSID37)
     C                   CALLP     PRERR2
     C                   CALLP     PRERR1
     PSAXERR           E
     PSAXFATERR        B
     DSAXFATERR        PI
     D  SAXE@                          *   VALUE
     C                   EVAL      LINE=QxmlSAXParseException_getLineNumber
     C                                      (SAXE@)
     C                   EVAL      COL =QxmlSAXParseException_getColumn...
     C                                       Number(SAXE@)
     C                   EVAL      MSG@= QxmlSAXException_getMessage
     C                                      (SAXE@)
     C                   CALLP     QxmlTranscode(MSG@:
     C                                       Qxml_UNICODE:
     C                                       TRANSMSG@:
     C                                       %ADDR(BYTESPROV):
     C                                       %ADDR(BYTESAVAL):
     C                                       Qxml_CCSID37)
     C                   CALLP     PRERR2
     C                   CALLP     PRERR1
     PSAXFATERR        E

     P addParm         B
     D addParm         pi
     D    parm                             like(parms) const
     D   blank         S             10I 0

     C                   eval      blank = %scan(' ' : parm)
     C                   if        blank > 0
     C                   eval      ind = ind + 1
     C                   eval      parms(ind)  = %subst(parm:1:blank-1)+x'00'
     C                   endif
     P addParm         E

	
XML4PR - XML4C Interface Wrapper for RPG, C and COBOL
Copyright 2000,2001,2002 International Business Machines. All Rights Reserved.