XML PARSE Statement
The XML PARSE statement parses an XML document and returns data values to their corresponding data elements. This data can then be processed by the COBOL program.
General Format:
XML PARSE identifier-1
PROCESSING PROCEDURE [IS]
procedure-name-1 [THROUGH procedure-name-2]
THRU
[[ON] EXCEPTION imperative-statement-1]
[NOT [ON] EXCEPTION imperative-statement-2]
[END-XML]
Syntax Rules:
identifier-1is an alphanumeric data item that holds an XML document.- The
PROCESSING PROCEDUREphrase specifies the name of a procedure to respond to the events generated by theXML PARSEstatement. procedure-name-1andprocedure-name-2define a consecutive sequence of operations to execute, beginning at the procedure named byprocedure-name-1and ending with the execution of the procedure named byprocedure-name-2.procedure-name-1,procedure-name-2indicates a section or paragraph in thePROCEDURE DIVISION.Procedure-name-1andprocedure-name-2may not be in a declarative section.
General Rules:
- Control passes between the parser and
procedure-name-1. The parser generates an XML event,procedure-name-1(or the code executed inprocedure-name-1throughprocedure-name-2) handles the event, and control is then returned to the parser. - The processing procedure interprets the information returned from the parser, and handles it programmatically. When the processing procedure (or procedures) is complete control is returned to the XML parser.
- The processing procedure can terminate the run unit with a
STOP RUNstatement.
ON EXCEPTION phrase
An EXCEPTION condition exists when an error occurs during the XML PARSE operation. The parser signals the exception by passing control to the processing procedure and updating XML-EVENT and XML-CODE special registers. If the ON EXCEPTION phrase is specified, control passes to imperative-statement-1. For details on the XML-EVENT and XML-CODE special registers, see your IBM documentation.
NOT ON EXCEPTION phrase
If the ON EXCEPTION phrase is specified, and no EXCEPTION condition is triggered, then control passes to imperative-statement-2.
END-XML phrase
The END-XML phrase delimits the scope of both XML GENERATE and XML PARSE statements.
Nested XML statements
An XML GENERATE or XML PARSE statement located in an ON EXCEPTION imperative-statement is referred to as a nested (or conditional) XML statement. The scope of a conditional XML GENERATE or XML PARSE statement is terminated by either an END-XML phrase at the same level of nesting, or by a separator period.
Special Registers
In the control flow between the parser and the processing procedure, the parser returns control to the processing procedure, and updates the special registers XML-CODE, XML-EVENT, and XML-TEXT.
XML-CODE
- The
XML-CODEspecial register is implicitly defineds as:01 XML-CODE PICTURE S9(9) USAGE BINARY VALUE 0. - The
XML-CODEspecial register communicates status between the XML parser and the processing procedure defined in anXML PARSEstatement. TheXML CODEspecial register also indicates whether anXML GENERATEstatement executed successfully or an exception occurred dur ing XML generation. - When an
XML PARSEstatement terminates, the special registerXML-CODEis populated either with a "0" to indicate a successful operation, or with a non zero error code. - Details on handling non zero error codes is detailed in the IBM Enterprise COBOL Programming Guide, in the "Handling XML Parse exceptions" chapter.LINK
XML-EVENT
- The
XML_EVENTspecial register is implicitly defined as:01 XML-EVENT USAGE DISPLAY PICTURE X(30) VALUE SPACE. - The
XML-EVENTspecial register communicates event information from the XML parser to the processing procedure defined in anXML PARSEstatement. - The
XML-EVENTspecial register is set to the name of the XML event. - XML events and associated special register contents are listed in the IBM COBOL Enterprise COBOL Programming Guide.
XML-TEXT
XML-TEXTis an elementary alphanumeric data item of the length of the contained XML document fragment. The length ofXML-TEXTcan vary from 0 to 16,777,215 bytes. There is no equivalent COBOL data description entry.- The
XML-TEXTspecial register contains document fragments that are of class alphanumeric. - The XML parser sets
XML-TEXTto the document fragment associated with anXML-EVENTbefore transferring control to the processing procedure when the operand of theXML PARSEstatement is an alphanumeric data item. Use theLENGTHfunction forXML-TEXTto determine the number of bytes thatXML-TEXTcontains. XML-TEXTcannot be used as a receiving item.XML EVENT's and associatedXML-TEXTcontents are listed in the IBM COBOL Enterprise COBOL Programming Guide.