the latest in rpg iv v5r4 features - mitec · your partner in as/400 and iseries education the...

25
Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris Jon.Paris @ Partner400.com www.Partner400.com RPG Does XML! This presentation may contain small code examples that are furnished as simple examples to provide an illustration. These examples have not been thoroughly tested under all conditions. We therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. All code examples contained herein are provided to you "as is". THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED. The authors, Jon Paris and Susan Gantner, are co-founders of Partner400, a firm specializing in customized education and mentoring services for AS/400 and iSeries developers. After individual careers with IBM, including workimg at both the Rochester and Toronto laboratories, Jon and Susan are now devoted to educating developers on techniques and technologies to extend and modernize their applications and development environments. Jon and Susan author regular technical articles for the IBM publication, IBM Systems Magazine, i5 edition, and the companion electronic newsletter, iSeries Extra. You may view articles in current and past issues and/or subscribe to the free newsletter at: http://eservercomputing.com/iseries/ . Feel free to contact the authors at: contact @ partner400.com or visit the Partner400 web site at http://www.partner400.com © Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 1-2

Upload: vukhanh

Post on 11-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Your Partner in AS/400 and iSeries Education

The Latest in RPG IVV5R4 Features

Jon ParisJon.Paris @ Partner400.comwww.Partner400.com

RPG Does XML!

This presentation may contain small code examples that are furnished as simple examples to provide an illustration. These examples have not been thoroughly tested under all conditions. We therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.All code examples contained herein are provided to you "as is". THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED.

The authors, Jon Paris and Susan Gantner, are co-founders of Partner400, a firm specializing in customized education and mentoring services for AS/400 and iSeries developers. After individual careers with IBM, including workimg at both the Rochester and Toronto laboratories, Jon and Susan are now devoted to educating developers on techniques and technologies to extend and modernize their applications and development environments.

Jon and Susan author regular technical articles for the IBM publication, IBM Systems Magazine, i5 edition, and the companion electronic newsletter, iSeries Extra. You may view articles in current and past issues and/or subscribe to the free newsletter at: http://eservercomputing.com/iseries/.

Feel free to contact the authors at: contact @ partner400.com or visit the Partner400 web site at http://www.partner400.com

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 1-2

Page 2: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Agenda

Direct support for XML parsingXML-INTO and XML-SAX

Other V5R4 RPG featuresEVAL-CORRSQL in /Free, etc.

New XML Op-codes and BIFsXML-INTO

Parses XML data directly into a field, array or DSXML-SAX

Parses XML data and notifies your program of all events encounteredStart/End of element, Element data, Attributes and Attribute data

%HANDLERIdentifies the procedure that will handle XML-SAX eventsMay also be used with XML-INTO

More on this later

%XMLIdentifies the source of the XML dataPlus processing options

Review of V5 Data Structure EnhancementsUnderstanding these is key to using the XML support

RPG IV XML V5R4 Support

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 3-4

Page 3: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

The %XML BIF is used with both XML-INTO and XML-SAXSo we will look at that first

Syntax: %XML ( xml_document : options )

xml_document - Identifies the source of the XMLBy default the XML is contained within the named variableIf option 'doc=file' is specified then the variable contains the name of the IFS file which contains the XML document

options - Controls the processing of the XML streamFormat is optionname1=value1 optionname2=value2

No space allowed between the option name and the equal signOr between the equal sign and the valueOptions can be specified in any case.

e.g. doc=file or DOC=FILE or Doc=FileWe will look at other options as we use them

Can be a literal string, a named constant or a character variable

%XML BIF Details

XML-INTO { ( E H ) } variable %XML ( ... )E extender allows you to handle errors via testing of %ErrorH extender controls half-adjust during assignment of numeric valuesvariable is the target for the operation

It can be a field, an array, a Data Structure or a Data Structure arrayIf assignment is to a numeric field, the rules of %DEC conversion etc. apply

%XML( ...) this BIF identifies the XML document It is also used to supply processing options

For example when the XML document is contained within a file in the IFS

Matching is based on namesThe hierarchy of the name(s) of the elements and attributes in the XML are matched with that of the variable parameterIf the name hierarchy does not match then "path= ... " can be used to direct the parser to the correct starting point

Basic Syntax - XML-INTO variable

XML-INTO recordCount %XML(XML_Input1: 'path=Customers case=any');

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 5-6

Page 4: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

XML Used in Examples 1, 2, and 3

<Customers>

<RecordCount> 6 </RecordCount>

<Company>Phones R Us</Company> <Company>Suchadeal Bank</Company> <Company>Rinky Dinky Toy Co.</Company> <Company>Partner400</Company> <Company>BlackHole Navigation</Company> <Company>Banker's Trust</Company>

</Customers>

This is the XML data that will be used in our initial examplesAlthough very few of the XML documents you see will be this simple!

There is a single instance of the RecordCount elementFollowed by multiple Company name elements

This data is stored in variable XML_Input1And in the IFS file /partner400/XML_Input1.xml

XML-INTO - Example 1 - Fill a VariableExtract the value of a single element

Two %XML options are usedcase=any

Deals with the fact that our XML document contains mixed-case namespath=Customers/RecordCount

Directs the XML parser to the correct starting point

// XML looks like this: // <Customers> // <RecordCount> 6 </RecordCount>

D recordCount S 5p 0

/Free

dsply ('Record Count before = ' + %Char(recordCount));

XML-INTO recordCount %XML(XML_Input1: 'path=Customers/RecordCount + case=any');

dsply ('Record Count after = ' + %Char(recordCount));

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 7-8

Page 5: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

XML-INTO - Example 2 - Fill an ArrayExtracting into an array

Note that xmlElements is a new field added to the PSDSIt contains the number of elements loaded into the array

'path= ... ' is not needed as the "shape" of the customers DS matches the XML document

i.e. <Customers><Company>...</Company><Company> ... ...

d customers DS d company 32a Dim(99)

D progStatus SDS D xmlElements 20i 0 Overlay(progStatus: 372)

d i s 10i 0

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

dsply ( %Char(xmlElements) + ' elements loaded' ); for i = 1 to xmlElements; dsply company(i); endfor;

This is the result of running the program shown on the chart above.

DSPLY 6 elements loaded DSPLY Phones R Us DSPLY Suchadeal Bank DSPLY Rinky Dinky Toy Co. DSPLY Partner400 DSPLY BlackHole Navigation DSPLY Bankers Trust

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 9-10

Page 6: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

XML-INTO - Example 2A - XML in IFSThis is the same basic example as the previous one

But it demonstrates how to handle an XML document that is in the IFSThe option 'doc=file' informs the compiler that the variable XML_Input contains the name of the XML fileNot the actual XML data as it did before

d customers DS d company 32a Dim(99)

D progStatus SDS D xmlElements 20i 0 Overlay(progStatus: 372)

d XML_Input s 256a Varying d Inz('/Partner400/XML_Input1.xml')

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

XML-INTO { ( E H ) } %HANDLER ( ... ) %XML ( ... )

Use this version when the XML data exceeds RPG limitsEither because the number of elements exceeds RPG's 32,767Or because the size of the data exceeds the 64K named DS size limit

You can also use this approach if you simply want to process the XML "in pieces"

For example one order at a time

The %HANDLER BIF Identifies the handling procedureIt will be called as each "INTO" action is completed. It can then process the data in any way that you like

XML-INTO using %HANDLER

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 11-12

Page 7: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

%HANDLER ( prototype : communication_area)The first parameter names the prototype of the handling procedure

This procedure will be called as each "INTO" action completes Handler's Parameter requirements are shown below

Return value can be set non-zero to terminate the processingFirst parameter is the communications areaSecond is the area to be filled (i.e. the -INTO area)

It must be an array - even if it only contains 1 elementThird is the count of the number of array elements filled

The communication area is any variable/array/DS that you would like to be passed to your handler

Use it to communicate between your main program and the handlerYou might want to do this because the handler subprocedure may not be in the same program as the main line code and will be called indirectly

%HANDLER BIF - XML-INTO Version

p Handler b d pi 10i 0 d commArea LikeDS(myCommArea) d company Like(companyName) Dim(4) Const d numElements 10u 0 Value

Highlighted keywords are

required

How the %Handler Process OperatesThis is a "call-back" process

C functions such as qsort and bsearch use same approach1. XML-INTO (or -SAX) starts the process by calling the XML parser2. The parser notifies the handler when it has data ready to process3. The handler processes the data and returns control to the parser

The parser returns to step 2 unless parsing is completed at which time ...4. The parser returns control to the main-line

XML-INTO (or XML-SAX)%HANDLER identifes handling routine

Passes communications area

Receives data passed to it by the parser

Plus the original communications area

Parser processes

XML document

1

3

4

2

RPG Main Line Code

RPG Handler Subprocedure

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 13-14

Page 8: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

XML-INTO - Example 3 - %HANDLERThis is similar to Example 2 but uses %Handler

This allows us to process documents which exceed RPG's limitationse.g. The Number of array elements or size maximum size of an array or DS

Each time the company array is filled the handler routine is calledIt will also be called to handle the final group of elements

We will look at the Handler procedure on the next chart

d HandleCompany PR 10i 0 d commArea 10i 0 d company 32a Dim(4) Const d numElements 10i 0 Value

d customers DS d company 32a Dim(4)

d count S 10i 0 Inz

/Free

XML-INTO %Handler( HandleCompany: count ) %XML(XML_Input1: 'path=Customers/Company case=any' );

dsply ('Processed ' + %char(count) + ' total elements');

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 15-16

Page 9: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

numElements contains the count of elements passedIt will only be less than the maximum on the final call

p HandleCompany b d pi 10i 0 d count 10i 0 d company 32a Dim(4) Const d numElements 10i 0 Value

d i s 10i 0

/Free for i = 1 to numElements; dsply company(i); endfor;

// add current count to total in Comm Area and display current count count += numElements; dsply ( %Char(numElements) + ' elements loaded on this call' );

return 0; /End-Free p HandleCompany e

XML-INTO - Example 3 - The handler

Comm Area is used to provide a total count of the elements

processed

Our sample XML contains 6 customer entries and the array we have defined handles 4 at a time. This is the result of running our example program against that XML stream. As you can see the field passed as the communications area has been used to keep a count of the total number of customers processed.

DSPLY Phones R Us DSPLY Suchadeal Bank DSPLY Rinky Dinky Toy Co. DSPLY Partner400 DSPLY 4 elements loaded on this call DSPLY BlackHole Navigation DSPLY Bankers Trust DSPLY 2 elements loaded on this call DSPLY Processed 6 total elements

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 17-18

Page 10: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

We need to quickly review Qualified data namesNested Data Structures

Before we go any further ...

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 19-20

Page 11: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

D date DS Qualified D year 4s 0 Inz(2003)D month 2s 0 Inz(01)D day 5s 0 Inz(01)

D orderDate DS LikeDS(date) Inz(*LikeDS)

Quick Review of Qualified Data Names You will need to understand this to do XML processing

Few XML documents are simple enough to map to a basic array or DS

DS Keyword QUALIFIEDReferences to fields in the DS must be qualified to the parent's nameQualification is through the use of a

In the DS below the fields are known as date.year, date.month and date.day

DS Keyword LIKEDSThe DS inherits all the fields (names and attributes) of the named DSUse of LIKEDS implicitly adds the QUALIFIED keyword

Optional

Some of the base support for DS capability extensions was added in V5R1 - this chart summarizes those features.

Once a DS is qualified, the same field name can appear in multiple Data Structures. Qualified data names are particularly useful when you want to /Copy some code containing standard Data Structures since even if any of the subfield names happen to already exist in the program, they will not cause an error.

There is also a new parameter value allowed on the INZ keyword for data structures defined with the LIKEDS keyword: INZ(*LIKEDS). This means that any initial values specified in the original data structure should be replicated in the new (LIKEDS) data structure as well. For example, if in our code sample on this chart had an initial value for the Year subfield in Date, such as INZ(2001), that initial value would ONLY be carried over to the OrderDate.Year field IF we added the keyword INZ(*LIKEDS) to the OrderDate D spec.

There is a particularly good discussion and example of this support as it relates to passing a data a structure as a prototyped parameter in the article by Hans Boldt and Barbara Morris in IBM's iSeries Magazine, May 2001 issue.

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 21-22

Page 12: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

D Address DS QUALIFIED D Street1 30a D Street2 30aD City 20a VARYINGD State 2aD Zip 5s 0D ZipPlus 4s 0

D InvoiceInfo DS QUALIFIED D MailAddr LikeDS(Address)D ShipAddr LikeDS(Address)

V5R2 Introduced Nested DSThe LIKEDS keyword can be used within a DS

Any DS that is to contain nested DS(s) must specify QUALIFIED

To reference the fields requires double qualificatione.g. InvoiceInfo.MailAddr.City or InvoiceInfo.ShipAddr.State

You can also code Data Structure arrays !!!!Say goodbye to Multiple Occurence DS

Data Structure ArraysThe DIM keyword can now be specified at the DS level

Often more useful than MODS since all levels are accessible at onceKeyword QUALIFIED is also required

Although the syntax checkers don't seem to catch this

Subscripting works in the same way as for individual fieldsAddress(5).City is the City field within the fifth elementAddress(5).Street(1) is the first Street field in the Street array

D Address DS DIM(20) QUALIFIEDD Street 30a DIM(2)D City 20a VARYINGD State 2aD ....................

C If Address(5).City = 'Rochester'C and Address(5).State = 'MN' // There's only one street that counts in Rochester MN!C Eval Address(5).Street(1) = '37th Street'C EndIf

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 23-24

Page 13: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

XML For Example 4<Customers> <RecordCount> 25 </RecordCount> <Customer type="wholesale"> <Contact>John Jones</Contact> <Company>Phones R Us</Company> <Address> <Street>5678 High Street</Street> <City>Mytown</City> <State>GA</State> <Zip>30033</Zip> </Address> </Customer> <Customer type="retail"> <Contact>Meanold Miser</Contact> <Company>Suchadeal Bank</Company> <Address> <Street>91011 Important Ave.</Street> <City>Bankville</City> <State>MN</State> <Zip>55901</Zip> </Address> </Customer></Customers>

Example 4 - Compound Structure

D customers DS Qualified D recordCount 3p 0 D customer LikeDS(customer) Dim(99)

D customer DS Qualified D type 10a D company 32a D discountCode 1a D address LikeDS(address)

D address DS D street 32a D city 24a D state 2a D zip 5s 0

The nested Data Structures below map the shape of the XMLNote that attributes of an element such as "type" are at the same level in the hierarchy as nested elements such as "Company"The recordCount is ignored in the first example

More on how this is done in a moment

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 25-26

Page 14: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Example 4 - Program Logic

XML-INTO customer %XML(XML_Input: 'path=Customers/Customer case=any + allowextra=yes allowmissing=yes');

XML-INTO customers %XML(XML_Input : 'case=any allowextra=yes + allowmissing=yes');

First version uses the "path= ... " optionThis steers the processing to the customer elementBut it will only process the first customer element in the XML

Second does not need a path option - the DS maps the XMLi.e. The DS "customers" contains the nested DS "customer"

allowextra - XML can contain elements not included in the DSThe XML element "Contact"

allowmissing - DS can include fields not present in the XMLThe field "discountCode" in the customer DS

XML-SAX"SAX" stands for Simple API for XML

But there's no such thing as a truly "Simple" example of SAX parsingSo we will simply demonstrate the basic process of identifying events

With XML-SAX the %HANDLER BIF must always be specifiedThe parameters to the Handler routine are somewhat different though

The return value and the Communications are the sameThe second parameter is a numeric value representing the event

It can be compared with RPG IV Special Words such as *XML_ATTR_NAMEThe third is a pointer to the string containing the current token

i.e. The attribute or element name, or data contentThe fourth is the length of that stringFifth contains an exception Id. when event *XML_EXCEPTION is signaled

D myHandler Pr 10I 0 D commArea LikeDS(myCommArea) D event 10I 0 VALUE D pstring * VALUE D stringLen 20I 0 VALUE D exceptionId 10I 0 VALUE

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 27-28

Page 15: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

XML-SAX EventsThese inlcude:

*XML_START_DOCUMENTIndicates that parsing has begun

*XML_START_ELEMENTThe name of the XML element that is starting

*XML_CHARSThe value of the XML element

*XML_END_ELEMENTThe name of the XML element that is ending

*XML_ATTR_NAMEThe name of the attribute

*XML_ATTR_CHARSThe value of the attribute

*XML_END_ATTRIndicates the end of the attribute

*XML_END_DOCUMENTIndicates the end of the document

XML-SAX ExampleAs noted a simple SAX example is a contradiction

So our example will simply demonstrate the basic mechanismWe will use it to simply identify and count the different elements

We will be building up an array of element namesPlus a count of the number of Start of Element and End of Element events that we encounter.The Communications Area will be used to pass this information back to our main line program

// Communications area definition d myCommArea ds d nameCount 10i 0 d elementData Dim(500) d name 64a Overlay(elementData) d startCount 10i 0 Overlay(elementData: *next) d endCount 10i 0 Overlay(elementData: *next)

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 29-30

Page 16: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

XML-SAX Processing - Data Definition

XML-SAX %Handler(MyHandler: myCommArea ) %XML( XML_Input );

for i = 1 to nameCount; dsply ('Name = ' + %subst(name(i): 1: 40) ); dsply ('Counts = ' + %char(startCount(i)) + '/' + %char(endCount(i)) endfor; ................................................................... // Handler procedure begins here

P myHandler B D PI 10I 0 D commArea LikeDS(myCommArea) D event 10I 0 VALUE D pstring * VALUE D stringLen 20I 0 VALUE D exceptionId 10I 0 VALUE

D string S 65535A Based(pString) D returnCode S 10I 0 INZ(0) D name S 256a Varying D element s 10i 0

This is the display produced by the XML-SAX program when processing the sample XML document which contains two complete Customer entries.

Note that the names are displayed at the end of the run, but appear in the sequence in which they are first encountered. A quick review of the XML will confirm this if you have any doubts.

DSPLY Starting parse DSPLY Name = Customers DSPLY Counts = 1/1 DSPLY Name = RecordCount DSPLY Counts = 1/1 DSPLY Name = Customer DSPLY Counts = 2/2 DSPLY Name = Contact DSPLY Counts = 2/2 DSPLY Name = Company DSPLY Counts = 2/2 DSPLY Name = Address DSPLY Counts = 2/2 DSPLY Name = Street DSPLY Counts = 2/2 DSPLY Name = City DSPLY Counts = 2/2 DSPLY Name = State DSPLY Counts = 2/2 DSPLY Name = Zip DSPLY Counts = 2/2

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 31-32

Page 17: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

XML-SAX Processing - Handler Logic

/free select; // Initialize communications area on start of document when event = *XML_START_DOCUMENT; clear commArea; // If this is an attribute name then add it to the list // if we haven't seen it already when event = *XML_START_ELEMENT or event = *XML_END_ELEMENT; name = %subst(string : 1 : stringLen); element = %lookup( name: commArea.name: 1: commArea.nameCount ); if element = 0; commArea.nameCount += 1; element = commArea.nameCount; commArea.name( element ) = name; endIf; if event = *XML_START_ELEMENT; commArea.startCount(element) += 1; else; commArea.endCount(element) += 1; EndIf; endsl; return returnCode;

Can't Wait for V5R4 ?Write your own parser in RPG

Cumbersome, but may be fun!

Invoke Java Parser Methods from RPG using V5R1 supportSeveral samples out on the web

There are some non-IBM C and C++ based parsers availableScott Klement iSeries port of the Expat C parserSee ScottKlement.com for more detailsSee Appendix for details of third party options

IBM's XML Toolkit for RPGProvides API interfaces to IBM's C++ parser

RPG APIs from the RPG-XML Suitewww.RPG-XML.com

V5R3: SAX Parser built into the ILE COBOL compilerV5R4: XML Generation - Coming to RPG in V5R5 ???

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 33-34

Page 18: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Other V5R4 features

EVAL-CORRA great way to avoid typing

/Free SQL SupportSyntax Checking

Other Enhancements:Null Indicator SupportPrefix

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 35-36

Page 19: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Copies corresponding fields from one DS to anotherPrimarily based on matching of field names

Secondary match is on base data type (e.g. Character, Numeric, Date)Sequence of elements in DS is not significantWhen the correspondence is not perfect normal EVAL rules apply

Compile listing identifies what will happen to each subfieldExtenders such as half-adjust may be specified

But only in /Free format - there is not enough room in fixed format opcode!!

EVAL CORR (Evaluate Corresponding)

D ds1 ds qualifiedD num 10i 0D extra dD char 20a varyingD otherfld 1a

D ds2 ds qualifiedD char 20aD otherfld 5p 0D num 15p 5

EVAL-CORR ds2 = ds1;

A d d i t i o n a l D i a g n o s t i c M e s s a g e s

Msg id Sv Statement Message text *RNF7350 00 000014 EVAL-CORR DS2 = DS1 ignores some subfields. See "EVAL-CORR summary 1" below.

............................................

E V A L - C O R R S u m m a r y

EVAL-CORR summary 1 000014 *RNF7341 EXTRAINDS2 In target only. *RNF7349 OTHERFLD Not same data type in source and target *RNF7342 EXTRAINDS1 In source only.

Which Fields Correspond ?With the default option of *NOXREF the compiler will only report on those fields which will not be copied

See below for examples

Specify OPTION(*XREF) to get additional informationSee next chart for examples

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 37-38

Page 20: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

A d d i t i o n a l D i a g n o s t i c M e s s a g e s

Msg id Sv Statement Message text *RNF7350 00 000014 EVAL-CORR DS2 = DS1 ignores some subfields. See "EVAL-CORR summary 1" below.

............................................

E V A L - C O R R S u m m a r y

EVAL-CORR summary 1 000014 *RNF7341 EXTRAINDS2 In target only. CHAR Assigned; target and source are compatible *RNF7349 OTHERFLD Not same data type in source and target NUM Assigned; target and source are compatible *RNF7342 EXTRAINDS1 In source only.

OPTION(*XREF) ListingInformation is shown to identify those fields that correspond

This is in addition to the *NOXREF information

Warnings will be shown if overflow may occurOr if field overlaps may affect the result

Syntax Checking in /FreeNot strictly an RPG IV enhancement but …

WDSC and CODE now syntax check /Free-form RPGSEU can do it too but who wants to use SEU <grin>

The syntax checker will warn you if you "forget" a semi-colonThis can be a little annoying when entering a multi-line calculation

Message remains highlighted until the semi-colon is entered

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 39-40

Page 21: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

SQL statements may now be coded directly in /Free formatNo End-Exec required

No longer requires C spec entriesi.e. C/Exec SQL, C+ and C/End-Exec

The keywords EXEC and SQL must be on the same line.

End the complete SQL statement with a semi colon

/Free EmpNbr = '00010'; Exec SQL Select LastName, WorkDept InTo :Name, :Deptno From Employee Where Empno = :EmpNbr; /End-Free

Embedded SQL in /Free

Every SQL statement requires an EXEC SQLEven if there are multiple consecutive SQL statements

/Free

Exec SQL Declare EmpCursor Cursor For Select EmpNo, LastName, Salary From Employee Where WorkDept = :Dept;

Exec SQL Open EmpCursor;

Exec SQL Fetch Next From EmpCursor InTo :EmpNbr,:LastName,:Salary;

/End-Free

Embedded SQL in /Free - contd.

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 41-42

Page 22: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Prior to V5R4 prefix could only:Add prefix characters to the start of externally defined field names

Optionally replacing starting characters

It can now remove leading characters from external namesSimply specify a null string as the replacement value

i.e. Two consecutive single quotesWorks on both F and D specs

Useful when externally defined fields already have a prefix And you want to get rid of it

Useful in combination with EVAL-CORR (more on next chart)

// Remove first 2 characters of field names in File1 and File2 FFile1 IF E Dusk Prefix('':2)FFile2 IF E Dusk Prefix('':2)

D ExtDefine E DS ExtName(MyFile)D Prefix('':2)

Extensions to Prefix Support

Keyword Resulting Field Name

PREFIX(PT) PTCMCUST Add prefix 'PT'

PREFIX(PT:1) PTMCUST Replace the first character by the characters 'PT'

PREFIX(PT:2) PTCUST Replace the first two characters by the characters 'PT'

PREFIX('':2) CUST Remove the first two characters of the name

This table shows how the externally defined field name CMCUST would be renamed by the different permutations of PREFIX shown.

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 43-44

Page 23: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Combine EVAL-CORR with the PREFIX enhancement Provides an easy means of moving data between two files

As long as the fields have common names except for the prefix

Files CUSTOMER and HISTORY share common field namesPrefix('': 2) strip the current two character prefixQualified allows us to diferentiate the fields in the two DS

e.g. DsCustomer.Name and DsHistory.NameEVAL-CORR will populate the DsHistory fields

D DsCustomer E Ds ExtName(Customer)D QualifiedD Prefix('':2)

D DsHistory E Ds Extname(History)D QualifiedD Prefix('':2)

/Free Eval-Corr DsHistory = DsCustomer;

EVAL-CORR - Externally Described DS

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 45-46

Page 24: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Parameter option *NULLINDIndicates that the null indicator(s) are passed along with the parameter

D AProgram PID Data LikeDs(EmployeeData) D Options(*nullind)

/Free ........ empData.salary = 50000; %NullInd(empData.salary) = *Off; ........ Return; /End-Free

D EmployeeData E Ds ExtName(Employee)

D AProgram PR ExtPgm(‘MYPGM') D Data LikeDs(EmployeeData) D Options(*nullind)

Additional Null Indicator Support

As the use of SQL becomes more prevalent in System i shops, it becomes increasingly likely that you will encounter situations where the database record being handled by an RPG program will have null capable fields. Part of the reason for this is that DDS and DDL (SQL) have different defaults for the null capable status of a field (column). The example below illustrates this. In the DDS example the field EMPID is not null capable by default - notice however that in the DDL version we had to specify NOT NULL to achieve this. Similarly in order to make SALARY null capable in the DDS version requires that we specify ALWNULL, whereas the DDL version simply specifies the field name and definition.

A UNIQUE A R EMPLOYEER A EMPID 7A A NAME 30A A GRADE 2A A BIRTHDATE L ALWNULL A JOINEDDATE L ALWNULL A SALARY 13P 2 ALWNULL A K EMPID

CREATE TABLE EMPLOYEE ( EMPID CHAR(7) NOT NULL, NAME CHAR(30) NOT NULL, GRADE CHAR(2) NOT NULL, BIRTHDATE DATE, JOINEDDATE DATE, SALARY DECIMAL(13, 2), PRIMARY KEY( EMPID ) ) ;

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 47-48

Page 25: The Latest in RPG IV V5R4 Features - MITEC · Your Partner in AS/400 and iSeries Education The Latest in RPG IV V5R4 Features Jon Paris ... the IFS file which contains the XML document

Questions ?

© Partner400, 2006 RPG IV - V5R4 - RPG Does XML - V5R4 Language Features - Page: 49-50