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

Writing a ProDataSet to XML

The following code sample writes a ProDataSet to an XML file. This is a snippet of the setup code showing the ProDataSet definition (note the NESTED options):
/* pi-tfx-writeSetup-4.i */
/* Definition of a ProDataSet. */
. . .
DEFINE DATASET DSET FOR ttDepartment, ttEmployee, ttFamily, ttBenefits
  DATA-RELATION DeptEmp FOR ttDepartment, ttEmployee
    RELATION-FIELDS(deptcode, deptcode) NESTED  DATA-RELATION EmpFam FOR ttEmployee, ttFamily
    RELATION-FIELDS (empnum,empnum) NESTED  DATA-RELATION EmpBene FOR ttEmployee,ttBenefits
    RELATION-FIELDS (empnum,empnum) NESTED.
. . .
The NESTED option cannot be used when min-xmlschema optional parameter is TRUE. The following is a code sample for a simple XML write from a static ProDataSet:
/* pi-tfx-write-4.p */
/* Writes the data from a static ProDataSet to an XML file. */

{pi-tfx-parameterVarDefs.i}
{pi-tfx-writeSetup-4.i}

DEFINE VARIABLE lReturn AS LOGICAL NO-UNDO.

ASSIGN
  cTargetType       = "FILE"
  cFile             = "Dept400.xml"
  lFormatted        = TRUE
  cEncoding         = ?
  cSchemaLocation   = ?
  lWriteSchema      = FALSE
  lMinSchema        = FALSE
  lWriteBeforeImage = FALSE.

lReturn = DATASET DSET:WRITE-XML(cTargetType, cFile, lFormatted,
  cEncoding, cSchemaLocation, lMinSchema, lWriteBeforeImage).
This example uses the NESTED option, and is a snippet of the resulting XML demonstrating what it does:
<?xml version="1.0"?>
<DSET xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttDepartment><DeptCode>400</DeptCode>
<DeptName>Sales</DeptName>
<ttEmployee>
<EmpNum>3</EmpNum>

<LastName>Smith</LastName>
<FirstName>Justine</FirstName>
<Address>1342 Atlantic Ave</Address>
<Address2>Apt 345b</Address2>
<City>Boston</City>
<State>MA</State>
<PostalCode>01834</PostalCode>
<HomePhone>617 333-3334</HomePhone>
<WorkPhone>800 787-8484</WorkPhone>
<DeptCode>400</DeptCode>
<Position>Sales Manager</Position>
<Birthdate>1960-01-08</Birthdate>
<StartDate>1997-06-19</StartDate>
<VacationDaysLeft>5</VacationDaysLeft>
<SickDaysLeft>4</SickDaysLeft>
<ttFamily>
<EmpNum>3</EmpNum>
<RelativeName>Kelly Smith</RelativeName>
<Relation>Daughter</Relation>
<Birthdate>1993-08-18</Birthdate>
<CoveredOnBenefits>true</CoveredOnBenefits>
<BenefitDate>1997-06-19</BenefitDate>
</ttFamily>
<ttFamily>
<EmpNum>3</EmpNum>

<RelativeName>Mark Smith</RelativeName>
<Relation>Spouse</Relation>
<Birthdate>1960-01-08</Birthdate>
<CoveredOnBenefits>true</CoveredOnBenefits>
<BenefitDate>1998-02-21</BenefitDate>
</ttFamily>
Note that the ttEmp row associated with employee number 3 of department 400 is nested within ttDept row for department 400 and that the rows from the ttFam table associated with employee number 3 are included within the ttEmp element.
Remove the NESTED keywords from the setup code and run it again. The resulting XML file now listed all rows from one table then all rows from the next table, and so on. The following is a condensed version of the XML highlighting this point:
<ttDepartment>
<ttEmployee>
<EmpNum>3</EmpNum>
...
</ttEmployee>
<ttEmployee>
<EmpNum>6</EmpNum>
...
</ttEmployee>
.
.
.
<ttFamily>
<EmpNum>3</EmpNum>
...
</ttFamily>
<ttFamily>
<EmpNum>3</EmpNum>
...
</ttFamily>
<ttFamily>
<EmpNum>3</EmpNum>
.ttFamily..
</ttFam>
<ttFamily>
<EmpNum>6</EmpNum>
...
</ttFamily>
. . .
</ttDepartment>