IBM i > DEVELOPER > RPG

XML-INTO Revisited

New RPG PTF solves level problems and repeating-element challenges


IBM recently announced two significant enhancements to RPG’s support for XML processing via the RPG Café. Using the Café for such an announcement is interesting in itself and perhaps a sign that IBM is trying new vehicles to reach its audience. However, the announcement’s content is even more interesting. Although it doesn’t provide a solution for all of the issues that we and others have encountered with XML-INTO, it certainly goes a long way toward dealing with two of the most common problems.

 

Here’s an extract from an XML document we’ll use to demonstrate the issues and how the new support addresses them:

    <?xml version="1.0" encoding="UTF-8"?>
    <CustomerData>
      <Customer>
(A)     <Name category="Retail" ID="J020">Jones and Co.</Name>
(B)     <Address type="Mailing">
          <Street>2550 Main Street</Street>
          <City>Knoxville</City>
          <State>TN</State>
        </Address>
(B)     <Address type="Shipping">
          <Street>45 Opryland Drive</Street>
          <City>Nashville</City>
          <State>TN</State>
        </Address>
      </Customer>
      <Customer>
        <Name category="Wholesale" ID="S197">Smith Brothers</Name>
        <Address type="All">
       .....

Find the full XML sample in Code Sample 1.

Problem 1: Data and Attributes at the Same Level

The Name element at (A) in the sample highlights the first problem. The attributes “category” and “ID” must be defined as subfields of the parent element “Name.” The only way we can handle this in RPG is to specify “Name” as a DS like so:

D Name         DS             Template
D   category            10a
D   ID                   4a

But when we do this, there’s nowhere to put the actual data (“Jones and Co.”). The parser can’t store it in Name since that would overwrite the category and ID fields. Faced with this problem, XML-INTO has, until now, simply ignored the data. That left us with a choice of either reformatting the XML document so that XML-INTO could handle it or resorting to XML-SAX to retrieve the parts that XML-INTO couldn’t reach. Neither solution was optimal, but with the advent of this new support we have a more direct solution.

All we must do is add the keyword datasubf=data to the %XML options list. When this is specified, if the parser encounters a situation of this type, it looks to see if there’s a subfield with the name “data” in the DS and, if so, stores the element’s data there.

If we add datasubf=data, our example can now be coded as:

D Name          DS           Template
D   category            10a
D   ID                   4a
D   data                32a

We’re not forced to use the name “data”—we can put anything we like after the equal sign and the parser will look for it. We must confess this approach wasn’t what we’d expected. We always assumed that when IBM added support it’d simply be in the form of duplicating the DS name as a subfield of the DS. In other words, the data in this case would have been stored in Name.Name. But it turns out IBM studied the code samples submitted along with the related bug reports and decided to base the support on the most common approaches users had made. Seems a good enough reason to us.

Note you may need to define multiple fields with the same base name (i.e., “data”) but this isn’t a problem since all of these fields will be in qualified data structures anyway. So, in practice, they’ll be know as X.data, Y.data, etc. In our demonstration program, the field name is Customer(c).Name.data. Find this at (C) in Code Sample 2.

 

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.


comments powered by Disqus
Buyers Guide

Advertisement

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

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

Advertisement