Try OpenEdge Now
skip to main content
Working with XML
Reading and Writing XML Data from Temp-Tables and ProDataSets : Sample ProDataSet to XML round-trip
 

Sample ProDataSet to XML round-trip

One of the most important use cases for the features described in this chapter is that of the XML round-trip. XML can be a persistent storage mechanism between OpenEdge sessions. For example, a mobile computing model could use XML as a persistent data store. Suppose a sales representative could work off-line from the server with a local copy of selected records, limited to his accounts, stored as XML. The mobile application could read the XML and work locally. The changes made off-line by the sales representative on the records would be stored locally as an updated XML file, storing before-image data as well. When the sales representative reconnects to the system, the XML would be sent back to the application server to synchronize with enterprise application. The application code could decide whether to apply the remote changes or overwrite with server changes. The before-image data could be used to compare changes that might have been made while the sales representative was working remotely.
All you need to do is create an XML write procedure to store your data and an XML read procedure to retrieve it. Of course, you must ensure that the min-xmlschema option is set to FALSE. That is, you want the AVM to write the ABL XML Schema extensions so that the ABL definitions are fully restored when the file is read back in.
The following include file sets up a pair of temp-tables and a ProDataSet for the two code samples to use:
/* pi-tfx-writeSetup-6.i */
/* Creates two new static temp-tables and a ProDataSet. */

/* Definition for Temp-Table ttCustomer */
DEFINE TEMP-TABLE ttCustomer NO-UNDO BEFORE-TABLE ttCustBef
  FIELD CustNum  LIKE Customer.CustNum
  FIELD Name     LIKE Customer.Name COLUMN-LABEL "Loyal Customer"
    XML-NODE-TYPE "Attribute"
  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 BEFORE-TABLE ttOrdBef
  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.

DEFINE DATASET dsCustomerOrders FOR ttCustomer, ttOrder
  DATA-RELATION custOrd FOR ttCustomer, ttOrder
    REPOSITION RELATION-FIELDS (CustNum, CustNum) NESTED.
The first code sample performs an XML write and informs you if it has been successful:
/* pi-tfx-write-6a.p */
/* Writes data from a static ProDataSet to an XML file. This demonstrates
the first half of a persistent storage mechanism through XML. */

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

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

hPDS = DATASET dsCustomerOrders:HANDLE.

/* Before the method call, your application does work with its data. */

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

lReturn = hPDS:WRITE-XML (cTargetType, cFile, lFormatted, cEncoding,
  cSchemaLocation, lWriteSchema, lMinSchema, lWriteBeforeImage).
IF lReturn = FALSE THEN DO:
  MESSAGE "WRITE-XML on ProDataSet failed!" VIEW-AS ALERT-BOX.
  RETURN.
END.
ELSE
  MESSAGE "Successful WRITE-XML on : " hPDS:NAME VIEW-AS ALERT-BOX.
The second code sample performs an XML read and informs you if it has been successful, as shown:
/* pi-tfx-write-6b.p */
/* Reads and writes the data to and from a static ProDataSet to an XML file. */

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

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

hPDS = DATASET dsCustomerOrders:HANDLE.

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

lReturn = hPDS:READ-XML(cSourceType, cFile, cReadMode, cSchemaLocation,
  lOverrideDefaultMapping).
IF NOT lReturn THEN DO:
  MESSAGE "READ-XML on ProDataSet failed!" VIEW-AS ALERT-BOX.
  RETURN.
END.
ELSE
  MESSAGE "Successful READ-XML on:" hPDS:NAME VIEW-AS ALERT-BOX.

/* After the method call, your application does work with its data. */
* Mapping ABL names to different XML element or attribute names