Rational Developer for System z
COBOL for Windows, Version 7.5, Programming Guide


Generating XML output

To transform COBOL data to XML, use the XML GENERATE statement.

XML GENERATE XML-OUTPUT FROM SOURCE-REC
      COUNT IN XML-CHAR-COUNT
  ON EXCEPTION
    DISPLAY 'XML generation error ' XML-CODE
    STOP RUN
  NOT ON EXCEPTION
    DISPLAY 'XML document was successfully generated.'
END-XML

In the XML GENERATE statement, you first identify the data item (XML-OUTPUT in the example above) that is to receive the XML output. Define the data item to be large enough to contain the generated XML output, typically five to eight times the size of the COBOL source data depending on the length of its data-name or data-names.

In the DATA DIVISION, you can declare the receiving identifier as alphanumeric (either an alphanumeric group item or an elementary item of category alphanumeric) or as national (either a national group item or an elementary item of category national).

The receiving identifier must be national if the external code pages are multibyte code pages or the XML output will contain any data from the COBOL source record that has any of the following characteristics:

If the receiving data item is alphanumeric, the code pages indicated by the compile-time locale and the runtime locale must be identical.

Next you identify the source data item that is to be transformed to XML format (SOURCE-REC in the example). The source data item can be an alphanumeric group item, national group item, or elementary data item of class alphanumeric or national. Do not specify the RENAMES clause in the data description of that data item.

If the source data item is an alphanumeric group item or a national group item, the source data item is processed with group semantics, not as an elementary item. Any groups that are subordinate to the source data item are also processed with group semantics.

Some COBOL data items are not transformed to XML, but are ignored. Subordinate data items of an alphanumeric group item or national group item that you transform to XML are ignored if they:

These items in the source data item are also ignored when you generate XML:

There must be at least one elementary data item that is not ignored when you generate XML. For the data items that are not ignored, ensure that the identifier that you transform to XML satisfies these conditions when you declare it in the DATA DIVISION:

An XML declaration is not generated. No white space (for example, new lines or indentation) is inserted to make the generated XML more readable.

Optionally, you can code the COUNT IN phrase to obtain the number of XML character positions that are filled during generation of the XML output. Declare the count field as an integer data item that does not have the symbol P in its PICTURE string. You can use the count field and reference modification to obtain only that portion of the receiving data item that contains the generated XML output. For example, XML-OUTPUT(1:XML-CHAR-COUNT) references the first XML-CHAR-COUNT character positions of XML-OUTPUT.

In addition, you can specify either or both of the following phrases to receive control after generation of the XML document:

You can end the XML GENERATE statement with the explicit scope terminator END-XML. Code END-XML to nest an XML GENERATE statement that has the ON EXCEPTION or NOT ON EXCEPTION phrase in a conditional statement.

XML generation continues until either the COBOL source record has been transformed to XML or an error occurs. If an error occurs, the results are as follows:

If no error occurs during XML generation, special register XML-CODE contains zero, and control is passed to the NOT ON EXCEPTION phrase if specified or to the end of the XML GENERATE statement otherwise.

Example: generating XML

related tasks
Controlling the encoding of generated XML output
Handling errors in generating XML output

related references
Locales and code pages that are supported
Classes and categories of data (COBOL for Windows Language Reference)
XML GENERATE statement (COBOL for Windows Language Reference)
Operation of XML GENERATE (COBOL for Windows Language Reference)
Extensible Markup Language (XML)


Terms of use | Feedback

Copyright IBM Corporation 1996, 2008.
This information center is powered by Eclipse technology. (http://www.eclipse.org)