This section includes two sample files.
The following sample program calls an XML file from the National Weather Service Web site and retrieves weather data for use in a COBOL program. (It does not modify or write to the file in any way.)
*Retrieve the xml data and parse it call "C$XML" using CXML-PARSE-FILE "http://www.nws.noaa.gov/data/current_obs/KMYF.xml move return-code to parser-handle *Move to the data item of the record, which is a *child of the record name element. call "C$XML" using CXML-GET-FIRST-CHILD parser-handle. move return-code to ele-1-handle *Get the desired fields, which are all siblings of the first *child. Get the handle to the desired sibling, then get the *data for that element using that handle. *General outlook field call "C$XML" using CXML-GET-SIBLING-BY-NAME ele-1-handle "weather" 0 move return-code to ele-2-handle call "C$XML" using CXML-GET-DATA ele-2-handle throw-away weather-val. *Temperature field call "C$XML" using CXML-GET-SIBLING-BY-NAME ele-1-handle "temperature_str" 0 move return-code to ele-2-handle call "C$XML" using CXML-GET-DATA ele-2-handle throw-away temp-val. *Relative humidity field call "C$XML" using CXML-GET-SIBLING-BY-NAME ele-1-handle "relative_humidi" 0 move return-code to ele-2-handle call "C$XML" using CXML-GET-DATA ele-2-handle throw-away humid-val. *Wind direction field call "C$XML" using CXML-GET-SIBLING-BY-NAME ele-1-handle "wind_dir" 0 move return-code to ele-2-handle call "C$XML" using CXML-GET-DATA ele-2-handle throw-away wind-dir. *Wind speed field call "C$XML" using CXML-GET-SIBLING-BY-NAME ele-1-handle "wind_mph" 0 move return-code to ele-2-handle call "C$XML" using CXML-GET-DATA ele-2-handle throw-away wind-mph. *Visibility field call "C$XML" using CXML-GET-SIBLING-BY-NAME ele-1-handle "visibility" 0 move return-code to ele-2-handle call "C$XML" using CXML-GET-DATA ele-2-handle throw-away vis-val.
The following sample shows how to create an XML file, add elements to it, and write data to the elements.
program-id. test. working-storage section. 01 parser-handle usage is handle. 01 element-handle usage is handle. COPY "acucobol.def". procedure division. main-logic. *Create a new XML file call "C$XML" using CXML-NEW-PARSER move return-code to parser-handle. *Add a top element (using the name of the file) call "C$XML" using CXML-ADD-CHILD parser-handle "custRec" move return-code to element-handle. *Add some namespace information call "C$XML" using CXML-ADD-ATTRIBUTE element-handle "xmlns:xsi" "http://www.w3.org/2001/XMLSchema-instance". *Add the first field of the record, which will be a child of *the last element. call "C$XML" using CXML-ADD-CHILD element-handle "cus-key" "555-55-5555" move return-code to element-handle. *Add the rest of the record call "C$XML" using CXML-ADD-SIBLING element-handle "cus-name" "Acucorp" move return-code to element-handle. call "C$XML" using CXML-ADD-SIBLING element-handle "cus-addr" "8515 Miralani Drive" move return-code to element-handle. call "C$XML" using CXML-ADD-SIBLING element-handle "cus-city" "San Diego" move return-code to element-handle. call "C$XML" using CXML-ADD-SIBLING element-handle "cus-state" "CA" move return-code to element-handle. call "C$XML" using CXML-ADD-SIBLING element-handle "cus-zip" "92126" *write the file call "C$XML" using CXML-WRITE-FILE, parser-handle "custRec.xml". call "C$XML" using CXML-RELEASE-PARSER, parser-handle. stop run.