Blog Archive

Generate XML using RPGLE with help of C language API’ s

The Extensible Markup Language, or XML for short, is a new technology for Web applications that has the official recommendation of the World Wide Web Consortium (W3C). XML is a descendant of the Standard Generalized Markup Language (SGML), a markup standard created by former IBMer Dr. Charles Goldfarb, that lets you create your own tags.

What is XML?

  • XML stands for EXtensible Markup Language
  • XML is a markup language much like HTML
  • XML was designed to carry data, not to display data
  • XML tags are not predefined. You must define your own tags
  • XML is designed to be self-descriptive
  • XML is a W3C Recommendation
Why do we need XML?
When people first hear about XML, they often ask why we need another markup language. Everybody's browser supports HTML today, so why create more tags? Given that lots of HTML tags haven't been implemented the same way by the big browser vendors, why let anybody and everybody create their own tags? The answer to these questions is that HTML and XML serve different functions: HTML tags describe how to render things on the screen, while XML tags describe what things are. Put another way, HTML tags are designed for the interaction between humans and computers; XML tags are designed for the interaction between two computers.

How XML will change the Web

Enable universal data
If you look at the Web today, you'll find several universal technologies, including TCP/IP, HTML, and Java.
  • TCP/IP is the universal connectivity protocol; everything from mainframes to laptops to cellular phones can connect to the Web using it.
  • HTML is the universal rendering language. Although not all browsers support all functions, there is a core set of HTML tags that can be rendered on any browser.
  • Finally, Java's promise of "write once, run anywhere" makes supporting the wide variety of devices on the Web much easier.
Because of these ubiquitous technologies, it's relatively straightforward to create a Web application that runs on any platform. XML completes the picture by enabling universal data. You can build an XML document that describes a data structure, and that structured data can be sent anywhere across the Web. XML will change the Web because of its power and flexibility as a data interchange format.

Enable business-to-business communication
One of the challenges in conducting e-business is communicating with other organizations, whether they are partners, suppliers, competitors, or even other groups within the same company. XML simplifies business-to-business communication because the only thing that any two organizations have to agree on is the XML tag set that will be used to represent data. Neither organization has to know how the other's back-end systems are organized. If my systems run OS/390 and your systems run Linux, that doesn't matter. If my databases are relational and yours are object-oriented, that doesn't matter. If my code was written in C++ and yours was written in Java, that doesn't matter. The only thing that's important is that we agree on a standard set of tags for data interchange.
Once we've agreed on a tag set, each of us can write the mapping code to transform XML documents into whatever format we need to work with our back-end systems. For example, an XML document that's received from a partner might be parsed, then converted into a transaction that drives some business process on my system. Even better, if another company joins our consortium, we don't have to write more code to interact with the systems of the new company. We simply require that company to follow the document rules we defined in our XML tag set.

Enable smart agents
When writing an agent, one of the challenges is to make sense of incoming data. A good agent interprets information intelligently, then responds to it accordingly. If the data sent to an agent is structured with XML, it's much easier for the agent to understand exactly what the data means and how it relates to other pieces of data it may already know. As we illustrated in our sample HTML document, writing code to interpret the data contained in HTML tags is difficult and error prone. With XML, the structure of the data is easily determined and manipulated.

Enable smart searches
A major problem with today's Web is that search engines can't process HTML intelligently. For example, if you're searching for someone named Chip, you might get pages for chocolate chip cookies, RAM chips, poker chips, and guys named Chip. On the other hand, if you were searching for documents that contained a tag with a value of "Chip," you would get much better results. Being able to limit searches to those XML documents that use a certain set of tags would allow you to weed out a massive amount of unrelated content.
As an aside, being able to limit search results to documents that use a particular tag set is one of the market forces that will drive the acceptance of XML. Say that a group of automobile dealers defines a tag set for used cars, and that several popular search engines promise great results because their search engines look only at XML documents using those tags. If you're an auto dealer, you can either join the market and support that tag set or be left out of the market completely. If your inventory is not described using the standard XML markup, would-be car buyers using an XML search engine will never find you.

Generate XML using RPGLE

Now that we know how useful is XML becoming in out day to day life. Let see how you can easily write them using iSeries Native Language RPGLE. There are various ways we can generate XML using RPGLE.

  • Write XML data into Physical file or database table and then copy the data to IFS.
  • Write data using CGIDEV2
  • Write data directly into IFS using C language API's
I am going to explain the last option writing directly into IFS using the C language API here. I am not very fond of the first method. The third method is really good it uses the CGIDEV2 utility which is available for free. CGIDEV2 was basically developed for generating HTML using RPGLE but now you can use it also to generate XML data. It uses a template and then the program maps the data over to the template to generate the final output XML. It needs installing the tool and also understanding the tools keywords , operators and custom tags.

Reference the C language binding directory so that we can use the API's
h dftactgrp(*no) bnddir('QC2LE')

Use the Copy source for IFSIO_H (Integrated File System I/O Header Member) to define prototypes, constants, etc. used in these C funtions API's.
d/copy *libl/qrpglesrc,IFSIO_H

Define file handler and some variables to be used in the program
d fd              s             10I 0
d crlf            c                   x'0D25'
d $xml            s            512a
d @CmdStr         s            512a   inz
d @Apostr         s              1a   inz(X'7D')

Create the XML file
The most important step here is to Open the file then close it and then open it again to write data into that. This is so that the CCSID is correct before you start writing data otherwise your data will be messed up.

        fd = open(%trim($file_path)
                  : O_WRONLY+O_CREAT+O_TRUNC+O_CCSID
                  : S_IRGRP + S_IWGRP + S_IXGRP +
                     S_IRUSR + S_IWUSR + S_IXUSR
                  : 819);
        callp close(fd);
        fd = open(%trim($file_path):O_WRONLY+O_TEXTDATA);

        $xml = '<?xml version="1.0" encoding="UTF-8"?>' + crlf +
               '<Orders>' + crlf;

        callp write(fd: %addr($xml): %len(%trim($xml)));


Start writing XML data elements

        $xml = '<CustPartyID>' +
                 %trim(x0cpid) +
               '</CustPartyID>' + crlf;
        callp write(fd: %addr($xml): %len(%trim($xml)));

        $xml = '<PreferredDeliveryDate>' +
                 %trim($ddmmyyyy) +
               '</PreferredDeliveryDate>' + crlf;
        callp write(fd: %addr($xml): %len(%trim($xml)));

        $xml = '<CustPartyOrderID>' +
                 %trim(%editc(x1po:'Z')) +
               '</CustPartyOrderID>' + crlf;
        callp write(fd: %addr($xml): %len(%trim($xml)));

        $xml = '<CustPartyReplyEmailAddress>' +
                 %trim(x0emal) +
               '</CustPartyReplyEmailAddress>' + crlf;
        callp write(fd: %addr($xml): %len(%trim($xml)));


Close XML file

        $xml = '</Orders>' + crlf;
        callp write(fd: %addr($xml): %len(%trim($xml)));
        callp close(fd);


MC Dug-T at developerWorks
Scott Klement

Click here for next Chapter

Recommended Reading