Examples of the XML-SAX operation
qsysprtDs = 'XML parser error: rc='
+ %CHAR(xmlRc)
+ '.';
endmon;
write qsysprt qsysprtDs;
*inlr = '1';
/end-free
P mySaxHandler B
D PI 10I 0
D info LIKEDS(handlerInfo_t)
D event 10I 0 VALUE
D stringPtr * VALUE
D stringLen 20I 0 VALUE
D exceptionId 10I 0 VALUE
D value S LIKE(value_t)
D BASED(info.pValue)
D chars S 65535A BASED(stringPtr)
D ucs2 S 16383C BASED(stringPtr)
D ucs2Len S 10I 0
/free
select;
// start parsing
when event = *XML_START_DOCUMENT; [4] clear info;
// start processing an attendee, by indicating
// that subsequent calls to this procedure should
// handle XML-attribute events.
when event = *XML_START_ELEMENT;
if %subst(chars : 1 : stringLen) = 'attendee';
info.handlingAttrs = *ON; [5] info.name = '';
info.company = '';
info.numAttendees += 1;
endif;
// display information about the attendee
when event = *XML_END_ELEMENT;
if %subst(chars : 1 : stringLen) = 'attendee';
info.handlingAttrs = *OFF;
qsysprtDs = 'Attendee '
+ info.name
+ ' is from company '
+ info.company;
write qsysprt qsysprtDs;
endif;
// prepare to get an attribute value by setting
// a basing pointer to the address of the correct
// variable to receive the value
when event = *XML_ATTR_NAME;
if info.handlingAttrs;
if %subst(chars : 1 : stringLen) = 'name';
info.pValue = %addr(info.name);
elseif %subst(chars : 1 : stringLen) = 'company';
info.pValue = %addr(info.company);
else;
// If the XML element is not expected to have
// extra attributes, halt the parsing by
// returning -1.
if not info.alwExtraAttr;
qsysprtDs = 'Unexpected attribute '
+ %subst(chars : 1 : stringLen)
+ ' found.';
write qsysprt qsysprtDs;
return -1; [6] endif;
info.pValue = *NULL;
endif;
endif;
// handle an exception
when event = *XML_EXCEPTION;
qsysprtDs = 'Exception '
+ %char(exceptionId)
+ ' occurred.';
write qsysprt qsysprtDs;
return exceptionId;
other;
// If this is an attribute we are interested
// in, the basing pointer for "value" has been
// set to point to either "name" or "company"
// Append each fragment of the value to the
// current data
if info.handlingAttrs
and info.pValue <> *NULL;
if event = *XML_ATTR_CHARS
or event = *XML_ATTR_PREDEF_REF;
value += %subst(chars : 1 : stringLen);
elseif event = *XML_ATTR_UCS2_REF;
ucs2Len = stringLen / 2; [7] value += %char(%subst(ucs2 : 1 : ucs2Len));
endif;
endif;
endsl;
return 0; [8] /end-free
P mySaxHandler E
This example illustrates several features of SAX parsing.
- The "External Return Code" subfield of the PSDS, named xmlRc here.
- The communication area data structure, used to communicate between the XML-SAX operation and the SAX event-handling procedure.
- The XML-SAX operation initiates the parsing of the XML document.
- The SAX event-handling procedure compares the event parameter to the special names *XML_START_DOCUMENT etc.
- The communication area is also used for the event-handling procedure to communicate with itself between calls.
- The event-handling procedure discovers an error and halts the parsing by returning -1.
- The *XML_ATTR_UCS2_REF event has UCS-2 data, independent of the CCSID that is normally used to return data for this XML-SAX operation. The length represents the number of bytes in the data, so it must be divided by two to obtain the number of UCS-2 characters.
- If the event-handling procedure does not discover any errors, it returns 0, indicating that parsing should continue.
The following sample XML document could be used with this example.
<meeting>
<attendee name="Jack" company="A&B Electronics"/>
<attendee company="City+ Waterworks" name="Jill"/>
<attendee name="Bill" company="Ace Movers" extra="yes"/>
</meeting>
For more information about XML operations, see XML Operations.