Program: PARSE2
//************************************************************************
// @Author: Aaron Bartell
// @Creation Date:
// @Desc: Same as EXAMPLE,PARSE1 except things have been "combined" to
// make the parsing easier in that there is only one local sub
// procedure handling all of the parser events vs. a separate one
// for each event.
// @Notes:
//************************************************************************
H dftactgrp(*no) bnddir('RXSBND')
/copy rxs,RXSCp
D allHandler pr
D pType value like(RXS_Type)
D pXPath value like(RXS_XPath)
D pData value like(RXS_XmlData)
D pDataLen value like(RXS_Length)
D errHandler pr
D pCurLine 10i 0 value
D pCurCol 10i 0 value
D pErrStr 1024a value varying
D gError ds likeds(RXS_Error)
D gXml s 512a varying
D gPhnCnt s 10i 0
/free
// Place the xml in a variable to be parsed. Normally you would be
// getting the xml from either an IFS file or from reading it in
// via RXS_readStdIn().
gXml =
'<PostAdr residential="true">' +
'<name title="Mr.">' +
'<first>Aaron</first>' +
'<last>Bartell</last>' +
'</name>' +
'<street>123 Center Rd</street>' +
'<cty>Mankato</cty>' +
'<state>MN</state>' +
'<zip>56001</zip>' +
'<phone>123-123-1234</phone>' +
'<phone>321-321-4321</phone>' +
'</PostAdr>';
monitor;
// Tell the parser we want to be notified of ALL element content
// events. Specify the local sub procedure address of allHandler.
RXS_allElemContentHandler(%paddr(allHandler));
// Initiate the parsing. Pass the variable containing the xml as the
// first parameter. Tell the parser that a variable is to be parsed
// (i.e. RXS_VAR vs. RXS_STMF). And lastly tell the parser what local
// sub procedure to call in the event an error occurs during parsing.
RXS_parse(gXml: RXS_VAR: %paddr(errHandler));
on-error;
// If any errors occur this will obtain the error and place it into
// the global variable gError. You can force an error to happen by
// modifying the xml above to be missing one of it's end tags (i.e.
// change </zip> to </zi>)
gError = RXS_catchError();
// Send a dialog message to the job log.
RXS_log(
RXS_DIAG: 'Error occurred during parsing. See previous messages.');
endmon;
*inlr = *on;
/end-free
//--------------------------------------------------------------------------------------------
// @Author: Aaron Bartell
// @Created: 2005-08-03
// @Desc: Handle all events based on specifying RXS_allElemContentHandler.
// Note that instead of displaying pData's contents in the job log
// one could just as easily place that data in a physical file or
// display it back to an interactive green screen display.
//
// @Notes: There are four events that your program can be notified of through the allHandler
// sub procedure and they are passed in the pEvntType parm. An event is triggered
// as the parser reads the document top down, left to right (same way you read the
// newspaper). Note that you will only be notified of events you specified before the
// call to RXS_parse by using API's RXS_allElemBegHandler, RXS_allElemContentHandler,
// RXS_allElemEndHandler, and RXS_allAttrHandler.
//
// The four events and their values (note that these can all be overidden by
// changing the RXSCFG file or by doing a temporary override on the RXS_parse command)
//
// Element Begin = '>' -- Placed at end of string (i.e. '/elem>')
// Element Content = '/' -- Placed at end of string (i.e. '/elem/')
// Element End = '/>' -- Placed at end of string (i.e. '/elem/>')
// Attribute = '@' -- Placed between the elem and attr (i.e. '/elem@attr')
//
// The most common events you will use are Element Content and Attribute simply
// because these are the two that provide business data via the pData parm on the
// allHandler procedure interface. Events Element Begin and Element End are very
// helpful when you have repeating XML data. You can then use the Begin and End
// events to do a CLEAR and WRITE to a PF record respectively. Remember that
// in-between the Element Begin and Element End events you will be notified of all the
// element content, which means that after the CLEAR (i.e. Element Begin event) you
// can place data into the PF record until you reach the Element End event at which
// time you can do a WRITE of that record because it has now been populated with data.
//--------------------------------------------------------------------------------------------
P allHandler b
D allHandler pi
D pType value like(RXS_Type)
D pXPath value like(RXS_XPath)
D pData value like(RXS_XmlData)
D pDataLen value like(RXS_Length)
/free
select;
when pXPath = '/PostAdr/zip/';
RXS_log(RXS_DIAG: 'Zip code is:' + pData);
when pXPath = '/PostAdr/phone/';
gPhnCnt = gPhnCnt + 1;
RXS_log(RXS_DIAG: 'Phone ' + %char(gPhnCnt) + ' is:' + pData);
endsl;
/end-free
P e
//------------------------------------------------------------------------
// @Author: Aaron Bartell
// @Created: 2005-08-03
// @Desc: Handle any errors that the parser encounters.
//------------------------------------------------------------------------
P errHandler B
D errHandler PI
D pCurLine 10i 0 value
D pCurCol 10i 0 value
D pErrStr 1024a value varying
/free
gError.code = 'PARSE2.1';
gError.severity = 100;
gError.pgm = 'PARSE.errHandler';
gError.text =
'Line:' + %char(pCurLine) +
' Column:' + %char(pCurCol) +
' ' + pErrStr;
/end-free
P E