XML GENERATE
The XML GENERATE statement converts data to XML format.
General Format
XML GENERATE Xml-Stream FROM Xml-Data
 
    [ COUNT IN Counter ]
 
    [ ON EXCEPTION Imperative-Statement-1 ]
 
    [ NOT ON EXCEPTION Imperative-Statement-2 ]
[END-XML]
Syntax Rules
1. Xml-Stream must reference an elementary data item of category alphanumeric, an alphanumeric group item or an elementary data item of category national. If it references an alphanumeric group item, then it is treated as though it were an elementary data item of category alphanumeric.
2. Xml-Data can be a group or elementary data-item. It cannot be a function identifier or be reference modified, but it can be subscripted, and must not overlap with Xml-Stream or Counter.
3. Counter must be a numeric data item.
General Rules
1. If the COUNT IN phrase is specified, after execution of the XML GENERATE statement Counter contains the count of generated XML character positions.
2. An exception condition exists when an error occurs during generation of the XML document, for example if Xml-Stream is not large enough to contain the generated XML document. In this case the XML generation stops and the content of the receiver is undefined. If the COUNT IN phrase is specified, Counter contains the number of character positions that were generated, which can range from 0 to the length of Xml-Stream. If the ON EXCEPTION phrase is specified, control is transferred to Imperative-Statement-1, otherwise control is transferred to the end of the XML GENERATE statement.
3. If an exception condition does not occur during generation of the XML document, control is passed to Imperative-Statement-2, if specified, otherwise to the end of the XML GENERATE statement.
4. When the statement completes, two special registers are set:
o XML-CODE contains the error type. Possible values are
0
Ok
1
Warning
10
Recoverable Error
100
Fatal Error
o XML-ERRMSG contains the error message string, if an error occurred.
Examples
Generate an XML structure from a data structure
working-storage section.
 01 my-html.
    05 my-head  pic x(10value "first".
    05 my-body.
       10 my-paragraph pic x(20value "paragraph 1".
       10 my-table.
          15 my-row-1.
             20 my-col-1 pic x(20value "col 1"
             20 my-col-2 pic x(20value "col 2"
          15 my-row-2.
             20 my-col-1 pic x(20value "col 1"
             20 my-col-2 pic x(20value "col 2"
 01 the-xml  pic x any length.
 
 procedure division.
 main.
  xml generate the-xml from my-html
  end-xml
  display the-xml.
*> It will display the following:
*> <my-html><my-head>first</my-head><my-body><my-paragraph>paragraph 1</my-paragraph><my-table><my-row-1><my-col-1>col 1</my-col-1><my-col-2>col 2</my-col-2></my-row-1><my-row-2><my-col-1>col 1</my-col-1><my-col-2>col 2</my-col-2></my-row-2></my-table></my-body></my-html>