Jon has recently been examining a couple of open-source tools for generating XML documents we thought we’d mention here. So far he’s barely scratched the surface of what they can do, but so far he's impressed. The tools in question are powerExt and the XMLi Toolkit.
Some of you may remember that way back in 2007 we wrote an article describing an approach that used CGIDEV2 ("Using CGIDEV2 for Generating XML"). That approach has stood us in good stead for many years, but there were times (e.g., when there were large numbers of optional elements and attributes) when we felt a different approach may be useful. These tools offer alternative methods of building XML that deal with those situations. The raison d'être behind these tools is very different. The XMLi Toolkit's author, Larry Ducie, developed his tooling to simplify the process of generating XML within RPG programs. powerExt's XML generation capabilities, on the other hand, are just one part of a toolset designed for producing dynamic modern RPG-based Web applications. We briefly mentioned this Web tooling in our recent Extra article on RPG UI modernization "Still Dreaming."
For basic XML creation, the approach taken by both tools is similar. API calls are used to add elements to the document and these APIs ensure the correct formatting of the XML, including making appropriate substitutions for characters such as "<" and ">.” Both build the XML document in memory and provide APIs that allow the resulting document to be written to the IFS, etc.
The following (partial) code examples both generate the same very simple XML document and write it to an IFS file.
XMLi:
xmli_openTag( 'root' );
xmli_addElement( 'Element1' : elementValue1 );
xmli_addElement( 'Element2' : elementValue2 );
xmli_closeTag( 'root' );
xmli_writeToFileWithVar( '/Partner400/XMLiTest.xml'
: char1024 : XML_ENCODING_UTF8);
powerExt:
xmlNode( 'root' );
xmlNode( 'Element1' : '' : elementValue1 );
xmlNode( 'Element2' : ' ': elementValue2 );
xmlEndNode();
echoToStmf( '/Partner400/powerExtTest.xml': 1208 );
As you can see, they’re very similar. But while both tools offer similar XML generation capabilities, XMLi goes one step further and offers a complete templating system that has offers some very impressive capabilities. It’s also designed as a plug-in style architecture and is therefore extensible to your specific needs.
XMLi Templating
The templating system is unbelievably powerful and allows for complete separation of the document layout from the RPG code that creates it. You'll have to download Larry's code samples to get a real idea of just how powerful this capability is. We'll keep our example simple and just attempt to match the creation of the XML document generated by the earlier code samples. Here's the template used:
<xmli:template xmlns:xmli="http://www.sourceforge.net/xmli" declaration="true"
ccsid="1208" format="pretty" >
<root>
<Element1><xmli:value-of select="parm[1]" /></Element1>
<Element2><xmli:value-of select="parm[2]" /></Element2>
</root>
<xmli:write-to-file path="'/Partner400/XMLiTemplateTest.xml'" />
</xmli:template>
Those of you familiar with xslt transforms will find this vaguely familiar. Note in particular the special values "parm[1]" and "parm[2.” These are supplied by the RPG code at run time. Here’s the RPG code:
xmli_loadTemplate('TEMP1' :'XMLiExamplesTemplatesExample11.xml');
xmli_setParm(1 : 'Parameter 1 Value');
xmli_setParm(2 : 'Parameter 2 Value');
xmli_runTemplate();
The interesting thing here is that, as with our CGIDEV2 approach, the template can be changed without changing the RPG code. Only if you want to add additional variables (parameters) do you need to change the RPG code; and even then it’s simply a matter of adding additional xmli_setParm() function calls. The really interesting thing is that templates can, among other things, include SQL to retrieve the data making for a truly dynamic environment. In fact, there’s even a command to allow templates to be executed without requiring any RPG code.
There's a lot to like with both of these tools and we're looking forward to spending more time working with them. In both cases the documentation is a little "thin" right now, but growing by the day and the example programs are very well documented and tell you most of what you need to know.
Thanks to Larry Ducie and Henrik Rützou for two excellent tools that can only help the IBM i community move forward.
Before we go ...
Just one last thing on the subject of community. IBM has added an IBM i-specific area to the DeveloperWorks website. There are some good links available and we're hopeful of seeing more good content in the future. Unlike the RPG Café, you don't need to sign in and so far the performance is pretty snappy--a welcome change. Check it out here.
Connect With Us: