XML and what 3.2 lacks

The previous two lessons showed JSON in this compiler: writing it works, reading it does not. XML is the same story with a different line between the half that works and the half that does not, and this page draws that line with the output of four builds.

XML GENERATE

XML GENERATE writes a record as an element tree, and it escapes what XML requires to be escaped:

IDENTIFICATION DIVISION. PROGRAM-ID. X1. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUS-REC. 05 CUS-ID PIC 9(3). 05 CUS-NAME PIC X(10). 01 XML-OUT PIC X(300). 01 XML-LEN PIC 9(3). PROCEDURE DIVISION. MOVE 42 TO CUS-ID. MOVE "Ada & Bob" TO CUS-NAME. XML GENERATE XML-OUT FROM CUS-REC COUNT XML-LEN DISPLAY "[" XML-OUT(1:XML-LEN) "]". STOP RUN.

cobc -x -free -Wall -o x1 x1.cob
./x1

[<CUS-REC><CUS-ID>42</CUS-ID><CUS-NAME>Ada &amp; Bob</CUS-NAME></CUS-REC>]

The element names are the COBOL names, the record is the outer element, the number lost its leading zeros because the field is numeric, and the ampersand of the name became &amp;. The count gave the length of the document, so a program can write XML-OUT(1:XML-LEN) and nothing more.

A table, which it cannot generate

A table is the one shape a document needs and this compiler cannot make. The statement builds with a warning and the output holds the first entry only:

IDENTIFICATION DIVISION. PROGRAM-ID. X3. DATA DIVISION. WORKING-STORAGE SECTION. 01 ITEMS. 05 ITEM-ENTRY OCCURS 3 TIMES. 10 ITEM-NAME PIC X(8). 01 XML-OUT PIC X(400). 01 XML-LEN PIC 9(3). PROCEDURE DIVISION. MOVE "apple" TO ITEM-NAME(1). MOVE "fig" TO ITEM-NAME(2). MOVE "pear" TO ITEM-NAME(3). XML GENERATE XML-OUT FROM ITEMS COUNT XML-LEN DISPLAY XML-OUT(1:XML-LEN). STOP RUN.

cobc -x -free -Wall -o x3 x3.cob
./x3

x3.cob:15: warning: OCCURS items in JSON/XML GENERATE is not implemented [-Wpending]
13 | MOVE "pear" TO ITEM-NAME(3).
14 | XML GENERATE XML-OUT FROM ITEMS COUNT XML-LEN
15 > DISPLAY XML-OUT(1:XML-LEN).
16 | STOP RUN.
17 | <EOF>
<ITEMS><ITEM-ENTRY><ITEM-NAME>apple</ITEM-NAME></ITEM-ENTRY></ITEMS>

One ITEM-ENTRY for a table of three, and the two other names are gone. The warning points at the statement that ends the sentence, which is where the compiler noticed; the cause is the OCCURS of the description. A document with a list in it is therefore written by hand, one element at a time, with STRING and a pointer.

XML PARSE in both forms

Reading a document has two spellings in the standard and neither works here. The first puts the record after INTO:

IDENTIFICATION DIVISION. PROGRAM-ID. X2. DATA DIVISION. WORKING-STORAGE SECTION. 01 XML-IN PIC X(300) VALUE '<CUS-REC><CUS-ID>42</CUS-ID><CUS-NAME>Ada</CUS-NAME></CUS-REC>'. 01 CUS-REC. 05 CUS-ID PIC 9(3). 05 CUS-NAME PIC X(10). PROCEDURE DIVISION. XML PARSE XML-IN INTO CUS-REC END-XML. DISPLAY "id " CUS-ID. DISPLAY "name [" CUS-NAME "]". STOP RUN.

cobc -x -free -Wall -o x2 x2.cob

x2.cob:11: warning: XML PARSE is not implemented [-Wpending]
9 | 05 CUS-NAME PIC X(10).
10 | PROCEDURE DIVISION.
11 > XML PARSE XML-IN INTO CUS-REC
12 | END-XML.
13 | DISPLAY "id " CUS-ID.
x2.cob:11: error: syntax error, unexpected INTO, expecting PROCESSING

The second form is the older one, a procedure that the parser calls with an event each time. It builds, and then the events never arrive:

IDENTIFICATION DIVISION. PROGRAM-ID. X4. DATA DIVISION. WORKING-STORAGE SECTION. 01 XML-IN PIC X(300) VALUE '<CUS-REC><CUS-ID>42</CUS-ID><CUS-NAME>Ada</CUS-NAME></CUS-REC>'. 01 WS-EVENT PIC X(20). 01 WS-TEXT PIC X(100). 01 WS-N PIC 9(3) VALUE 0. PROCEDURE DIVISION. XML PARSE XML-IN PROCESSING PROCEDURE IS HANDLE-EVENT END-XML. DISPLAY "events " WS-N. STOP RUN. HANDLE-EVENT. ADD 1 TO WS-N. DISPLAY "event " WS-EVENT. IF XML-EVENT = "CONTENT-CHARACTERS" MOVE XML-TEXT TO WS-TEXT DISPLAY "text [" FUNCTION TRIM(WS-TEXT) "]" END-IF.

cobc -x -free -Wall -o x4 x4.cob
./x4

x4.cob:11: warning: XML PARSE is not implemented [-Wpending]
9 | 01 WS-N PIC 9(3) VALUE 0.
10 | PROCEDURE DIVISION.
11 > XML PARSE XML-IN PROCESSING PROCEDURE IS HANDLE-EVENT
12 | END-XML.
13 | DISPLAY "events " WS-N.
event
event
events 002

The procedure ran twice, because the two halves of the statement each call something, and XML-EVENT was empty both times. No name, no text and no event: the runtime has no parser behind the verb.

So a COBOL program of this compiler reads XML the way it reads JSON, with INSPECT and reference modification over the elements it needs, or it hands the document to a program written in another language and reads the record that comes back. The lessons on calling C and Python later in this course are the second road, and the JSON PARSE lesson before this one is the first.

Exercises

  1. Generate a document from a record with a signed amount and read the sign in the output.
  2. Put a < and a STRING, one line per element.
  3. Take the parser of the JSON lesson and make it read <CUS-NAME>Ada</CUS-NAME>.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does XML GENERATE write for a numeric field?

    Answer

    The number without its leading zeros

    The identifier 42 of a PIC 9(3) field came out as 42.

  2. What happens to an ampersand in a name field?

    Answer

    It is written as &amp;amp;, as XML requires

    Ada &amp; Bob came out as Ada &amp;amp; Bob.

  3. What does XML GENERATE do with an OCCURS table?

    Answer

    It writes the first entry and warns that the feature is not implemented

    The output held one ITEM-ENTRY and the build warned with -Wpending.

  4. Which form of XML PARSE does this compiler accept?

    Answer

    Neither: INTO is a syntax error and PROCESSING builds but sets no event

    The page shows the syntax error and the empty XML-EVENT of the second form.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9(3) VALUE 42. 01 W-OUT PIC X(100). 01 W-LEN PIC 9(3). PROCEDURE DIVISION. XML GENERATE W-OUT FROM W-N COUNT W-LEN DISPLAY W-N. STOP RUN.

    Answer

    042

    The DISPLAY of the field itself still shows the three digits of the PIC, whatever the generator wrote.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC X(10) VALUE "A B". PROCEDURE DIVISION. DISPLAY "[" FUNCTION TRIM(W-A) "]". STOP RUN.

    Answer

    [A B]

    TRIM removes the padding only, so the space between the letters stays.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-L PIC X(60) VALUE "<CUS-NAME>Ada</CUS-NAME>". 01 W-POS PIC 9(3) VALUE 0. PROCEDURE DIVISION. INSPECT W-L TALLYING W-POS FOR CHARACTERS BEFORE "<CUS-NAME>" DISPLAY W-POS. STOP RUN.

    Answer

    000

    The name element is the first thing in the line, so no characters stand before it.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(2) VALUE 3. 01 W-B PIC 9(2) VALUE 4. 01 W-R PIC 9(3). PROCEDURE DIVISION. COMPUTE W-R = W-A * W-B DISPLAY W-R. STOP RUN.

    Answer

    012

    Three times four is twelve in a three-digit field.

  9. Why does a program that needs a list in XML write it by hand?

    Answer

    The generator writes only the first entry of a table

    The table program produced a single ITEM-ENTRY for three names.

  10. What is the honest way to read XML in this compiler?

    Answer

    Read the elements it needs with INSPECT and reference modification, or hand the document to another language

    Neither form of XML PARSE fills a record in this build.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev