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

Creating a dynamic temp-table with XML Schema

A dynamic temp-table needs its definition supplied before it can be put into a PREPARED state for reading data. Using READ-XMLSCHEMA( ) on a temp-table with no definition creates the definition from the XML Schema. The method then places the temp-table in the PREPARED state. The code example that follows does several things:
*Creates a dynamic temp-table
*Reads in XML Schema
*Displays some information about the definition of the temp-table
The read and write XML methods have many parameters. To make the code samples in this chapter more readable, each parameter is represented by a descriptively named variable. All these variable definitions are stored in an include file, as shown:
/* pi-tfx-parameterVarDefs.i */
/* Variables representing parameter values in the READ-XML( ),
READ-XMLSCHEMA( ), WRITE-XML( ), and WRITE-XMLSCHEMA( ) methods. */

DEFINE VARIABLE cSourceType             AS CHARACTER NO-UNDO.
DEFINE VARIABLE cTargetType             AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFile                   AS CHARACTER NO-UNDO.
DEFINE VARIABLE cReadMode               AS CHARACTER NO-UNDO.
DEFINE VARIABLE cSchemaLocation         AS CHARACTER NO-UNDO.
DEFINE VARIABLE lOverrideDefaultMapping AS LOGICAL   NO-UNDO.
DEFINE VARIABLE cFieldTypeMapping       AS CHARACTER NO-UNDO.
DEFINE VARIABLE cVerifySchemaMode       AS CHARACTER NO-UNDO.
DEFINE VARIABLE cEncoding               AS CHARACTER NO-UNDO.
DEFINE VARIABLE lFormatted              AS LOGICAL   NO-UNDO.
DEFINE VARIABLE lWriteSchema            AS LOGICAL   NO-UNDO.
DEFINE VARIABLE lMinSchema              AS LOGICAL   NO-UNDO.
DEFINE VARIABLE lWriteBeforeImage       AS LOGICAL   NO-UNDO.
Here is the code sample:
/* pi-tfx-read-1.p */
/* Provides XML Schema for a new dynamic temp-table. */

{pi-tfx-parameterVarDefs.i}

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

CREATE TEMP-TABLE httCust.


ASSIGN
  cSourceType             = "FILE"
  cFile                   = "ttCustomer.xsd"
  lOverrideDefaultMapping = ?
  cFieldTypeMapping       = ?
  cVerifySchemaMode       = ?.

DISPLAY "Is dynamic temp-table PREPARED? " httCust:PREPARED SKIP.
DISPLAY "Reading XML Schema..." SKIP.

lReturn = httCust:READ-XMLSCHEMA(cSourceType, cFile,
  lOverrideDefaultMapping, cFieldTypeMapping, cVerifySchemaMode).
IF lReturn THEN DO:
  DISPLAY "Is dynamic temp-table now PREPARED? " httCust:PREPARED SKIP.
  DISPLAY "How many columns in dynamic temp-table? "
    httCust:DEFAULT-BUFFER-HANDLE:NUM-FIELDS SKIP.
END.
The code displays the following:
In this next version, a non-ABL generated XML Schema file will be used to create a new dynamic temp-table. The schema is very similar to the Feedback table of the Sports2000 database. The purpose of this example is to demonstrate mapping an XML Schema field to something more useful for your ABL application.
Here is the ttFeedback.xsd file:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="ttFeedback">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="ttFeedbackRow" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Contact" type="xsd:string"/>
<xsd:element name="Company" type="xsd:string"/>
<xsd:element name="EmailAddress" type="xsd:string"/>
<xsd:element name="Phone" type="xsd:string"/>
<xsd:element name="Fax" type="xsd:string"/>
<xsd:element name="Comments" type="xsd:string"/>
<xsd:element name="Department" type="xsd:string"/>
<xsd:element name="Rating" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Elements with the XML Schema type "string" are mapped to CHARACTER fields by default.
Here is the code that maps the Comments field to a CLOB:
/* pi-tfx-read-1b.p */
/* Provides XML Schema for a new dynamic temp-table. */

{pi-tfx-parameterVarDefs.i}

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

CREATE TEMP-TABLE httFeedback.

ASSIGN
  cSourceType             = "FILE"
  cFile                   = "ttFeedback.xsd"

  lOverrideDefaultMapping = ?
  cFieldTypeMapping       = "Comments, CLOB"
  cVerifySchemaMode       = ?.

DISPLAY "Is dynamic temp-table PREPARED? " httFeedback:PREPARED SKIP.
DISPLAY "Reading XML Schema..." SKIP.

lReturn = httFeedback:READ-XMLSCHEMA(cSourceType, cFile,
  lOverrideDefaultMapping, cFieldTypeMapping, cVerifySchemaMode).
IF lReturn THEN DO:
  DISPLAY "Is dynamic temp-table now PREPARED? " httFeedback:PREPARED SKIP.
  DISPLAY "What is the data type of the Comments field? "
    httFeedback:DEFAULT-BUFFER-HANDLE:BUFFER-FIELD("Comments"):DATA-TYPE.

END.
Here is the code sample output: