Try OpenEdge Now
skip to main content
Working with XML
Reading and Writing XML with the Document Object Model (DOM) : Creating XML output from ABL : Example of creating an output XML file
 

Example of creating an output XML file

The following sample program creates an XML file consisting of all fields in all the customer records where the cust‑num is less than "5". You must use the SAVE( ) method on the X-DOCUMENT object in order to create the actual XML file. For example:

i-outcus.p

/* i-outcus.p - Export the Customer table to an xml file*/
DEFINE VARIABLE hDoc   AS HANDLE  NO-UNDO.
DEFINE VARIABLE hRoot  AS HANDLE  NO-UNDO.
DEFINE VARIABLE hRow   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.

CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.
CREATE X-NODEREF hRow.
CREATE X-NODEREF hField.
CREATE X-NODEREF hText.

hBuf = BUFFER Customer:HANDLE.

/* Set up a root node */
hDoc:CREATE-NODE(hRoot,"Customers","ELEMENT").
hDoc:APPEND-CHILD(hRoot).

FOR EACH Customer WHERE Customer.CustNum < 5:
  hDoc:CREATE-NODE(hRow,"Customer","ELEMENT"). /* create a row node */
  hRoot:APPEND-CHILD(hRow). /* put the row in the tree */
  hRow:SET-ATTRIBUTE("CustNum", STRING(Customer.CustNum)).
  hRow:SET-ATTRIBUTE("Name", Customer.Name).

  /* Add the other fields as tags in the xml */
  REPEAT ix = 1 TO hBuf:NUM-FIELDS:
    hDBFld = hBuf:BUFFER-FIELD(ix).
    IF hDBFld:NAME = "CustNum" OR hDBFld:NAME = "Name" THEN NEXT.

    /* Create a tag with the field name */
    hDoc:CREATE-NODE(hField, hDBFld:NAME, "ELEMENT").

    /* Put the new field as next child of row */
    hRow:APPEND-CHILD(hField).

    /* Add a node to hold field value. The empty string ("") represents the
       value that will be set later. */
    hDoc:CREATE-NODE(hText, "", "TEXT").

    /* Attach the text to the field */
    hField:APPEND-CHILD(hText).
    hText:NODE-VALUE = STRING(hDBFld:BUFFER-VALUE).
  END.
END.

/* Write the XML node tree to an xml file */
hDoc:SAVE("file","cust.xml").

DELETE OBJECT hDoc.
DELETE OBJECT hRoot.
DELETE OBJECT hRow.
DELETE OBJECT hField.
DELETE OBJECT hText.
A partial output of the above program appears below:
<?xml version='1.0' ?>
<Customers>
<Customer Name="Lift Line Skiing" Cust-num="1">
<Country>USA</Country>
<Address>276 North Street</Address>
<Address2></Address2>
<City>Boston</City>
<State>MA</State>
<Postal-Code>02114</Postal-Code>
<Contact>Gloria Shepley</Contact>
<Phone>(617) 450-0087</Phone>
<Sales-Rep>HXM</Sales-Rep>
<Credit-Limit>66700</Credit-Limit>
<Balance>42568</Balance>
<Terms>Net30</Terms>
<Discount>35</Discount>
<Comments>This customer is on credit hold.</Comments>
</Customer>
<Customer Name="Urpon Frisbee" Cust-num="2">
<Country>Finland</Country>
<Address>Rattipolku 3</Address>
. . .
</Customer>
</Customers>
Note: The previous example adds carriage returns and indentations for readability; the actual file contains one long string.
* Writing an XML file to a MEMPTR, a stream, or a LONGCHAR