Consultor Eletrônico



Kbase P21041: How to write an XML file in 4GL?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   29/11/2007
Status: Verified

GOAL:

How to write an XML file in 4GL?

FACT(s) (Environment):

Progress 9.1x

FIX:

The following code, taken from the "Progress External Program Interfaces" manual, creates an XML file consisting of all fields in the customer table where the cust-num is less than "5".

/* 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 i AS INTEGER NO-UNDO.

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

ASSIGN hBuf = BUFFER customer:HANDLE.

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

FOR EACH customer NO-LOCK WHERE 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(custnum)).
hRow:SET-ATTRIBUTE("Name",NAME).

/* Add the other fields as tags in the xml */
DO i = 1 TO hBuf:NUM-FIELDS:
ASSIGN hDBFld = hBuf:BUFFER-FIELD(i).

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 */
hDoc:CREATE-NODE(hText, "", "TEXT").

/* attach the text to the field */
hField:APPEND-CHILD(hText).
ASSIGN 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.