Try OpenEdge Now
skip to main content
Working with XML
Reading XML Documents with the Simple API for XML (SAX) : ABL SAX and WebSpeed : Example code: reading XML data using WebSpeed
 

Example code: reading XML data using WebSpeed

This example reads XML data using WebSpeed using the i-sax3s.p server procedure. The example can use the callbacks in i-sax2h.p (the example handler procedure from the previous example code).

i-sax3s.p

/* This particular procedure is intended to be run on a server with an
   available web server and functioning WebSpeed broker/messenger. */

/* This is needed to support webspeed applications */
{src/web/method/cgidefs.i}

DEFINE VARIABLE hHandler AS HANDLE NO-UNDO.
DEFINE VARIABLE hParser  AS HANDLE NO-UNDO.

CREATE SAX-READER hParser.

/* Run the persistent procedure that contains the callbacks */
RUN "i-sax2h.p" PERSISTENT SET hHandler.

/* Give the SAX-READER the handle to the persistent procedure */
hParser:HANDLER = hHandler.

/* Check to see if there is an XML document available on the webstream and if
   true, give it to the sax parser.*/
IF (WEB-CONTEXT:IS-XML) THEN
  hParser:SET-INPUT-SOURCE("handle", WEB-CONTEXT).

hParser:SAX-PARSE( ) NO-ERROR.

/* By the time SAX-PARSE returns, our callbacks have been called as many
times as necessary and we're done processing the XML document
(or there was an error) */
IF ERROR-STATUS:ERROR THEN DO:
  IF ERROR-STATUS:NUM-MESSAGES > 0 THEN
    /* Unable to begin the parse */
    MESSAGE ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX.
  ELSE
    /* Error raised in a callback */
    MESSAGE RETURN-VALUE VIEW-AS ALERT-BOX.
END.
ELSE
  MESSAGE "Document parsed successfully".

DELETE OBJECT hParser.
DELETE PROCEDURE hHandler.