Consultor Eletrônico



Kbase 21510: 4GL. How to create records from and XML Document.
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   11/14/2001
SUMMARY:
This knowledge Base apllies to V9.1C and Higher.

This Knowledge Base tries to provide an example of how to create records in a Progress DataBase from an XML document.

In Example if you have the following XML file and you need to import the datas to a specific table.

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


The procedure to use to be able to create the record is the following.

/* XML input from 4GL */
/* Declarations */
DEFINE VARIABLE hDoc AS HANDLE NO-UNDO.
DEFINE VARIABLE hRoot AS HANDLE NO-UNDO.
DEFINE VARIABLE hTable 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.
DEFINE VARIABLE j AS INTEGER NO-UNDO.
/* Create the Object we will need */
CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.
CREATE X-NODEREF hTable.
CREATE X-NODEREF hField.
CREATE X-NODEREF hText.
hBuf = BUFFER customer:HANDLE.
/* Read in the file created by the output example.
Note that the entire file is read and parsed at
this point. */
hDoc:LOAD ("FILE", "d:\Cust.xml", FALSE).
/* Get the Root of the Structure */
hDoc:GET-DOCUMENT-ELEMENT (hRoot).
/* Read each customer of the Root */
REPEAT i = 1 TO hRoot:NUM-CHILDREN:
hRoot:GET-CHILD (hTable,i).
/* Create a Customer Record in the Database */
CREATE customer.
/* CustNum and Name are attributes, so we have
to get those values from there */
custnum = INTEGER (hTable:GET-ATTRIBUTE ("CustNum")).
NAME = hTable:GET-ATTRIBUTE ("name").
/* The remaining fields are elements with text values. */
REPEAT j = 1 TO hTable:NUM-CHILDREN:
hTable:GET-CHILD (hField,j).
/* skip any null value */
IF hField:NUM-CHILDREN < 1 THEN NEXT.
hDBFld = hBuf:BUFFER-FIELD (hField:NAME).
/* Get text value of field and put it in the customer
buffer. Note that the field name is assumed to be the
element name. But the rules for allowed names in XML are
less stringent than the rules for Progress column names */
hField:GET-CHILD (hText,1).
hDBFld:BUFFER-VALUE = hText:NODE-VALUE.
END.
END.
/* Delete the objects we created.
Note that when we delete hDoc, the structure under it
will be deleted as well */
DELETE OBJECT hDoc.
DELETE OBJECT hroot.
DELETE OBJECT hTable.
DELETE OBJECT hField.
DELETE OBJECT hText.