The following code illustrates a typical way to use to use ABL support for the Document Object Model (DOM) API, to create an XML document from data in an OpenEdge database.
This creates an XML document with three main nodes. The first two contain arbitrary text to show how to build any node necessary using ABL statements. The second of the two shows how nodes can be nested in a parent-child relationship. The third main node contains data extracted from the first record in the Customer table of the Sports2000 sample database shipped with OpenEdge.
DEFINE VARIABLE hDoc AS HANDLE.
DEFINE VARIABLE hRoot AS HANDLE.
DEFINE VARIABLE hRow AS HANDLE.
DEFINE VARIABLE hField AS HANDLE.
DEFINE VARIABLE hText AS HANDLE.
DEFINE VARIABLE hBuf AS HANDLE.
DEFINE VARIABLE hDBFld AS HANDLE.
DEFINE VARIABLE i AS INTEGER.
CREATE X-DOCUMENT hDoc. /* XML document handle */
CREATE X-NODEREF hRoot. /* root node handle */
CREATE X-NODEREF hRow. /* handle for DB table rows & misc non-DB nodes */
CREATE X-NODEREF hField. /* handle for DB fields & non-DB nested nodes*/
CREATE X-NODEREF hText. /* handle for text nodes */
/*set up a root node*/
hDoc:CREATE-NODE(hRoot,"Datagram","ELEMENT").
hDoc:APPEND-CHILD(hRoot).
hRoot:SET-ATTRIBUTE("xmlns","").
/* For each node below the root: */
/* 1. Create an element node. */
/* 2. Attach it to the root. */
/* 3. Create a text node for it. */
/* 4. Link the text node to the element node. */
/* 5. Fill in the text. */
/* hard-code nodes that don't come from the OpenEdge database */
/* Example: "MessageId" node */
hDoc:CREATE-NODE(hRow,"MessageID","ELEMENT").
hRoot:APPEND-CHILD(hRow).
hDoc:CREATE-NODE(hText, "", "TEXT").
hRow:APPEND-CHILD(hText).
hText:NODE-VALUE = "8a58f0b1-7e27-415c-b4f1-c87ab0a02084".
/* Example: Passport node has children linked to it in a nested fashion */
hDoc:CREATE-NODE(hRow,"Passport","ELEMENT").
hRoot:APPEND-CHILD(hRow).
hDoc:CREATE-NODE(hField, "Node", "ELEMENT").
hRow:APPEND-CHILD(hField).
hDoc:CREATE-NODE(hText, "", "TEXT").
hField:APPEND-CHILD(hText).
hText:NODE-VALUE = "http://www.mycompany.com/cis".
hDoc:CREATE-NODE(hField, "SecurityKey", "ELEMENT").
hRow:APPEND-CHILD(hField).
/* Example: "Document" node using the fields in the sports2000 Customer table */
hBuf = BUFFER customer:HANDLE.
FIND FIRST customer NO-LOCK. /* just one for demonstration */
hDoc:CREATE-NODE(hRow,"Document","ELEMENT"). /*create a row node*/
hRoot:APPEND-CHILD(hRow). /*put the row in the tree*/
/*Add the fields as tags in the xml*/
REPEAT i = 1 TO hBuf:NUM-FIELDS:
hDBFld = hBuf:BUFFER-FIELD(i).
/*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).
/* If field has unknown value, just leave the tag empty */
IF hDBFld:BUFFER-VALUE = ? THEN NEXT.
/* Add a node to hold field value. Text nodes don't get a name. */
hDoc:CREATE-NODE(hText, "", "TEXT").
hText:NODE-VALUE = STRING(hDBFld:BUFFER-VALUE).
/*attach the text to the field*/
hField:APPEND-CHILD(hText).
END.
/*write the XML node tree to an xml file*/
hDoc:SAVE("file","myfile.xml").
DELETE OBJECT hDoc.
DELETE OBJECT hRoot.
DELETE OBJECT hRow.
DELETE OBJECT hField.
DELETE OBJECT hText.
The above code produces the following output:
<?xml version="1.0" ?>
<Datagram xmlns="">
<MessageID>8a58f0b1-7e27-415c-b4f1-c87ab0a02084</MessageID>
<Passport>
<Node>http://www.mycompany.com/cis</Node>
<SecurityKey />
</Passport>
<Document>
<CustNum>1</CustNum>
<Country>USA</Country>
<Name>Lift Tours</Name>
<Address>276 North Drive</Address>
<Address2 />
<City>Burlington</City>
<State>MA</State>
<PostalCode>01730</PostalCode>
<Contact>Gloria Shepley</Contact>
<Phone>(617) 450-0086</Phone>
<SalesRep>HXM</SalesRep>
<CreditLimit>66700</CreditLimit>
<Balance>903.64</Balance>
<Terms>Net30</Terms>
<Discount>35</Discount>
<Comments>This customer is on credit hold.</Comments>
<Fax />
<EmailAddress />
</Document>
</Datagram>