Consultor Eletrônico



Kbase 21509: 4GL. How to output Data from a DataBase to an XML Document?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   14/11/2001
SUMMARY:
This Knowledge Base applies to V9.1C and Higher.

This Knowledge base tries to provide an example of how to create a XML document taking the data from a Progress Database Table.

The code:

/* XML Output from 4GL */
/* Declaration */
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 the Object we need */
CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.
CREATE X-NODEREF hRow.
CREATE X-NODEREF hField.
CREATE X-NODEREF hText.
/* Get the buffer for the customer thable */
hBuf = BUFFER Customer:HANDLE.
/* Set up a Root Node */
hDoc:CREATE-NODE (hRoot, "Customers", "ELEMENT").
hDoc:APPEND-CHILD (hRoot).
FOR EACH customer WHERE custnum < 5:
/* Create a Customerrow mode */
hDoc:CREATE-NODE (hRow, "Customer", "ELEMENT").
hRoot:APPEND-CHILD (hRow).
/* Put the row in the tree. CustNum and Name are attributes
of this element. The remainning fields are elements */
hRow:SET-ATTRIBUTE ("CustNum", STRING (CustNum)).
hRow:SET-ATTRIBUTE ("Name", NAME).
/* Add the other fields as elements */
REPEAT i = 1 TO hBuf:NUM-FIELDS:
hDBFld = hBuf:BUFFER-FIELD(i).
/* We already did CustNum and Name above so we skip them */
IF hDBFld:NAME = "CustNum" OR hDBFld:NAME = "Name" THEN NEXT.
/* Create an element with the field name as the tag.
Note that the field name is the same as the element
Name. The rules for allowed names in XML are less
stringent than the rules for Progress column names. */
hDoc:CREATE-NODE (hfield, hDBFld:NAME, "ELEMENT").
hRow:APPEND-CHILD (hField).
/* Make new field next row child */
hDoc:CREATE-NODE (hText, "", "TEXT").
/* Node to hold Value */
hField:APPEND-CHILD (hText).
/* attach text to field */
hText:NODE-VALUE = STRING (hDBfld:BUFFER-VALUE).
END.
END.
/* write the XML node tree to an xml file */
hDoc:SAVE ("file", "d:\cust.xml").
/* Delete the objects. Note that deleting the document
object deletes the DOM structure under it also */
DELETE OBJECT hDoc.
DELETE OBJECT hRoot.
DELETE OBJECT hRow.
DELETE OBJECT hField.
DELETE OBJECT hText.

The above code will generate the following cust.xml XML Document.

<?xml version="1.0"?>
<Customers>
<Customer Name="Pedro3" CustNum="1">
<Country>USA</Country>
<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>12.25</Balance>
<Terms>Net30</Terms>
<Discount>35</Discount>
<Comments>This customer is on credit hold.</Comments>
<Fax/>
<EmailAddress/>
</Customer>
<Customer Name="Urpon Frisbee" CustNum="2">
<Country>Finland</Country>
<Address>Rattipolku 3</Address>
<Address2/>
<City>Oslo</City>
<State>Uusima</State>
<PostalCode>45321</PostalCode>
<Contact>Urpo Leppakoski</Contact>
<Phone>(603) 532 5471</Phone>
<SalesRep>DKP</SalesRep>
<CreditLimit>27600</CreditLimit>
<Balance>24.12</Balance>
<Terms>Net30</Terms>
<Discount>35</Discount>
<Comments>Ship all products 2nd Day Air.</Comments>
<Fax/>
<EmailAddress/>
</Customer>
<Customer Name="Hoops " CustNum="3">
<Country>USA</Country>
<Address>Suite 415</Address>
<Address2>40 Grove St.</Address2>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>02112</PostalCode>
<Contact>Michael Traitser</Contact>
<Phone>(617) 355-1557</Phone>
<SalesRep>HXM</SalesRep>
<CreditLimit>75000</CreditLimit>
<Balance>2000</Balance>
<Terms>Net30</Terms>
<Discount>10</Discount>
<Comments>This customer is now OFF credit hold.</Comments>
<Fax/>
<EmailAddress/>
</Customer>
<Customer Name="Go Fishing Ltd" CustNum="4">
<Country>United Kingdom</Country>
<Address>Unit 2</Address>
<Address2>83 Ponders End Rd</Address2>
<City>Harrow</City>
<State>Middlesex</State>
<PostalCode>HA8 bbb</PostalCode>
<Contact>Alan Frogbrook</Contact>
<Phone>081 883 6827</Phone>
<SalesRep>SLS</SalesRep>
<CreditLimit>15000</CreditLimit>
<Balance>14235.14</Balance>
<Terms>Net30</Terms>
<Discount>10</Discount>
<Comments/>
<Fax/>
<EmailAddress/>
</Customer>
</Customers>