The XML GENERATE statement converts data to XML format.
Format 1 >>-XML GENERATE--identifier-1--FROM--identifier-2---------------> >--+-----------------------------+------------------------------> '-COUNT--+----+--identifier-3-' '-IN-' >--+-------------------------------------------+----------------> '-+----+--EXCEPTION--imperative-statement-1-' '-ON-' >--+------------------------------------------------+-----------> '-NOT--+----+--EXCEPTION--imperative-statement-2-' '-ON-' >--+---------+------------------------------------------------->< '-END-XML-'
Format 2 >>-XML GENERATE--FILE-STREAM--+-----------+--identifier-4-------> +-APPEND----+ '-OVERWRITE-' >--FROM--identifier-2--+-----------------------------+----------> '-COUNT--+----+--identifier-3-' '-IN-' >--+-------------------------------------------+----------------> '-+----+--EXCEPTION--imperative-statement-1-' '-ON-' >--+------------------------------------------------+-----------> '-NOT--+----+--EXCEPTION--imperative-statement-2-' '-ON-' >--+---------+------------------------------------------------->< '-END-XML-'
When identifier-1 references an alphanumeric group item, identifier-1 is treated as though it were an elementary data item of category alphanumeric.
identifier-1 must not be described with the JUSTIFIED clause, and cannot be a function identifier. identifier-1 can be subscripted or reference modified.
identifier-1 must not overlap identifier-2 or identifier-3.
If identifier-1 references a data item of category alphanumeric, the generated XML document is encoded with the CCSID specified by the PROCESS statement CCSID option d - XML GENERATE single-byte data CCSID in effect when the source code was compiled. If the CCSID in effect is 65535, the job default CCSID at run time will be used.
If identifier-1 references a data item of category
national, the generated XML document is encoded in UCS-2.
If PROCESS statement CCSID option d specifies a National CCSID, that CCSID is used.
Otherwise, the
CCSID specified by the NTLCCSID PROCESS option is used.
A byte order
mark is not generated.
identifier-1 must reference a data item of category national if the generated XML includes data from identifier-2 for:
identifier-1 must be large enough to contain the generated XML document. Typically, it should be from five to eight times the size of identifier-2, depending on the length of the data-name or data-names within identifier-2. If identifier-1 is not large enough, an error condition exists at the end of the XML GENERATE statement.
identifier-2 must not overlap with identifier-1 or identifier-3.
identifier-2 must not specify the RENAMES clause.
The following data items specified by identifier-2 are ignored by the XML GENERATE statement:
All data items specified by identifier-2 that are not ignored according to the rules above must satisfy the following conditions:
For example, given the following data declaration:
01 STRUCT.
02 STAT PIC X(4).
02 IN-AREA PIC X(100).
02 OK-AREA REDEFINES IN-AREA.
03 FLAGS PIC X.
03 PIC X(3).
03 COUNTER USAGE COMP PIC S9(9).
03 ASFNPTR REDEFINES COUNTER USAGE PROCEDURE-POINTER.
03 UNREFERENCED PIC X(92).
02 NG-AREA1 REDEFINES IN-AREA.
03 FLAGS PIC X.
03 PIC X(3).
03 PTR USAGE POINTER.
03 ASNUM REDEFINES PTR USAGE COMP PIC S9(9).
03 PIC X(92).
02 NG-AREA2 REDEFINES IN-AREA.
03 FN-CODE PIC X.
03 UNREFERENCED PIC X(3).
03 QTYONHAND USAGE BINARY PIC 9(5).
03 DESC USAGE NATIONAL PIC N(40).
03 UNREFERENCED PIC X(12).
The following data items can be specified as identifier-2:
The following data items cannot be specified as identifier-2:
identifier-3 must not overlap identifier-1 or identifier-2.
When no APPEND or OVERWRITE phrase is used, a new file will be created with the XML file encoding CCSID and the converted XML data will be saved into it. If a file with the same name exists when running a program, XML generation stops and the special register XML-CODE contains an exception code representing this error.
If APPEND phrase is used, the converted XML data will be appended to the existing file when the file has the XML file encoding CCSID; otherwise XML generation stops and the special register XML-CODE contains an exception code representing this error.
However, if PROCESS option XMLGEN(KEEPFILEOPEN) has been specified and the IFS file is currently open,
then specifying XML GENERATE without the APPEND or OVERWRITE phrase can be used to close the IFS file,
and no error or exception code will be issued 
If OVERWRITE phrase is used, the existing file will be replaced by a new file with the XML file encoding CCSID; the converted XML data will be saved into the new file.
Any other file operation errors except those mentioned above will trigger a runtime inquiry message including file operation error message. If "G" is answered to continue the operation, an exception code will be set in special register XML-CODE.
If the ON EXCEPTION phrase is specified, control is transferred to imperative-statement-1. If the ON EXCEPTION phrase is not specified, the NOT ON EXCEPTION phrase, if any, is ignored, and control is transferred to the end of the XML GENERATE statement. Special register XML-CODE contains an exception code, as detailed in the IBM Rational Development Studio for i: ILE COBOL Programmer's Guide.
The scope of a conditional XML GENERATE or XML PARSE statement can be terminated by:
END-XML can also be used with an XML GENERATE or XML PARSE statement that does not specify either the ON EXCEPTION or the NOT ON EXCEPTION phrase.