Developer > General

Developer

Processing XML Documents with COBOL

General - Processing XML Documents with COBOL

Bookmark and Share Print Email

XML is fast becoming the preferred format for data interchange in todays e-business world. Leveraging this technology is something of keen interest to iSeries COBOL shops. Lets explore how COBOL applications can work with XML.

XML Considerations
A first step is to choose a location for the XML documents. On the iSeries server, the most likely source for XML documents is the IFS. Compared to the QSYS file system, the IFS space provides more flexible advantages in terms of file format and the CCSID encoding.

When parsing XML documents with COBOL, you have a couple of options. IBMs XML Toolkit for iSeries (5733-XT1), which contains the XML for procedural parser, is the obvious and simple choice. In essence, the parser provides a set of APIs for procedural languages such as COBOL and RPG to interact with the underlying XML parser.

Also, with the introduction of IBM i5/OS V5R3, an embedded XML parser is provided with ILE COBOL. This can be invoked using the newly introduced XML PARSE syntax.

At this point, iSeries users can only generate XML documents programmatically. Thus, when generating an XML document, the document content should be well determined at runtime. Users can simply generate the output document while adhering to the XML format.

Parsing XML Documents
Parsing XML documents plays a major role in enabling XML technology for COBOL applications. For further insight, lets more closely examine the two aforementioned parsing options.

  1. While the XML Toolkits XML procedural parser was (at least prior to V5R3), the primary option, invoking these APIs from within a COBOL program isnt trivial. Sets of two ILE COBOL programs are created-a main program and corresponding event-handler program. The main program initializes the XML environment, instantiates the XML parser and sets up the callback programs for the registered XML events. The event-handler program handles the business logic associated with the XML events. At application runtime, these two programs are called back and forth as events arise. For details, see the whitepaper, "Mapping data from XML documents to DB2 on iSeries using ILE COBOL." A link to this and other resources is included in "References."
  2. The V5R3 XML PARSE statement has two formats. The first is offered on the zSeries COBOL compiler. It provides XML parsing for XML documents within COBOL. The second format provides additional XML parsing support from the iSeries IFS. Both formats are designed to help COBOL programmers parse XML documents using a simpler and more intuitive coding style.

Code Sample 1, Code Sample 2 and Code Sample 3 illustrate a simple "address book" COBOL application. The application is designed to read in an XML document containing several address entries from the IFS. The XML document is then parsed and address entries are mapped to the corresponding fields as defined in DB2 Universal Database (UDB) for iSeries. Code samples 1 and 2 are the main program and the event handler program, respectively, using the XML Toolkit for iSeries. Code Sample 3 uses the V5R3 XML PARSE statement.

Summary
The coding effort and simplicity of the XML PARSE statement are evident from the examples. However, XML PARSE currently supports only Simple API for XML (SAX) parsing. For COBOL programmers interested in and Document Object Model (DOM) parsing, the APIs provided by the XML Toolkit for iSeries offer a complementary alternative.

Cindy Lee is the manager of iSeries application development tools for IBM. Cindy can be reached at cinderel@ca.ibm.com.


Advertisement



Buyers Guide

Browse products and services for Developer.



Advertisement