Try OpenEdge Now
skip to main content
Working with XML
Reading and Writing XML with the Document Object Model (DOM) : Reading XML input into ABL : Examples of reading an input XML file
 

Examples of reading an input XML file

The i-attnam.p example shows reading in a file called "personal.xml", processing through all the child nodes and displaying information if the node name is "person".

i-attnam.p

/* i-attnam.p */
DEFINE VARIABLE hDoc  AS HANDLE  NO-UNDO.
DEFINE VARIABLE hRoot AS HANDLE  NO-UNDO.
DEFINE VARIABLE lGood AS LOGICAL NO-UNDO.

CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.

hDoc:LOAD("file", "personal.xml", TRUE).
hDoc:GET-DOCUMENT-ELEMENT(hRoot).

RUN getChildren(hRoot, 1).
DELETE OBJECT hDoc.
DELETE OBJECT hRoot.

PROCEDURE getChildren:
  DEFINE INPUT PARAMETER hParent AS HANDLE  NO-UNDO.
  DEFINE INPUT PARAMETER iLevel  AS INTEGER NO-UNDO.

  DEFINE VARIABLE ix       AS INTEGER NO-UNDO.
  DEFINE VARIABLE hNoderef AS HANDLE  NO-UNDO.

  CREATE X-NODEREF hNoderef.

  REPEAT ix = 1 TO hParent:NUM-CHILDREN:
    lGood = hParent:GET-CHILD(hNoderef, ix).
IF NOT lGood THEN LEAVE.
    IF hNoderef:SUBTYPE <> "element" THEN NEXT.
IF hNoderef:NAME = "person" THEN
      MESSAGE "getattr id gives" hNoderef:GET-ATTRIBUTE("id")
        hNoderef:ATTRIBUTE-NAMES.
RUN getChildren(hNoderef, (iLevel + 1)).
  END.

  DELETE OBJECT hNoderef.
END PROCEDURE.
The i-incus.p example reads in the output file created by the previous program, i-outcus.p and creates temp-table entries.

i-incus.p

/* i-incus.p - Import the Customer table from an XML file */
DEFINE VARIABLE hDoc   AS HANDLE  NO-UNDO.
DEFINE VARIABLE hRoot  AS HANDLE  NO-UNDO.
DEFINE VARIABLE hTable AS HANDLE  NO-UNDO.
DEFINE VARIABLE hField AS HANDLE  NO-UNDO.
DEFINE VARIABLE hText  AS HANDLE  NO-UNDO.
DEFINE VARIABLE hBuf   AS HANDLE  NO-UNDO.
DEFINE VARIABLE hDBFld AS HANDLE  NO-UNDO.
DEFINE VARIABLE ix     AS INTEGER NO-UNDO.
DEFINE VARIABLE jx     AS INTEGER NO-UNDO.

/* So we can create new recs */
DEFINE TEMP-TABLE ttCustomer LIKE Customer.

CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.
CREATE X-NODEREF hTable.
CREATE X-NODEREF hField.
CREATE X-NODEREF hText.
hBuf = BUFFER ttCustomer:HANDLE.

/* Read in the file created in i-outcus.p */
hDoc:LOAD("file", "cust.xml", FALSE).
hDoc:GET-DOCUMENT-ELEMENT(hRoot).

/* Read each Customer from the root */
REPEAT ix = 1 TO hRoot:NUM-CHILDREN:
  hRoot:GET-CHILD(hTable, ix).
  CREATE ttCustomer.

  /* Get the fields given as attributes */
  ttCustomer.CustNum = INTEGER(hTable:GET-ATTRIBUTE("CustNum")).
  ttCustomer.Name    = hTable:GET-ATTRIBUTE("Name").

  /* Get the remaining fields given as elements with text */
  REPEAT jx = 1 TO hTable:NUM-CHILDREN:
    hTable:GET-CHILD(hField, jx).
    IF hField:NUM-CHILDREN < 1 THEN NEXT.

/* Skip any null value */
    hDBFld = hBuf:BUFFER-FIELD(hField:NAME).
    hField:GET-CHILD(hText, 1).

    /* Get the text value of the field */
    hDBFld:BUFFER-VALUE = hTEXT:NODE-VALUE.
  END. /* REPEAT jx */
END. /* REPEAT ix */

DELETE OBJECT hDoc.
DELETE OBJECT hRoot.
DELETE OBJECT hTable.
DELETE OBJECT hField.
DELETE OBJECT hText.

/* show data made it by displaying temp-table */
FOR EACH v:
  DISPLAY ttCustomer.Name.
END.