Serialize XML from a dynamic array
The XMLList and XMLName attributes can be used to serialize dynamic arrays to XML and vice versa.
These attributes have distinct uses depending on the output you want.
The XMLList
attribute is used especially
to produce a list of the same elements from an array, without a surrounding tag. This output on its
own is not valid XML because there is no parent node. For valid XML output, the attribute must be
used inside another variable, such as a record. Compare Example 1: List of the same elements and Example 3: List inside a record.
It is useful to use XMLList
specifically to map a one dimensional array to an
XML schema element that has more than one occurrence, inside another XML element.
An array (without XMLList
), on the other hand, always produces a surrounding
tag and thus a valid XML document. See Example 2: Array of the same elements and Example 4: Array inside a record.
XMLName
attribute's function is to
provide a name for XML elements. If you do not use the XMLName
attribute, the tags
used in the XML output are the names of the 4GL variables. Or if there are no variables, they are
named "<element>". DYNAMIC ARRAY
.
Example 1: List of the same elements
XMLList
outputs a list of the same elements. Typically,
this array is defined inside another variable (see Example 3: List inside a record), because the resulting XML is not
valid otherwise. The XMLName
attribute is set to name the element.
DEFINE a RECORD
list DYNAMIC ARRAY ATTRIBUTES(XMLList) OF STRING ATTRIBUTE(XMLName="MyElt")
END RECORD
<MyElt>One</MyElt><MyElt>Two</MyElt>
When viewed in an XML
viewer, it would display as shown.
<MyElt>One</MyElt> <MyElt>Two</MyElt>
Example 2: Array of the same elements
In this example an array is defined without XMLList
. This produces output that
is a valid XML document with an array of the same elements, and a surrounding tag. The
XMLName
attribute tags both the array and the elements.
DEFINE arr DYNAMIC ARRAY ATTRIBUTE(XMLName="SurroundingTag") OF STRING ATTRIBUTE(XMLName="MyElt")
The
XML output may look like
this:<SurroundingTag><MyElt>One</MyElt><MyElt>Two</MyElt></SurroundingTag>
When
viewed in an XML viewer, it would display as shown.
<SurroundingTag> <MyElt>One</MyElt> <MyElt>Two</MyElt> </SurroundingTag>
Example 3: List inside a record
The XMLList
must be defined inside another variable, such as a record, in order
to produce output that is a valid XML document. Several lists may be included in the record.
DEFINE myVar1 RECORD ATTRIBUTES(XMLName="Root")
val1 INTEGER ATTRIBUTES(XMLName="Val1"),
list DYNAMIC ARRAY ATTRIBUTE(XMLList) OF STRING ATTRIBUTES(XMLName="MyElt"),
val2 FLOAT ATTRIBUTES(XMLName="Val2")
END RECORD
This
would produce XML output like
this:<Root><Val1>148</Val1><MyElt>hello</MyElt><MyElt>there</MyElt><Val2>0.58</Val2></Root>
When
viewed in an XML viewer, it would display as shown.
<Root> <Val1>148</Val1> <MyElt>hello</MyElt> <MyElt>there</MyElt> <Val2>0.58</Val2> </Root>
Example 4: Array inside a record
This example shows that the array is output inside a surrounding tag inside the record.
DEFINE myVar2 RECORD ATTRIBUTES(XMLName="Root")
val1 INTEGER ATTRIBUTES(XMLName="Val1"),
arr DYNAMIC ARRAY ATTRIBUTES(XMLName="Sample") OF STRING ATTRIBUTES(XMLName="MyElt"),
val2 FLOAT ATTRIBUTES(XMLName="Val2")
END RECORD
This
would produce XML output like
this:<Root><Val1>148</Val1><Sample><MyElt>hello</MyElt><MyElt>there</MyElt></Sample><Val2>0.58</Val2></Root>
When
viewed in an XML viewer, it would display as shown.
<Root> <Val1>148</Val1> <Sample> <MyElt>hello</MyElt> <MyElt>there</MyElt> </Sample> <Val2>0.58</Val2> </Root>