MainframeMaster

COBOL Tutorial

COBOL XML GENERATE - Quick Reference

Progress0 of 0 lessons

Overview

XML GENERATE is a COBOL feature that allows you to automatically generate XML documents from COBOL data structures. It converts COBOL records and data items into well-formed XML format, making it easy to create XML output for web services, data exchange, or other XML-based applications.

Purpose and Usage

  • Automatic XML generation from COBOL data
  • Web service integration
  • Data exchange with modern systems
  • XML report generation
  • Legacy system modernization

Syntax

XML GENERATE follows specific syntax patterns for creating XML documents:

Basic XML GENERATE Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
* Basic XML GENERATE syntax XML GENERATE xml-output FROM data-structure. * XML GENERATE with options XML GENERATE xml-output FROM data-structure COUNT IN xml-length ON EXCEPTION PERFORM xml-error-handling END-XML. * XML GENERATE with specific options XML GENERATE xml-output FROM customer-record COUNT IN xml-size ON EXCEPTION PERFORM handle-xml-error NOT ON EXCEPTION PERFORM process-generated-xml END-XML.

XML GENERATE automatically creates XML from COBOL data structures.

Data Structure Definition

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
* Data structure for XML generation 01 customer-record. 05 customer-id PIC X(10). 05 customer-name PIC X(50). 05 customer-email PIC X(100). 05 customer-address. 10 street PIC X(100). 10 city PIC X(50). 10 state PIC X(2). 10 zip-code PIC X(10). 05 customer-phone PIC X(15). 01 xml-output PIC X(2000). 01 xml-length PIC 9(5). * XML GENERATE usage XML GENERATE xml-output FROM customer-record COUNT IN xml-length ON EXCEPTION DISPLAY "XML generation failed" NOT ON EXCEPTION DISPLAY "XML generated successfully" DISPLAY "Length: " xml-length END-XML.

Practical Examples

Here are some practical uses of XML GENERATE in COBOL:

Customer Data Export

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
* Exporting customer data to XML DATA DIVISION. WORKING-STORAGE SECTION. 01 customer-data. 05 customer-id PIC X(10). 05 customer-name PIC X(50). 05 customer-email PIC X(100). 05 customer-status PIC X(1). 01 xml-output PIC X(2000). 01 xml-length PIC 9(5). PROCEDURE DIVISION. EXPORT-CUSTOMER-XML. * Load customer data MOVE "CUST001" TO customer-id. MOVE "John Doe" TO customer-name. MOVE "john.doe@email.com" TO customer-email. MOVE "A" TO customer-status. * Generate XML XML GENERATE xml-output FROM customer-data COUNT IN xml-length ON EXCEPTION DISPLAY "Error generating XML" PERFORM error-handling NOT ON EXCEPTION DISPLAY "XML generated successfully" PERFORM save-xml-to-file END-XML. STOP RUN. SAVE-XML-TO-FILE. * Save generated XML to file OPEN OUTPUT xml-file. WRITE xml-record FROM xml-output. CLOSE xml-file. DISPLAY "XML saved to file". * Generated XML example: * * CUST001 * John Doe * john.doe@email.com * A *

Exporting customer data to XML format using XML GENERATE.

Web Service Response

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
* Creating web service response XML DATA DIVISION. WORKING-STORAGE SECTION. 01 service-response. 05 response-status PIC X(10). 05 response-message PIC X(100). 05 response-data. 10 data-count PIC 9(4). 10 data-items OCCURS 10 TIMES. 15 item-id PIC X(10). 15 item-name PIC X(50). 15 item-value PIC 9(8)V99. 01 xml-response PIC X(5000). 01 response-length PIC 9(5). PROCEDURE DIVISION. CREATE-WEB-SERVICE-RESPONSE. * Prepare response data MOVE "SUCCESS" TO response-status. MOVE "Data retrieved successfully" TO response-message. MOVE 3 TO data-count. * Set data items MOVE "ITEM001" TO item-id(1). MOVE "Product A" TO item-name(1). MOVE 100.50 TO item-value(1). MOVE "ITEM002" TO item-id(2). MOVE "Product B" TO item-name(2). MOVE 200.75 TO item-value(2). MOVE "ITEM003" TO item-id(3). MOVE "Product C" TO item-name(3). MOVE 150.25 TO item-value(3). * Generate XML response XML GENERATE xml-response FROM service-response COUNT IN response-length ON EXCEPTION PERFORM handle-generation-error NOT ON EXCEPTION PERFORM send-xml-response END-XML. STOP RUN. SEND-XML-RESPONSE. * Send XML response to client DISPLAY "Content-Type: application/xml". DISPLAY "Content-Length: " response-length. DISPLAY xml-response. * Generated XML example: * * SUCCESS * Data retrieved successfully * * 3 * * ITEM001 * Product A * 100.50 * * * *

Creating web service response XML using XML GENERATE.

Best Practices

  • Always provide exception handling for XML generation errors.
  • Use meaningful data structure names that will become XML element names.
  • Validate data before generating XML to ensure proper output.
  • Consider the size of the output field to accommodate the generated XML.
  • Test XML generation with various data scenarios.
  • Document the expected XML structure and format.

Common Pitfalls

  • Not providing adequate space for generated XML output.
  • Failing to handle XML generation exceptions.
  • Using inappropriate data types that don\'t convert well to XML.
  • Not considering XML namespace requirements.
  • Creating overly complex data structures that generate confusing XML.

Test Your Knowledge

1. What is XML GENERATE in COBOL?

  • A file operation
  • An XML generation mechanism
  • A data declaration
  • A program structure

2. What does XML GENERATE do?

  • Parses XML data
  • Generates XML from COBOL data
  • Validates XML
  • Transforms XML

3. What type of data can XML GENERATE process?

  • Only numeric data
  • Only alphanumeric data
  • COBOL data structures and records
  • Only file data

4. Where is the generated XML stored?

  • In a file
  • In a COBOL field
  • In memory
  • All of the above

5. What is the primary benefit of using XML GENERATE?

  • Faster processing
  • Automatic XML creation from COBOL data
  • Better error handling
  • Memory efficiency

Frequently Asked Questions