Try OpenEdge Now
skip to main content
Working with XML
Reading and Writing XML Data from Temp-Tables and ProDataSets : Reading XML into a temp-table, temp-table buffer, or ProDataSet : Reading XML into a ProDataSet
 

Reading XML into a ProDataSet

XML information read into a ProDataSet includes:
*Schema for defining a ProDataSets's temp-table buffers, temp-table indexes, and temp-table data-relations
*Data for the temp-table buffers
*Before-image information for the temp-table buffers
In this first code example, two static temp-tables are defined in an include file:
/* pi-tfx-ttSetup-7.i */
/* Creates two static temp-tables for use in a ProDataSet. */
/* Definition for temp-table ttCustomer */
DEFINE TEMP-TABLE ttCustomer NO-UNDO BEFORE-TABLE ttCustBefore
  FIELD CustNum  LIKE Customer.CustNum
  FIELD Name     LIKE Customer.Name
  FIELD Country  LIKE Customer.Country
  FIELD Comments LIKE Customer.Comments FORMAT "x(40)"
  INDEX CustNum  IS PRIMARY UNIQUE CustNum
  INDEX Name Name
  INDEX Comments IS WORD-INDEX Comments.

/* Definition for temp-table ttOrder */
DEFINE TEMP-TABLE ttOrder NO-UNDO
  FIELD OrderNum  LIKE Order.OrderNum
  FIELD CustNum   LIKE Order.CustNum
  FIELD OrderDate LIKE Order.OrderDate
  INDEX OrderNum  IS PRIMARY UNIQUE OrderNum
  INDEX CustOrder IS UNIQUE CustNum OrderNum
  INDEX OrderDate OrderDate.
Here is a snippet of the XML file:
<?xml version="1.0"?>
<dsCustomerOrders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttCustomer>
<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.0</CreditLimit>
<Balance>903.64</Balance>
<Terms>Net30</Terms>
<Discount>35</Discount>
<Comments>This customer is on credit hold.</Comments>
<Fax/>
<EmailAddress/>
<ttOrder>
<Ordernum>6</Ordernum>
<CustNum>1</CustNum>
<OrderDate>1998-02-11</OrderDate>
<ShipDate>1998-02-16</ShipDate>
<PromiseDate>1998-02-16</PromiseDate>
<Carrier>Standard Mail</Carrier>
<Instructions/>
<PO/>
<Terms>Net30</Terms>
<SalesRep>HXM</SalesRep>
<BillToID>0</BillToID>
<ShipToID>0</ShipToID>
<OrderStatus>Shipped</OrderStatus>
<WarehouseNum>0</WarehouseNum>
<Creditcard>American Express</Creditcard>
</ttOrder>
. . .
Note: The child records are nested within the parent records.
The following code sample defines the static ProDataSet, reads the previous XML file, and reports all the orders by customer number:
/* pi-tfx-read-7.p */
/* Populates a static ProDataSet with records from an XML file. */

{pi-tfx-parameterVarDefs.i}
{pi-tfx-ttSetup-7.i}

DEFINE VARIABLE hPDS    AS HANDLE  NO-UNDO.
DEFINE VARIABLE lReturn AS LOGICAL NO-UNDO.

DEFINE DATASET dsCustomerOrders FOR ttCustomer, ttOrder
  DATA-RELATION custOrd FOR ttCustomer, ttOrder
RELATION-FIELDS(CustNum, CustNum) NESTED.

hPDS = DATASET dsCustomerOrders:HANDLE.

ASSIGN
  cSourceType             = "FILE"
  cFile                   = "dsCustomerOrders.xml"
  cReadMode               = "EMPTY"
  cSchemaLocation         = ?
  lOverrideDefaultMapping = NO.

lReturn = hPDS:READ-XML (cSourceType, cFile, cReadMode,
  cSchemaLocation, lOverrideDefaultMapping).
IF lReturn THEN DO:
  FOR EACH ttCustomer BY ttCustomer.CustNum:
    FOR EACH ttOrder WHERE ttOrder.CustNum = ttCustomer.CustNum:
      DISPLAY ttCustomer.CustNum ttCustomer.Name FORMAT "x(30)"
        ttOrder.OrderNum.
    END.
  END.
END.
The code displays a report of all the orders for each customer. For example:
This next code sample provides a dynamic ProDataSet handle with XML Schema and data:
/* pi-tfx-read-8.p */
/* Populates an empty dynamic ProDataSet with XML Schema and records
from an XML document. */

{pi-tfx-parameterVarDefs.i}

DEFINE VARIABLE hPDS    AS HANDLE  NO-UNDO.
DEFINE VARIABLE hQuery  AS HANDLE  NO-UNDO.
DEFINE VARIABLE lReturn AS LOGICAL NO-UNDO.

CREATE DATASET hPDS.

ASSIGN
  cSourceType             = "FILE"
  cFile                   = "Dept400-2.xml"
  cReadMode               = "EMPTY"
  cSchemaLocation         = "Dept400-2.xsd"
  lOverrideDefaultMapping = ?
  cFieldTypeMapping       = ?
  cVerifySchemaMode       = ?.

lReturn = hPDS:READ-XML(cSourceType, cFile, cReadMode, cSchemaLocation,
  lOverrideDefaultMapping, cFieldTypeMapping, cVerifySchemaMode).
IF lReturn THEN
  DISPLAY SKIP "How many temp-table buffers in the ProDataSet?"
    hPDS:NUM-BUFFERS SKIP
    "How many data-relations in the ProDataSet?"
    hPDS:NUM-RELATIONS SKIP(2).

CREATE QUERY hQuery.
hQuery:SET-BUFFERS(hPDS:GET-BUFFER-HANDLE("ttEmp")).
hQuery:QUERY-PREPARE("FOR EACH ttEmp").
hQuery:QUERY-OPEN( ).
hQuery:GET-FIRST( ).

DISPLAY "Displaying Employee Roster..." SKIP(2)
  "Employee No. Last Name First Name".

REPEAT WHILE NOT hQuery:QUERY-OFF-END:
  DISPLAY
    hPDS:GET-BUFFER-HANDLE("ttEmp"):BUFFER-FIELD("EmpNum"):BUFFER-VALUE
    hPDS:GET-BUFFER-HANDLE("ttEmp"):BUFFER-FIELD("LastName"):BUFFER-VALUE
    hPDS:GET-BUFFER-HANDLE("ttEmp"):BUFFER-FIELD("FirstName"):BUFFER-VALUE.
  hQuery:GET-NEXT( ).
END. /* REPEAT */
The following is the output for this code:
In this sample, a handle was defined and used to create a new dynamic ProDataSet. The ProDataSet is in the CLEAR state at this point, meaning it has no ABL definition. When the code calls the READ-XML( ) method, the method sees that XML Schema has been provided. Since the dynamic ProDataSet is in the clear state, it knows the schema is being provided to create ABL definitions for the object from the XML Schema. If the ProDataSet had not been in the CLEAR state, the method would have to verify the XML Schema against whatever ABL definition existed for the ProDataSet.
The method can proceed to read in the data from the XML document provided in the method call. The code displays the number of temp-tables and data-relations in the dynamic object as well as a summary of the employee records now in the ttEmp temp-table.