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

IBM i > DEVELOPER > RPG

More on RPG's XML Support

Jon Paris and Susan Gantner address a reader’s question about processing XML with RPG.

Jon Paris and Susan Gantner address a reader’s question about processing XML with RPG.

In the article “A Traditional Approach to a Modern Technology,” we discussed the basics of processing XML with RPG. We want to delve a little deeper now that V5R4 is more widely available. During our thought process, we received an e-mail from a reader who used our examples but had an XML document that didn’t fit the basic models we described. Since the question is one that often comes up, we thought we’d cover it and related topics this month.

Mapping XML Attributes to RPG Data Structures

Before we discuss our reader’s question, a brief review of the basics is in order. In our basic examples, we demonstrated the parsing of a simple document similar to this:

<Customers>
     <Company>Phones R Us</Company>
     <Company>Company 2</Company>
     <Company>Last One</Company>
...

Both the data definition and processing is quite straightforward and can essentially be handled by the following RPG code:

d customers     DS
d   company         32a Dim(999)

/Free
    XML-INTO company %XML( XML_Input : 'case=any');

When we’re able to use this approach, we can even take advantage of the fact that the XML parser will supply a count of the number of company elements found and place it in xmlElements, an 8-byte integer (20i) at offset 372 in the program status data structure (PSDS).

So far so good, but the question our reader posed was how to represent, in RPG terms, an XML document that looks like this:

<Customers>
     <OtherInfo Location="25" />
     <Company>Phones R Us</Company>
     <Company>Company 2</Company>
     <Company>Last One</Company>
    …

The concern is how to handle the Location attribute of the OtherInfo element. The simple answer is that an attribute of an element is considered to be at the same hierarchical level as a child of that element. In other words, this:

<OtherInfo Location="25" />

And this:

<OtherInfo>
   <Location>25</Location>
</OtherInfo>

are effectively the same thing and therefore we code for the first version the same way as if the XML looked like the second version. Look at the code sample below and you’ll see what we mean. otherInfo is coded as a nested data structure using the LikeDS keyword, and the location attribute is coded as a subfield of that DS.

d customers      DS                 qualified
d   otherInfo              LikeDS(otherInfo)
d   company             32a Dim(999)

d otherInfo     DS
d location             2a

  /Free
    XML-INTO customers %XML( XML_Input : 'case=any');

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.



Advertisement

Advertisement

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