%HANDLER は、1 つのイベントまたは一連のイベントを処理するプロシージャーを識別するために使用します。%HANDLER は値を戻しません。また、XML-SAX および XML-INTO の第 1 オペランドとしてのみ指定できます。
第 1 オペランド handlingProcedure は、処理プロシージャーのプロトタイプを指定します。プロトタイプによって指定された、またはプロトタイプが明示的に指定されていない場合にはプロシージャー・インターフェースによって指定された戻り値およびパラメーターは、 処理プロシージャーが必要とするパラメーターと一致している必要があります。 要件は、%HANDLER が指定されている命令によって決まります。 処理プロシージャーの定義に関する特定の要件については、XML-SAX (XML 文書の構文解析)および XML-INTO (XML 文書の変数への構文解析)を参照してください。
第 2 オペランド communicationArea は、処理プロシージャーのすべての呼び出しでパラメーターとして渡される変数を指定します。オペランドは、参照によって渡されるプロトタイプ・パラメーターの検査で使用する規則と同じ規則に従って、 処理プロシージャーの最初のプロトタイプ・パラメーターに完全に一致してい る必要があります。 通信域パラメーターには、配列やデータ構造など、任意のタイプを使用できます。
* 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
D info DS
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
/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 = 'companyname';
// 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);
// 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;
:
:
* 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 VARYING
D BASED(string)
D ucs2value S 16383C VARYING
D 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
and %subst(value : 1 : stringLen) = comm.attrName;
comm.haveAttr = *ON;
comm.attrValue = '';
// 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
その他の %HANDLER の例については、XML-SAX (XML 文書の構文解析)および XML-INTO (XML 文書の変数への構文解析)を参照してください。