Progress
External Program
Interfaces


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:

e-outcus.p
/* e-outcus.p - Export the Customer table to an xml file*/
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.
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 cust-num < 5:
    hDoc:CREATE-NODE(hRow,"Customer","ELEMENT"). /*create a row node*/
    hRoot:APPEND-CHILD(hRow).  /*put the row in the tree*/
    hRow:SET-ATTRIBUTE("Cust-num",STRING(cust-num)).
    hRow:SET-ATTRIBUTE("Name",NAME).
    /*Add the other fields as tags in the xml*/
    REPEAT i = 1 TO hBuf:NUM-FIELDS:
        hDBFld = hBuf:BUFFER-FIELD(i).
        IF hDBFld:NAME = "Cust-num" 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).
        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. Note that the carriage returns and indentations have been entered for readability; the actual file contains one long string:

<?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> 

Writing an XML File To a MEMPTR Or a Stream

You can also write an XML file to a MEMPTR or to an output stream as the following code fragments demonstrate. The following fragment shows saving an XML file to a MEMPTR:

DEFINE VARIABLE memfile AS MEMPTR.
. . .
hDoc:SAVE("memptr",memfile). /* SAVE() will set the memptr size */
. . . 

The following fragment displays saving an XML file to an output stream:

DEFINE STREAM xmlstream.
. . .
OUTPUT STREAM xmlstream TO custxml.xml.
hDoc:SAVE("stream","xmlstream").
OUTPUT CLOSE.
. . . 


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095