Disoriented by SAX vs. DOM? Baffled by XML APIs? You are not alone. Writing RPGLE code to parse XML documents can be a tedious task. There is, however, a (free) utility called Xalan that will immensely ease your job, and let concentrate on the business rather than the bytes.
Xalan is a Java XSL parser, that will take as input 2 XML files:
The first is the XML we want to transform,
and the second is a XML file that defines the transformation.
The transformation can output another XML, an HTML file, a PDF file (using special extensions).
Of special interest to iSeries professionals is the fact that a transformation can output
a regular, flat text file, that is suited for import into a DB2 table.
You can find many examples of XSL transformations on the Web,
but here is a real world example that is relevant in our context.
The Bank of Israel publishes a daily XML file with the currency rates.
With the attached code, you can find a sample of this file,
and the XSL to transform it into a Comma Separated Values text file.
The result file can be imported into a DB2 table using CPYFRMIMPF command.
How to get Xalan to your iSeries computer?
-----------------------------------------------------------------
If you have VRR1, and have Java, then chances are that it is already installed.
Type WRKLNK OBJ('/QIBM/ProdData/OS400/xml/lib/*')
If you see the files xalan101.jar and xerces103.jar then you are already set up.
If you do not have the files, you can get them at here.
Note: This is an OLD version of Xalan. New version are available, but have not been tried by me, yet.
If you decide to use a newer Xalan distribution, pay attention to the CLASSPATH variable.
Create a new IFS directory called XML:
MKDIR '/xml'
Transfer the ZIP file via FTP to the XML directory.
Run the following command (it's
To continue reading for free, register below or login
To read more you must become a member of Search400.com
');
// -->

best to submit to batch):
QSH CMD('cd /xml; ajar -x -r xalan-j_1_0_1.zip')
About the attached code:
=================================================================
The CL program takes 5 parameters:
XML - The file to be translated.
XSL - The transformation description.
OUT - The result file.
CSSID - The code page of the result file.
VER - The AS/400 version. If *PRV, the script assumes that you installed Xalan manually according to the above listed instructions. Use *PRV in case you did not find the xalan101.jar file,
even if you have 5.1 or higher.
XALAN *cmd:
=================================================================
/* CRTCMD CMD(XALAN) PGM(*LIBL/XALANCPP) ... */
CMD PROMPT('Translate XML using XSLT')
PARM KWD(XML) TYPE(*PNAME) LEN(256) MIN(1) +
CASE(*MIXED) PROMPT('Full XML file path')
PARM KWD(XSL) TYPE(*PNAME) LEN(256) MIN(1) +
CASE(*MIXED) PROMPT('Full XSL file path')
PARM KWD(out) TYPE(*PNAME) LEN(256) MIN(1) +
CASE(*MIXED) PROMPT('Full Result file path')
PARM KWD(CCSID) TYPE(*CHAR) LEN(7) PROMPT('CCSID +
of result file')
PARM KWD(VER) TYPE(*CHAR) LEN(7) RSTD(*YES) +
DFT(V5R1M0) VALUES(V5R1M0 V5R2M0 *PRV) +
PROMPT('OS400 release')
/* End of Command XALAN */
XALANCPP *PGM:
=============================================================
PGM PARM(&XML &XSL &OUT &CCSID &VER)
DCL VAR(&JAVA) TYPE(*CHAR) LEN(1024)
DCL VAR(&qshell) TYPE(*CHAR) LEN(1024)
DCL VAR(&XML) TYPE(*CHAR) LEN(256)
DCL VAR(&XSL) TYPE(*CHAR) LEN(256)
DCL VAR(&OUT) TYPE(*CHAR) LEN(256)
DCL VAR(&CCSID) TYPE(*CHAR) LEN(7)
DCL VAR(&VER) TYPE(*CHAR) LEN(6)
/* Prepare java call */
IF COND(&VER = '*PRV') THEN(DO)
CHGVAR VAR(&JAVA) VALUE('java -classpath +
/xml/xalan_1_0_1/xalan.jar:/xml/xalan_1_0_1+
/xerces.jar:. +
org.apache.xalan.xslt.Process ' *BCAT +
'-IN ' *BCAT &XML *BCAT ' -XSL ' *BCAT +
&XSL *BCAT ' -OUT ' *BCAT &OUT *BCAT ' +
-TEXT')
enddo
ELSE CMD(DO)
CHGVAR VAR(&JAVA) VALUE('java -classpath +
/QIBM/ProdData/OS400/xml/lib/xalan101.jar:/+
QIBM/ProdData/OS400/xml/lib/xerces103.jar:. +
org.apache.xalan.xslt.Process ' *BCAT +
'-IN ' *BCAT &xml *BCAT ' -XSL ' *BCAT +
&xsl *BCAT ' -OUT ' *BCAT &out *BCAT ' -TEXT'
enddo
STRQSH CMD(&JAVA)
/* Change CCSID of OUT file
CHGVAR VAR(&QSHELL) VALUE('setccsid' *bcat &ccsid *BCAT &out)
STRQSH CMD(&qshell)
RETURN
EXIT: ENDPGM
/* End of Program XALANCPP */
Sample XML to translate:
Note: The encoding in the original XML file is wrong.
==============================================================