With the advent of V5R4, RPG IV can be used to do basic parsing of XML natively. The new XML-INTO opcode, while not strictly intuitive, can easily be used to extract pieces of data from XML.
So where do you get XML? One place is from Web services. But just what is a Web service? Before we look at that, let's review a primary candidate for a Web services routine: shipment tracking.
Let's assume you need to get the tracking information for a package you've shipped. You go to the shipper's Web site and enter the tracking number, and the package delivery status is displayed. This is accomplished though the Web site via a CGI script (or program) that generates the tracking information and writes it out to a Web page.
What if you could get that tracking information into your RPG IV program? That way, you could give your customer service people the ability to report to your customers the current location of their orders.
Web services can be anything, but fundamentally they are CGI programs that return data in a known format. Rather than behaving like typical CGI programs and returning data formatted with HTML, Web services routines return data in other formats. Typically, this means XML, but it can be any defined format.
Accessing a Web services routine with RPG IV can be complicated. Consider that when you're online via a Web browser, the browser reads and renders CGI program responses, and simulating a browser in RPG IV, for example, can be challenging.
To call Web service routines, you need to use either a service program of routines that makes doing HTTP calls easier or TCP/IP and sockets programming APIs.
The free iSockets service program allows you to easily call CGI and Web services from within RPG IV. In fact, it was created specifically for this purpose.
The iSockets routines that simplify calls to Web Services are PostURLData and GetURLData. The PostURLData procedure uses the CGI POST method to send the request, whereas the GetURLData procedure uses the CGI GET method to send the request. Other than that, they are similar. Most Web services require you to use POST or GET and fail if you use the wrong one. So make sure you know which method your Web service requires.
To illustrate a call to a Web service routine, I found a Web service that converts Celsius to Fahrenheit. You pass in degrees Celsius, and it returns degrees Fahrenheit. For example, if you send it 36 degrees Celsius, it'll return the following data:
If you look at the above XML, you'll notice that buried in that mess is the number 97, which is the converted degrees in Fahrenheit.
The element named "short" identifies the converted value. The namespace is not really relevant to anything, so we can ignore it.
To retrieve this information into our own RPG IV program, we need to call the iSockets PostURLData procedure and specify the Web services program. Here's an example:
D szDomain S 128A Varying
D Inz('www.w3schools.com')
D WebService S 128A Varying
D Inz('/webservices/tempconvert.asmx+
D /CelsiusToFahrenheit')
D szFormData S 64A Varying
D Inz('Celsius=')
D nCelsius S 10I 0 Inz(36)
D szHtml S 4096A
C eval szFormData = szFormData + %char(nCelsius)
C eval nBytes=PostUrldata(szDomain:
C szWebService : szFormData :
C %addr(szHtml) : %size(szHtml))
The above program calls a Web services program on www.w3schools.com and passes 'Celsius=36' to the Web services program. Upon completion of the process, the field named szHtml contains the response data from the Web services program. This includes any HTTP response headers.
Normally, HTTP response headers are discarded once you verify that you received the proper response. (If something else is returned, you have to decide what you want to do about it.) iSockets includes the ParseHttpReply procedure to detect the HTTP response header and return the location of the actual data you need to use in your RPG IV program.
The following code adds this capability to the previous example:
D nDataPos S 10I 0
D nLen S 10I 0
D nCelsius S 10I 0 Inz(36)
D nFahren S 10I 0
C if nBytes > 0
C eval nDataPos = ParseHTTPReply(pHTML:nBytes)
C eval nLen = nBytes-(nDataPos-1)
C callp Joblog(%subst(szHtml:nDataPos:nLen))
C endif
** If compiling on V5R4, then we can use XML-INTO to parse
** the XML response.
/IF DEFINED(*V5R4M0)
C XML-INTO nFahren %xml(%subst(szHtml:nDataPos: nLen):
C 'path=short allowextra=yes')
C callp joblog('%s Fahrenheit is %s Celsius':
C %char(nFahren) : %char(nCelsius))
/ENDIF
Upon completion of the XML-INTO opcode, the nFahren variable contains the value 97, which is extracted from the returned XML. Note the 'path=short' option on the %XML built-in function of the XML-INTO opcode. This option indicates which XML element should be extracted. Since this Web server returns the
This is just a taste of how RPG IV can use iSockets to easily access Web services and process the results using XML-INTO.
Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.
LATEST COMMENTS
MC Press Online