Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : Pub/Sub messaging examples : Publishing, receiving, and parsing an XMLMessage
 

Publishing, receiving, and parsing an XMLMessage

The procedures example10.p and example11.p create, publish, receive, and parse an XMLMessage.
To run example10.p and example11.p:
1. Run example11.p so the subscriber is running before you publish. The following example subscribes, receives, and parses an XMLMessage:
example11.p
/* Receives and parse an XML message. */
DEFINE VARIABLE hPubSubSession AS HANDLE  NO-UNDO.
DEFINE VARIABLE msgConsumer1   AS HANDLE  NO-UNDO.
DEFINE VARIABLE msgNum         AS INTEGER NO-UNDO.
DEFINE VARIABLE stillWaiting   AS LOGICAL NO-UNDO INITIAL TRUE.
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN beginSession IN hPubSubSession.
RUN createMessageConsumer IN hPubSubSession
  (THIS-PROCEDURE, "messageHandler", OUTPUT msgConsumer1).
RUN subscribe IN hPubSubSession
  ("people", ?, ?, FALSE, msgConsumer1) NO-ERROR.
RUN startReceiveMessages IN hPubSubSession.
RUN waitForMessages IN hPubSubSession ("inWait", THIS-PROCEDURE, ?).
RUN deleteSession IN hPubSubSession.
PROCEDURE messageHandler:
  DEFINE INPUT PARAMETER hMessage         AS HANDLE NO-UNDO.
  DEFINE INPUT PARAMETER hMessageConsumer AS HANDLE NO-UNDO.
  DEFINE OUTPUT PARAMETER hReply          AS HANDLE NO-UNDO.
  DEFINE VARIABLE hDoc    AS HANDLE    NO-UNDO.
  DEFINE VARIABLE hRoot   AS HANDLE    NO-UNDO.
  DEFINE VARIABLE ix      AS INTEGER   NO-UNDO.
  DEFINE VARIABLE mDoc    AS MEMPTR    NO-UNDO.
  DEFINE VARIABLE xmlText AS CHARACTER NO-UNDO.
  CREATE X-DOCUMENT hDoc.
  CREATE X-NODEREF hRoot.
  SET-SIZE(mDoc) = 400000. /* The size is an estimate. */
  ix = 1.
  DO WHILE NOT DYNAMIC-FUNCTION('endOfStream' IN hMessage):
    xmlText = DYNAMIC-FUNCTION('getTextSegment':U IN hMessage).
    PUT-STRING(mDoc, ix) = xmlText.
    ix = ix + LENGTH(xmlText).
  END.
  hDoc:LOAD("memptr", mDoc, FALSE).
  hDoc:GET-DOCUMENT-ELEMENT(hRoot).
  RUN getPeople(hRoot, 1).
  RUN deleteMessage IN hMessage.
  SET-SIZE(mDoc) = 0.
  stillWaiting = FALSE.
END PROCEDURE.

/* Displays the XML node names and XML text. */
PROCEDURE getPeople:
  DEFINE INPUT PARAMETER hParent AS HANDLE  NO-UNDO.
  DEFINE INPUT PARAMETER iLevel  AS INTEGER NO-UNDO.
  DEFINE VARIABLE hNodeRef AS HANDLE  NO-UNDO.
  DEFINE VARIABLE ix       AS INTEGER NO-UNDO.
  CREATE X-NODEREF hNodeRef.
  REPEAT ix = 1 TO hParent:NUM-CHILDREN.
    hParent:GET-CHILD(hNodeRef, ix).
    IF hNoderef:NAME = "#text" THEN
      MESSAGE "Text: " hNodeRef:NODE-VALUE.
    ELSE
      MESSAGE "Node name: " hNodeRef:NAME.
    RUN getPeople(hNodeRef, (iLevel + 1)).
  END.
  DELETE OBJECT hNoderef.
END PROCEDURE.
FUNCTION inWait RETURNS LOGICAL:
  RETURN stillWaiting.
END.
2. Run example10.p to create and publish an XMLMessage with the data of 100 people, as shown:
example10.p
/* Publishes an XML message. */
DEFINE VARIABLE hPubSubSession AS HANDLE    NO-UNDO.
DEFINE VARIABLE hMesg          AS HANDLE    NO-UNDO.
DEFINE VARIABLE cPerson        AS CHARACTER NO-UNDO.
DEFINE VARIABLE ix             AS INTEGER   NO-UNDO.
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN beginSession IN hPubSubSession.
RUN createXMLMessage IN hPubSubSession (OUTPUT hMesg).
/* Creates an XML message with 100 people. */
RUN appendText IN hMesg('<?xml version="1.0" ').
RUN appendText IN hMesg("encoding='ISO8859-1' ?>").
/* Create personnel node */
RUN appendText IN hMesg("<personnel>").
REPEAT ix = 1 TO 100:
  ASSIGN
    cPerson = "<person>"
    cPerson = cPerson + "<name>"
    cPerson = cPerson + "<family>SecondName</family>"
    cPerson = cPerson + "<given>FirstName" + STRING(ix) + "</given>"
    cPerson = cPerson + "</name>"
    cPerson = cPerson + "<email>myEmail@subpargolf.com</email>"
    cPerson = cPerson + "</person>".
  RUN appendText IN hMesg (cPerson).
END.
RUN appendText IN hMesg ("</personnel>").
RUN publish IN hPubSubSession ("people", hMesg, ?, ?, ?).
RUN deleteMessage IN hMesg.
RUN deleteSession IN hPubSubSession.