XML ドキュメントへのデータ値の設定

次の段階では、COBOL プログラムにデータ値を設定します。ここでも、プログラムの変更箇所が太字で示されています。

Identification Division.
Program-Id. Getting-Started.
Data Division.
Working-Storage Section.
01  Customer-Address.
    02  Cust-Name     Pic X(128).
    02  Address-1     Pic X(128).
    02  Address-2     Pic X(128).
    02  Address-3.
        03  City      Pic X(64).
        03  State     Pic X(2).
        03  Zip       Pic 9(5) Value 0 Binary.
Copy "lixmlall.cpy".
Procedure Division.
A.
    XML INITIALIZE.
    If Not XML-OK Go To Z.

    Move "Micro Focus" to Cust-Name.
    Move "8310 Capital of Texas Highway, North"
        to Address-1.
    Move "Building 2, Suite 100" to Address-2.
    Move "Austin" to City.
    Move "TX" to State.
    Move 78731 to Zip.

     XML EXPORT FILE
         Customer-Address
         "Address"
         "getstarted#customer-address".
     If Not XML-OK Go To Z.

Z.
Copy "lixmltrm.cpy".
    GoBack.
Copy "lixmldsp.cpy".
End Program  Getting-Started.

簡単な MOVE 文を使用してデータ構造の内容を指定しています。

ここでもう一度、コマンド ラインからプログラムをコンパイルして実行します。次のコマンドを使用します。

cobol getstarted.cbl xmlgen(ws) noobj; run getstarted

今度は、次のように、XML ドキュメント (Address.xml ファイル) にすべてのデータ値が設定されます。

<?xml version="1.0" encoding="UTF-8"?>
 <customer-address xmlns:xtk="http://www.microfocus.com/xcentrisity/
                             \xml-extensions/symbol-table/">
  <cust-name>Micro Focus</cust-name>
  <address-1>8310 Capital of Texas Highway, North</address-1>
  <address-2>Building 2, Suite 100</address-2>
  <address-3>
   <city>Austin</city>
   <state>TX</state>
   <zip>78731</zip>
  </address-3>
 </customer-address>

UNIX では次のコマンドを使用します。

cob -i getstarted.cbl -C 'xmlgen(ws)' cobrun getstarted