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 XML from a temp-table buffer's current row
 

Writing XML from a temp-table buffer's current row

The SERIALIZE-ROW( ) method serializes a buffer's current row to an XML string.
The syntax for SERIALIZE-ROW( ) is shown below. The method returns TRUE or FALSE to indicate whether the operation was successful.

Syntax

SERIALIZE-ROW ( target-format , target-type ,
  { file | stream | stream-handle | memptr | longchar | JsonObject }
  [ , formatted [, encoding [ , omit-initial-values
  [ , omit-outer-object ] ] ] ] )
target-format
A CHARACTER expression that specifies the target format used to serialize the current row. Valid values are "JSON" and "XML". However, this method description focuses on serializing to an XML string.
For information on using this method to serialize the current row of a temp-table buffer to JSON, see the description of this method in OpenEdge Development: Working with JSON.
target-type
A CHARACTER expression that specifies the target type for the XML string. Valid values are: "FILE", "STREAM", "MEMPTR", "HANDLE", and "LONGCHAR".
"JsonObject" is a valid value for target-type, but only when target-format is "JSON". If you specify "JsonObject" when target-format is "XML", SERIALIZE-ROW( ) generates an error message and returns FALSE.
file
A CHARACTER expression that specifies the name of a file to which the AVM writes the XML string. You can specify an absolute pathname or a pathname relative to the current working directory. If a file with the specified name already exists, the AVM verifies that the file is writeable and overwrites the file.
stream
A CHARACTER expression that specifies the name of a stream. If you specify the empty string (""), the AVM writes the XML string to the default unnamed output stream. For WebSpeed, write the XML string to the WebSpeed-defined output stream (WEBSTREAM).
stream-handle
A HANDLE variable that specifies a stream object handle.
memptr
A MEMPTR variable to contain the XML string in memory. If you do not specify the encoding parameter, the AVM encodes the text written to the MEMPTR as UTF-8. This method allocates the required amount of memory for the XML string and sets the size of the variable. When you are finished using the MEMPTR, you must free the associated memory, by setting the MEMTER to zero bytes with the SET-SIZE statement.
longchar
A LONGCHAR variable to contain the XML string in memory.
The AVM saves the XML string to the LONGCHAR variable in the code page that corresponds to the character encoding you specify in the encoding option. If you do not specify a character encoding for the XML string, the AVM saves the LONGCHAR variable in UTF-8.
If the LONGCHAR variable's code page is fixed (that is, set using the FIX-CODEPAGE statement) and the fixed code page is not equivalent to the character encoding you specify in the encoding option, the SERIALIZE-ROW( ) method generates an error message and returns FALSE. The XML string is not saved to the LONGCHAR.
JsonObject
A reference to a Progress.Json.ObjectModel.JsonObject object.
When target-format is "XML", this parameter value is invalid. If you specify a JsonObject parameter when target-format is "XML", SERIALIZE-ROW( ) generates an error message and returns FALSE.
formatted
An optional LOGICAL expression where TRUE directs the AVM to format the XML string in a hierarchical manner using extra white space, carriage returns, and line feeds. The default value is FALSE. If you specify the Unknown value (?), the method uses the default value of FALSE.
encoding
An optional CHARACTER expression that specifies the name of the character encoding the AVM uses to write the XML string. The default encoding is "UTF-8".
The encoding name must specify a Unicode transformation format. Valid values are "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", and "UTF-32LE".
Note: If you specify the empty string ("") or the Unknown value (?), the AVM uses the default encoding of "UTF-8".
omit-initial-values
An optional LOGICAL expression where TRUE directs the AVM to exclude temp-table fields containing their initial values from the XML string, and FALSE directs the AVM to include all temp-table field data in the XML. The default value is FALSE. If you specify the Unknown value (?), the method uses the default value of FALSE.
omit-outer-object
This argument is not valid when target-format is "XML". If set to TRUE, SERIALIZE-ROW( ) generates an error message and returns FALSE.
The following example demonstrates the use of SERIALIZE-ROW( ) to produce XML output.
DEFINE TEMP-TABLE ttCust NO-UNDO
  FIELD custNum AS INTEGER
  FIELD Name AS CHARACTER
  FIELD Address AS CHARACTER.

DEFINE VARIABLE bcust AS HANDLE.

FIND FIRST sports2000.Customer.
CREATE ttCust.
BUFFER-COPY sports2000.Customer TO ttCust.
bcust = TEMP-TABLE ttCust:DEFAULT-BUFFER-HANDLE.
bcust:SERIALIZE-ROW("XML", "file", "bcust.xml", TRUE).
The XML output that SERIALIZE-ROW( ) produces in bcust.xml follows.
<?xml version="1.0"?>
<ttCust xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<custNum>1</custNum>
<Name>Lift Tours</NAME>
<Address>276 North Drive</Address>
</ttCust>