Problem:
Release: 4.0
How to write XML with repeating elements?
Resolution:
This article shows how to create an XML file/document that has a repeating element, one that has the maxOccurs="unbounded" attribute. The following is based on the excerpt below from an XML schema file that describes Weather Forecasts. It has 2 main elements, WeatherForecastHeader and WeatherForecast which each have some child elements. In this article you'll also see the word node mentioned to refer to an element. The WeatherForecastHeader occurs only once, but the element named "WeatherForecast" can repeat any number of times as suggested by the attribute maxOccurs="unbounded".
<xs:element name="WeatherForecasts">
<xs:complexType>
<xs:sequence>
<xs:element name="WeatherForecastHeader" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="StartDate" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="EndDate" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="City" type="xs:string" minOccurs="1" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="ForeCastType" form="unqualified" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="WeatherForecast" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="ForecastDate" type="xs:string" minOccurs="1"/>
<xs:element name="ForecastHigh" type="xs:string" minOccurs="1"/>
<xs:element name="ForecastLow" type="xs:string" minOccurs="1"/>
<xs:element name="ForecastDescription" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
The following COBOL record was generated from the XML Schema file using the cbl2xml utility
and exists as the record definition of the XD for this document in the file section:
01 WeatherForecasts identified by "WeatherForecasts" namespace is
"http://FakeForecasts.com/WeatherForecasts" count in
WeatherForecasts-count.
02 WeatherForecastHeader identified by "WeatherForecastHeader"
count in WeatherForecastHeader-count.
03 StartDate PIC X(80) identified by "StartDate" count in
StartDate-count.
03 EndDate PIC X(80) identified by "EndDate" count in
EndDate-count.
03 City PIC X(80) identified by "City" count in City-count.
03 ForeCastType PIC X(80) identified by "ForeCastType" is
attribute count in ForeCastType-count.
02 WeatherForecast identified by "WeatherForecast" count in
WeatherForecast-count.
03 ForecastDate PIC X(80) identified by "ForecastDate" count in
ForecastDate-count.
03 ForecastHigh PIC X(80) identified by "ForecastHigh" count in
ForecastHigh-count.
03 ForecastLow PIC X(80) identified by "ForecastLow" count in
ForecastLow-count.
03 ForecastDescription PIC X(80) identified by
"ForecastDescription" count in ForecastDescription-count.
To write WeatherForecastHeader, you would move values to the StartDate, EndDate, City,
and ForeCastType fields since they belong to WeatherForecastHeader, and write it out using the
statement:
Write WeatherForecasts key is all WeatherForecastHeader.
When you use "key is" in the write statement, the data is not actually written to disk. Instead,
it adds the node specified to the internal (in memory) representation of the XML document. If "all"
is also specified in the write statement, any contained nodes (StartDate, EndDate, City, and
ForecastType in this case) are also added to the internal representation of the XML document.
To write WeatherForecast, you need to move values to the fields ForecastDate, ForecastHigh,
ForecastLow, and ForecastDescription since they belong to WeatherForecast and then issue
the following statement:
Write WeatherForecasts key is all WeatherForecast.
Repeat this for each WeatherForecast that needs to be written. To complete the XML document and actually write it to disk, use the write statement on the 01 level of the COBOL record description for WeatherForecasts. Do not use the "key is" phrase. The write statement without "key is" takes the record specified and writes the internal representation to disk.
Write WeatherForecasts.
The xml file status on this last write should contain the number of bytes written to disk. This
applies to Net Express 4.0 with XML fixpack V4.0.004 or higher. The writes with the "key is" phrase should issue an xml status of zero unless the write fails, in which case the status should be a negative number.
Please see the attached demo for a working program.