Newsletters Subscriptions Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 60 -- September 10, 2003

More on XML Schema Validation with RPG


Hey, David:

I have a CL program that produces an XML document and stores it in the Integrated File System. Hopefully, I soon will be able to enhance the program to FTP the document from there to a business partner across the Internet. I've realized that I don't know how to validate the document against a schema in the job stream that creates the document and sends it off. Instead, I manually run the program and validate it, using a Microsoft DLL. I'm interested in what you had to say, but I'm a little unclear on a few things ["Improved XML Validation with Schemas," Midrange Programmer, OS/400 Edition, August 14 issue].

  • What are JDOM and Xerces? Are they third-party products that I have to buy? Where would I get them?

  • I only know CL and RPG, not Java. Is Qshell an aspect of Java?

  • Would I be able to run the process that you describe from a batch job running a CL program?

I am on V5R2. I'd appreciate any feedback that you could give me.

--Glenn


I am glad you decided to share your questions, Glenn. You are not the only RPG programmer wondering about RPG's ability to process XML documents. IBM does offer an XML for RPG and procedural languages (QXML4PR310) product, which is being withdrawn and replaced by the XML Toolkit for iSeries (5733XT1). These tools attempt to bridge the gap between the object-oriented Xerces C++ interface and a procedural RPG IV interface.

JDOM and Xerces are open-source and free projects maintained by volunteer programmers. Xerces was developed by IBM, then donated to the Apache Software Foundation. Xerces provides complete parsing support for XML documents and comes in C++ and Java versions.

JDOM is also an open-source program. It's meant to simplify using XML in Java. Under the covers, JDOM relies on an XML parser like Xerces.

Qshell is provided with all iSeries systems at no extra cost. It is a shell that allows you to enter Unix-like commands on the iSeries and is very similar to the command prompt that is available with Microsoft Windows. You should be able to find Qshell on the installation media that came with your system. Install Qshell by typing "GO LICPGM" at an OS/400 command prompt and selecting option 11. Put a "1" next to 5722SS1 option 30.

In addition to running commands from a command line, you can run commands in Qshell using the QSH command, which answers your third question on whether you can run an XML validation from a submitted CL program. I modified the ValidDocument Java program slightly to make it easier to run a validation from a CL program. Here are the commands you would use:

ADDENVVAR ENVVAR(CLASSPATH) +
VALUE('/jdom-b9/lib/xerces.jar:/java/xml/jdom-
b9/lib/xml-apis.jar:/jdom-b9/build/jdom.jar:/java')

QSH CMD('java ValidDocument file:///dir/file.xml file:///dir/file.xsd')

The Add Environment Variable (ADDENVVAR) command sets the CLASSPATH environment variable Java uses when searching for Java programs. The second command starts Qshell and uses the Java command to run the main method in the ValidDocument Java program.

Unfortunately, it is very difficult to get error messages from Qshell back to a CL program. You can redirect errors to an IFS file, but then you have to read that file. One way to get around this problem is to use RPG's ability to call Java programs. Here is a simple RPG program that calls the validation method in the ValidDocument java program.

DString$new       PR              O   extproc(
D                                       *JAVA:
D                                       'java.lang.String':
D                                       *CONSTRUCTOR)
D                              100A   CONST VARYING
DString$getBytes  PR           100A   extproc(
D                                       *JAVA:
D                                       'java.lang.String':
D                                       'getBytes')
DValidDocument$validate...
D                 PR              O   STATIC
D                                     EXTPROC(
D                                       *JAVA:
D                                       'ValidDocument':
D                                       'validate')
D                                     CLASS(*JAVA:'java.lang.String')
D xmlFile                         O   CLASS(*JAVA:'java.lang.String')
D                                     CONST
D SchemaFile                      O   CLASS(*JAVA:'java.lang.String')
D                                     CONST
DerrorMessage     S               O   CLASS(*JAVA:'java.lang.String')
DerrorMessageC    S             52A
 /free
   errorMessage = ValidDocument$Validate(
     String$new('file///tmp/po.xml'):
     String$new('/file///tmp/po.xsd'));
   errorMessageC = String$getBytes(errorMessage);
 /end-free
C     errorMessageC DSPLY
C                   EVAL      *INLR = *ON

Before you run this RPG program you will need to set your CLASSPATH using the ADDENVVAR command. Then call it from an OS/400 command line. If the XML document is invalid, a String object is returned containing the message. The "String$getBytes()" method converts that string to a character field.

I hope this example helps you accomplish your goals. The main area you might want to modify is how the Java program returns an error. In this case the program does not distinguish between general Java errors and schema validation errors.

Installing JDOM and Compiling ValidDocument.java

The preceding commands assume that JDOM is installed in a root directory named "/jdom-b9." To install JDOM, go to the JDOM Web site and download the latest binary version, which is jdom-b9.zip. Expand the JDOM ZIP file to a directory on your PC, then copy that directory to your iSeries system using a mapped drive, or you can use FTP. To use FTP, start an FTP session on your PC and connect to your iSeries system. Enter the following commands in the FTP session:

cd /tmp
lcd c:\dircontainingJDOM
bin
put jdom-b9.zip

Now start Qshell on your iSeries by running the QSH command from an OS/400 command line. Enter the following commands to install JDOM:

cd /
jar –xf /tmp/jdom-b9.zip

Now compile the JAR files found in the "/jdom-b9" directory. You do not have to compile the JAR files, but doing so will greatly improve performance.

SBMJOB CMD(QSH CMD('for jar in 
  $(find –L jdom-b9 -name 
  ''*.jar''); 
  do system "CRTJVAPGM CLSF(''"$jar"'') 
  OPTIMIZE(40)";done')) 
  JOB(JDOM) JOBQ(QPGMR)

The next step is to put the source file on your iSeries system and compile it.

<a href="file://Please add location of attached 
   file">ValidDocument.java</a>

You can copy it to a mapped drive, or use FTP. My example assumes you put the file into a root directory named "java." Here are the FTP statements you need to use:

cd /java
lcd c:\dircontainingValidDocument.java
put ValidDocument.java

Now use the QSH command to start Qshell and compile ValidDocument using the following commands:

export –s CLASSPATH=/jdom-b9/lib/xerces.jar:/java/xml
   /jdom-b9/lib/xml-apis.jar:/jdom-b9/build/jdom.jar

java ValidDocument.java

These steps may seem confusing, but over time I think you will find this process becomes easier and lets you take advantage of Java's strengths.

--David Morris


Sponsored By
DAMON TECHNOLOGIES

The Next Evolution of RPG is Here!

RSP (RPG Server Pages) is the best way to develop Web applications with RPG.

  • Developers use their existing RPG skills.
  • More robust than CGI with greater flexibility.
  • RSP is not just visual development. It is an application server built specifically for the iSeries.
  • Full debug capabilities.
  • Session Handling with a built in garbage collector.
  • The most cost effective solution for the iSeries.

With RSP, Web content is developed with the Ease, Speed, and Reliability of RPG.

In today's fast paced business world, there is not enough time or resources to convert RPG developers into Java developers. The logical step to bring your business critical applications to the Web is with RSP. RSP gives the developer the tools necessary to create fast and reliable Web applications.

Download your FREE copy of RSP today!

www.damontech.com


THIS ISSUE
SPONSORED BY:

Damon Technologies
Profound Logic Software


BACK ISSUES

TABLE OF
CONTENTS

More on XML Schema Validation with RPG

Alternatives to Published Tips

Reader Feedback and Insights: May I Have a Sample of That Report?


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris
Shannon O'Donnell

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.