You are currently on IBM Systems Media’s archival website. Click here to view our new website.


Using CGIDEV2 for Generating XML

IBM’s free CGIDEV2 library is the most flexible way to generate XML.

IBM’s free CGIDEV2 library is the most flexible way to generate XML.

While lecturing on the topic of RPG IV's V5R4 support for processing XML, people often say, "That is a great feature, but how can I generate XML?" There are of course a number of answers to this question, ranging from "Buy tool x" to "Write it to a work file and then use CPYTOIMPF to generate the IFS file." Of all of the "no-charge" options, our favorite, and by far the most flexible, is to use IBM's free CGIDEV2 library. Some of you may be wondering why we would be using a Web-development tool when the objective is to generate XML. If you think about it though, it's perhaps not such an odd choice. The most cursory look at examples of the two will quickly reveal the similarities. Take a quick look at the snippet of XML (Figure 1) and you'll see what we mean. Looks a lot like HTML doesn't it?

This particular XML document contains the basic details (product code, description, price, etc.) of all of the tools that a company sells. Tools are grouped into categories (hammers in this example) and there are multiple products in each category.

Both HTML and XML are examples of tag languages. In fact, both have their origins in Generalized Markup Language (GML), which was invented by IBM engineers in the late 1960s as a mechanism to try to solve the problems inherent in exchanging documents between different computer systems.

"But CGIDEV2 outputs its results to the browser - how does that help me?"

Luckily, some time ago, CGIDEV2 incorporated the ability to write its generated HTML to a file in the IFS. Mel Rothman (the Rochester developer originally responsible for CGIDEV2) developed this feature originally as a means to dynamically generate a Web page that could subsequently be served statically, for example, to build Web pages containing price lists that would be updated daily. Why use CPU power to dynamically build the page every time it is requested when you can build it once a day and display the results!

So How Does it Work?

As we discussed in our earlier article, CGIDEV2 uses a template system and a "fill in the blanks" approach to building its output stream--think DDS, the approach is very similar. If you take a look at the template (Figure 2) and compare it with the earlier XML sample, you should be able to get a good idea of how the template relates to the final XML file. (Note: This template excludes the normal XML header information in the interests of simplicity.)

There are two (hopefully) obvious differences between this template and the original XML. The first (A) is the introduction of the lines that begin with "/$" - these identify what CGIDEV2 refers to as section names. You can think of them as record format names, and they are used to specify the section(s) of the template that we wish to output at a specific time. This one (FileHeader) will be output to mark the beginning of the document.

Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters