* Data structure used as a parameter between * the XML-SAX operation and the handling * procedure. * - "attrName" is set by the procedure doing the * XML-SAX operation and used by the handling procedure * - "attrValue" is set by the handling procedure * and used by the procedure doing the XML-SAX * operation * - "haveAttr" is used internally by the handling * procedure H/COPY QCPYSRC,HSPECLE D/COPY QCPYSRC,Dspgminf4 D xmlRc 10I 0 OVERLAY(PGMINF:368) D info DS INZ D attrName 20A VARYING D haveAttr N D attrValue 20A VARYING * Prototype for procedure "myHandler" defining * the communication-area parameter as being * like data structure "info" D myHandler PR 10I 0 D commArea LIKEDS(info) D event 10I 0 VALUE D string * VALUE D stringLen 20I 0 VALUE D exceptionId 10I 0 VALUE D xMLDOC S 265A VARYING /free // The purpose of the following XML-SAX operation // is to obtain the value of the first "companyname" // attribute found in the XML document. // The communication area "info" is initialized with // the name of the attribute whose value is // to be obtained from the XML document. attrName = 'title'; Xmldoc = '/QIBM/ProdData/xmltoolkit2/xml5_6_0/samples/Data/' +'AddressBook.Xml'; // Start SAX processing. The procedure "myHandler" // will be called for every SAX event; the first // parameter will be the data structure "info". xml-sax(e) %handler(myHandler : info) %xml(xmldoc :'doc=file'); // The XML-SAX operation is complete. The // communication area can be checked to get the // value of the attribute. if not %error() and attrValue <> ''; dsply (attrName + '=' + attrValue); endif; *Inlr = *On; Return; /End-Free // The SAX handling procedure "myHandler" P myHandler B D PI 10I 0 D comm LIKEDS(info) D event 10I 0 VALUE D string * VALUE D stringLen 20I 0 VALUE D exceptionId 10I 0 VALUE D value S 65535A BASED(string) D ucs2value S 16383C BASED(string) D rc S 10I 0 INZ(0) /free select; // When the event is a "start document" event, // the handler can initialize any internal // subfields in the communication area. when event = *XML_START_DOCUMENT; comm.haveAttr = *OFF; // When the event is an "attribute name" event, // and the value of the event is the required // name, the internal subfield "haveAttr" is // set to *ON. If the next event is an // attribute-value event, the value will be // saved in the "attrValue" subfield. when event = *XML_ATTR_NAME; If %subst(value : 1 : stringLen) = comm.attrName; comm.haveAttr = *ON; comm.attrValue = ''; Endif; // When "haveAttr" is on, the data from any // attribute-value should be saved in the "attrValue" // string until the *XML_END_ATTR event occurs when comm.haveAttr; select; when event = *XML_ATTR_CHARS or event = *XML_ATTR_PREDEF_REF; comm.attrValue += %subst(value : 1 : stringLen); when event = *XML_ATTR_UCS2_REF; stringLen = stringLen / 2; comm.attrValue += %char(%subst(ucs2value : 1 : stringLen)); when event = *XML_END_ATTR; // We have the entire attribute value // so no further parsing is necessary. // A non-zero return value tells the // RPG runtime that the handler does // not want to continue the operation rc = -1; endsl; endsl; return rc; /end-free P E