Rational Developer for System z
Enterprise COBOL for z/OS バージョン 4.1 プログラミング・ガイド


例: XML の処理用プログラム

以下の例では、XML PARSE ステートメントおよび処理プロシージャーの使用方法を示します。

この XML 文書は、構文解析のフローを確認する目的のソースです。XMLPARSE(XMLSS) コンパイラー・オプションおよび XMLPARSE(COMPAT) コンパイラー・オプションの両方のプログラムの出力を以下に示します。 XML 文書とプログラムの出力結果を比較して、パーサーと処理プロシージャーとの間の相互作用を確認し、イベントと文書フラグメントを突き合わせてください。

 cbl codepage(1047)
 Identification division.
   Program-id. XMLSAMPL.

 Data division.
  Working-storage section.
******************************************************************
* XML document, encoded as initial values of data items.         *
******************************************************************
   1 xml-document.
    2 pic x(39) value '<?xml version="1.0" encoding="IBM-1047"'.
    2 pic x(19) value ' standalone="yes"?>'.
    2 pic x(39) value '<!--This document is just an example-->'.
    2 pic x(10) value '<sandwich>'.
    2 pic x(35) value '  <bread type="baker&apos;s best"/>'.
    2 pic x(41) value '  <?spread please use real mayonnaise  ?>'.
    2 pic x(31) value '  <meat>Ham &amp; turkey</meat>'.
    2 pic x(40) value '  <filling>Cheese, lettuce, tomato, etc.'.
    2 pic x(10) value '</filling>'.
    2 pic x(35) value '  <![CDATA[We should add a <relish>'.
    2 pic x(22) value ' element in future!]]>'.
    2 pic x(31) value '  <listprice>$4.99 </listprice>'.
    2 pic x(27) value '  <discount>0.10</discount>'.
    2 pic x(11) value '</sandwich>'.
   1 xml-document-length computational pic 999.

******************************************************************
* Sample data definitions for processing numeric XML content.    *
******************************************************************
   1 current-element pic x(30).
   1 xfr-ed pic x(9) justified.
   1 xfr-ed-1 redefines xfr-ed pic 999999.99.
   1 list-price computational pic 9v99 value 0.
   1 discount computational pic 9v99 value 0.
   1 display-price pic $$9.99.

 Procedure division.
  Mainline section.

     XML parse xml-document processing procedure xml-handler
       On exception
         Display 'XML document error ' XML-Code
       Not on exception
         Display 'XML document successfully parsed'
     End-XML

******************************************************************
*    Process the transformed content and calculate promo price.  *
******************************************************************
     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!'

     Goback.

  xml-handler section.
     Evaluate XML-Event
* ==> Order XML events most frequent first
       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...
             Move XML-Text to xfr-ed
             Move xfr-ed-1 to discount
         End-evaluate
       When 'END-OF-ELEMENT'
         Display 'End element tag: {' XML-Text '}'
         Move spaces to current-element
       When 'START-OF-DOCUMENT'
         Display 'Start of document'
       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
     .
 End program XMLSAMPL.

XMLPARSE(XMLSS) を使用した構文解析の出力例

以下の出力結果では、構文解析の各イベントが、どの文書フラグメントから発生しているかを確認することができます。

 Start of document                                                         
 Version: {1.0}                                                            
 Encoding: {IBM-1047}                                                      
 Standalone: {yes}                                                         
 Comment: {This document is just an example}                               
 Start element tag: {sandwich}                                             
 Content characters: {  }                                                  
 Start element tag: {bread}                                                
 Attribute name: {type}                                                    
 Attribute value characters: {baker's best}                                
 End element tag: {bread}                                                       
 Content characters: {  }                                                  
 PI target: {spread}                                                       
 PI data: {please use real mayonnaise  }                                   
 Content characters: {  }                                                  
 Start element tag: {meat}                                                 
 Content characters: {Ham & turkey}                                        
 End element tag: {meat}                                                       
 Content characters: {  }                                                  
 Start element tag: {filling}                                              
 Content characters: {Cheese, lettuce, tomato, etc.}                       
 End element tag: {filling}                                                       
 Content characters: {  }                                                  
 Start of CData: {}                                                        
 Content characters: {We should add a <relish> element in future!}         
 End of CData: {}                                                          
 Content characters: {  }                                                  
 Start element tag: {listprice}                                            
 Content characters: {$4.99 }                                              
 End element tag: {listprice}                                                       
 Content characters: {  }                                                  
 Start element tag: {discount}                                             
 Content characters: {0.10}                                                
 End element tag: {discount}                                                       
 End element tag: {sandwich}                                                       
 End of document.                                                          
 XML document successfully parsed                                          
                                                                           
 -----+++++***** Using information from XML *****+++++-----                
                                                                           
   Sandwich list price:  $4.99                                             
   Promotional price:    $4.49                                             
   Get one today!                                                          

XMLPARSE(COMPAT) を使用した構文解析の出力例

以下の出力結果では、構文解析の各イベントが、どの文書フラグメントから発生しているかを確認することができます。

 Start of document                                                           
 Version: {1.0}                                                              
 Encoding: {IBM-1047}                                                        
 Standalone: {yes}                                                           
 Comment: {This document is just an example}                                 
 Start element tag: {sandwich}                                               
 Content characters: {  }                                                    
 Start element tag: {bread}                                                  
 Attribute name: {type}                                                      
 Attribute value characters: {baker}                                         
 Attribute value character: {'}                                              
 Attribute value characters: {s best}                                        
 End element tag: {bread}                                                    
 Content characters: {  }                                                    
 PI target: {spread}                                                         
 PI data: {please use real mayonnaise  }                                     
 Content characters: {  }                                                    
 Start element tag: {meat}                                                   
 Content characters: {Ham }                                                  
 Content character: {&}                                                      
 Content characters: { turkey}                                               
 End element tag: {meat}                                                     
 Content characters: {  }                                                    
 Start element tag: {filling}                                                
 Content characters: {Cheese, lettuce, tomato, etc.}                         
 End element tag: {filling}                                                  
 Content characters: {  }                    
 Start of CData: {<![CDATA[}                                                 
 Content characters: {We should add a <relish> element in future!}           
 End of CData: {]]>}                                                         
 Content characters: {  }                                                    
 Start element tag: {listprice}                                              
 Content characters: {$4.99 }                                                
 End element tag: {listprice}                                                
 Content characters: {  }                                                    
 Start element tag: {discount}                                               
 Content characters: {0.10}                                                  
 End element tag: {discount}                                                 
 End element tag: {sandwich}                                                 
 End of document.                                                            
 XML document successfully parsed                                            
                                                                             
 -----+++++***** Using information from XML *****+++++-----                  
                                                                             
   Sandwich list price:  $4.99                                               
   Promotional price:    $4.49                                               
   Get one today!                                                            

ご利用条件 | フィードバック

このインフォメーション・センターでは Eclipse テクノロジーが採用されています。(http://www.eclipse.org)