CGIDEV2 is a versatile tool. In past newsletters, I've demonstrated how it can be used for Web development and for sending e-mail. In this article, I'll show how it's useful for creating XML documents.
CGIDEV2 works by letting you create a template that includes substitution variables and then lets you replace those variables at run-time from your RPG program.
Although it's designed to output HTML, there's nothing that prevents you from outputting XML instead. All you have to do is design your template so that it contains XML instead of HTML. Once you're done, instead of writing it to the browser with wrtsection('*fini'), you can write it to the IFS with WrtHtmlToStmf().
For example, UPS requires an XML document to be sent to them in order to track a package using their UPS Online Tools program. The following CGIDEV2 template could be used for this:
/$AccessRequest
<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
<AccessLicenseNumber>/%LicenseNo%/</AccessLicenseNumber>
<UserId>/%UserID%/</UserId>
<Password>/%Password%/</Password>
</AccessRequest>
/$TrackingRequest
<?xml version="1.0"?>
<TrackRequest xml:lang="en-US">
<Request>
<TransactionReference>
<CustomerContext>Example 1</CustomerContext>
<XpciVersion>1.0001</XpciVersion>
</TransactionReference>
<RequestAction>Track</RequestAction>
<RequestOption>activity</RequestOption>
</Request>
<TrackingNumber>/%TrackingNo%/</TrackingNumber>
</TrackRequest>
The lines that start with /$ are section delimiters. In the example above, there are two sections. They are called AccessRequest and Tracking Request. Within each section is the XML code that should be written, along with some variables. The variables are set apart with the /% and %/ characters.
To use CGIDEV2 to create an XML document from that template, you need to load the template with the getHtmlIfsMult() API, replace each variable with the updHtmlVar() API, and then write the sections out with the wrtsection() API. Once the XML document has been generated, it will be in CGIDEV2's memory. To write it from memory to disk, you use the WrtHtmlToStmf() API. This is demonstrated in the following example program:
H DFTACTGRP(*NO)
/copy hspecsbnd
/copy prototypeb
/copy usec
/free
SetNoDebug(*OFF);
getHtmlIfsMult( '/myTemplates/UpsTrack.tmpl' );
updHtmlVar('LicenseNo' : '123');
updHtmlVar('UserID' : 'Blah');
updHtmlVar('Password' : 'boogl');
wrtsection('AccessRequest');
updHtmlVar('TrackingNo': 'blah');
wrtsection('TrackingRequest');
WrtHtmlToStmf('/upsdir/TestTrack.xml': 819);
*inlr = *on;
/end-free
If you've used CGIDEV2 for HTML, you'll notice that this isn't any different, except for two things: You put XML tags in the template, and you write it to disk instead of to a browser.
You can write a section more than once if needed. That's useful if you have a list where the XML tags repeat, but different data needs to be stored in them each time.
For example, consider the following customer master file:
A UNIQUE
A R CUSTMASF
A CUSTNO 4S 0
A NAME 30A
A ADDR1 30A
A ADDR2 30A
A CITY 13A
A STATE 2A
A ZIPCODE 9S 0
A K CUSTNO
If you wanted to create an XML document containing all of the customers from this file, you could use CGIDEV2. To get started, create a template containing the XML code.
/$FileHeading
<?xml version="1.0"?>
<CustFile>
/$CustRec
<CustRec custno="/%Custno%/">
<name>/%Name%/</name>
<addrLine1>/%Address1%/</addrLine1>
<addrLine2>/%Address2%/</addrLine2>
<cityName>/%City%/</cityName>
<stateCode>/%state%/</stateCode>
<postalCode type="us-zip">/%ZipCode%/</postalCode>
</CustRec>
/$FileFooter
</CustFile>
In this example, you'll want to write the FileHeading section once at the start of the CUSTMAS file and the FileFooter section once at the end. In between, the CustRec section needs to be written for each record in the file.
H DFTACTGRP(*NO)
/copy hspecsbnd
FCUSTMAS IF E DISK
/copy prototypeb
/copy usec
/free
SetNoDebug(*OFF);
getHtmlIfsMult( '/myTemplates/CustXml.tmpl' );
wrtsection('FileHeading');
setll *start custmas;
read custmas;
dow not %eof(custmas);
updHtmlVar('Custno' : %char(CustNo) );
updHtmlVar('Name' : Name );
updHtmlVar('Address1' : Addr1 );
updHtmlVar('Address2' : Addr2 );
updHtmlVar('City' : City );
updHtmlVar('State' : State );
updHtmlVar('ZipCode' : %editw(ZipCode: ' - '));
wrtsection('CustRec');
read custmas;
enddo;
wrtsection('FileFooter');
WrtHtmlToStmf('/export/custmas.xml': 819);
*inlr = *on;
/end-free