Problem:
The following is an example of how to use the XML PARSE statement within Net Express.
This example is different to the XMLPARSE example on the SupportLine Net Express examples page. That demonstrates the use of the Microsoft XML parser MSXML COM server, whereas this example demonstrates IBM XML PARSE.
Resolution:
$SET MF
id division.
program-id. testparse.
data division.
working-storage section.
01 xml-document.
05 pic x(36) value '<?xml version="1.0" encoding="UTF-8"'.
05 pic x(19) value ' standalone="yes"?>'.
05 pic x(39) value '<!--This document is just an example-->'.
05 pic x(10) value '<sandwich>'.
05 pic x(35) value ' <bread type="baker's best"/>'.
05 pic x(41) value ' <?spread please use real mayonnaise ?>'.
05 pic x(31) value ' <meat>Ham & turkey</meat>'.
05 pic x(40) value ' <filling>Cheese, lettuce, tomato, etc.'.
05 pic x(10) value '</filling>'.
05 pic x(35) value ' <![CDATA[We should add a <relish>'.
05 pic x(22) value ' element in future!]]>'.
05 pic x(31) value ' <listprice>$4.99 </listprice>'.
05 pic x(27) value ' <discount>0.10</discount>'.
05 pic x(11) value '</sandwich>'.
01 xml-document-length pic 9(3) comp-5.
*----------------------------------------------------------------*
* Sample data definitions for processing numeric XML content. *
*----------------------------------------------------------------*
01 current-element pic x(30).
01 xfr-ed pic x(9) justified.
01 list-price pic 9v99 value 0.
01 discount pic 9v99 value 0.
01 display-price pic $$9.99.
01 any-key pic x.
procedure division.
000-begin.
XML PARSE xml-document PROCESSING PROCEDURE 100-xml-handler
ON EXCEPTION
display 'XML document error ' XML-CODE
NOT ON EXCEPTION
display 'XML document successfully parsed'
END-XML
display ' '
display ' Using information from XML '
display ' '
move list-price to display-price
display ' Sandwich list price: ' display-price
compute display-price = list-price * (1 - discount)
display ' Promotional price: ' display-price
display ' Get one today!'
accept any-key
stop run.
*----------------------------------------------------------------*
100-xml-handler.
evaluate XML-EVENT
*==> Order XML events mostfrequentfirst
when 'START-OF-ELEMENT'
display 'Start element tag: <' XML-TEXT '>'
move XML-TEXT to current-element
when 'CONTENT-CHARACTERS'
display 'Content characters: <' XML-TEXT '>'
*==> Transform XML content to operational COBOL data item...
evaluate current-element
when 'listprice'
*==> Using function NUMVAL-C...
compute list-price = function numval-c(XML-TEXT)
when 'discount'
*==> Using de-editing of a numeric edited item...
compute discount = function numval-c(XML-TEXT)
end-evaluate
when 'END-OF-ELEMENT'
display 'End element tag: <' XML-TEXT '>'
move spaces to current-element
when 'START-OF-DOCUMENT'
compute xml-document-length = function length(XML-TEXT)
display 'Start of document: length= '
xml-document-length ' characters.'
when 'END-OF-DOCUMENT'
display 'End of document.'
when 'VERSION-INFORMATION'
display 'Version: <' XML-TEXT '>'
when 'ENCODING-DECLARATION'
display 'Encoding: <' XML-TEXT '>'
when 'STANDALONE-DECLARATION'
display 'Standalone: <' XML-TEXT '>'
when 'ATTRIBUTE-NAME'
display 'Attribute name: <' XML-TEXT '>'
when 'ATTRIBUTE-CHARACTERS'
display 'Attribute value characters: <' XML-TEXT '>'
when 'ATTRIBUTE-CHARACTER'
display 'Attribute value character: <' XML-TEXT '>'
when 'START-OF-CDATA-SECTION'
display 'Start of CData: <' XML-TEXT '>'
when 'END-OF-CDATA-SECTION'
display 'End of CData: <' XML-TEXT '>'
when 'CONTENT-CHARACTER'
display 'Content character: <' XML-TEXT '>'
when 'PROCESSING-INSTRUCTION-TARGET'
display 'PI target: <' XML-TEXT '>'
when 'PROCESSING-INSTRUCTION-DATA'
display 'PI data: <' XML-TEXT '>'
when 'COMMENT'
display 'Comment: <' XML-TEXT '>'
when 'EXCEPTION'
compute xml-document-length = function length(XML-TEXT)
display 'Exception ' XML-CODE ' at offset'
xml-document-length '.'
when other
display 'Unexpected XML event: ' XML-EVENT '.'
end-evaluate.