RPGLE XML Parser – XML-SAX together with XML-INTO sample code Part 7

I am non a fan of RPGLE XML-INTO but volition endeavor to parse the XML Order.xml using the Op-Code. Here is a sample code how to produce it. I notice it cumbersome in addition to its gets to a greater extent than tough every bit your XML layout complexity increases.
Click hither to read from outset Chapter

XML-INTO (Parse an XML Document into a Variable)

Free-Form Syntax XML-INTO{(EH)} receiver %XML(xmlDoc {: options });
XML-INTO{(EH)} %HANDLER(handlerProc : commArea) %XML(xmlDoc {: options });

XML-INTO tin give the sack operate inwards 2 dissimilar ways:

  • Reading XML information conduct into an RPG variable
  • Reading XML information gradually into an array parameter that it passes to the physical care for specified yesteryear %HANDLER(handlerProc).

The outset operand specifies the target of the parsed data. It tin give the sack incorporate a variable elevate or the %HANDLER built-in function.

The instant operand must endure the %XML built-in function, identifying the XML document to endure parsed in addition to the options controlling the means the parsing is done. See %XML (xmlDocument {:options}) for to a greater extent than information on %XML.

%XML (xmlDocument {:options})

%XML is used every bit the instant operand of the XML-SAX in addition to XML-INTO performance codes to specify the XML document to endure parsed, in addition to the options to command how the document is parsed. %XML does non provide a value, in addition to it cannot endure specified anywhere other than for the XML-SAX in addition to XML-INTO performance codes.

“Options” Parameter Options
Parameter
Options
Description
doc
doc=string
doc=file
string, the default, tells the %XML built-in constituent that the XMLdocument parameter contains a string or plain of XML (i.e., non a file name).
file tells the %XML built-in constituent that the XMLdocument parameter is the elevate of an IFS file that contains the XML.
case
case=lower
case=upper
case=any
This specifies how the XML is stored. Specifying case=lower or case=upper is faster than case=any because the parser volition convert the XML to all working capital missive of the alphabet when case=any is specified.
trim
trim=all
trim=none
This controls whether spaces (tabs, linefeeds, excess blanks) should endure removed earlier assigning to variables. It applies merely to grapheme fields. Other information types ever trim back extra spaces.
allowmissing
allowmissing=no
allowmissing=yes
When you lot re-create the XML values to a information structure, this choice controls whether or non a node/element for each information construction subfield must be inwards the XML. You likely desire to prepare this to “yes” inwards most cases.
allowextra
allowextra=no
allowextra=yes
The complement of allowmissing, this choice controls whether or non to allow extra nodes/elements inwards the XML that produce non convey a corresponding subfield elevate inwards the target information structure.
path
path=xmlnode/name
This is possibly the most powerful characteristic of %XML. It allows you lot to conduct cry back an chemical ingredient inwards an XML document. If this choice is non specified, the chemical ingredient retrieved is the same every bit the target plain elevate (for standalone fields) in addition to the names of the subfields (for information structures).

Link for Sample Order.xml used inwards the illustration code below

Sample Code using RPGLE XML-INTO

h option(*nodebugio) dftactgrp(*no)                                                                                                 D Order           ds                  qualified                   D                                     dim(100)                    D   OrderID                      9a                               D   OrderLine                         likeds(OrderLine_t)         D                                     dim(100)                                                                                      D OrderLine_t     DS                  qualified                   D                                     based(Template)             D   OrderlineID                  4a                               D   ItemID                      20a                               D   ItemDescription...                                            D                               30a                               D   Quantity                     8a                               D   Price                       11a                                                                                                 D x               sec             10i 0                             D y               sec             10i 0                             D $xml_file       sec           1000a   varying                     D $options        sec            100a   varying                     D $divider        sec             52a                                 /free                                                                        $xml_file = '/MyXML/data/Order.xml';                                   $options  = 'doc=file +                                                             path=Orders/Order +                                                    case=any +                                                             allowextra=yes +                                                       allowmissing=yes';                                                                                                               xml-into Order %xml($xml_file: $options);                                                                                                     x = 1;                                                                                                                                          dow x <= %elem(Order) in addition to Order(x).OrderID<>*blanks;                                                                                             $divider = *ALL'-';                                                    //Display Order Number                                                 $divider = 'Order# ' + %trim(Order(x).OrderID) + $divider;             dsply $divider;                                                                                                                          y = 1;                                                                   dow y <= %elem(Order.OrderLine) in addition to                                             Order(x).OrderLine(y).OrderlineID <> *blanks;                    //Display Order Line No.                                               dsply    Order(x).OrderLine(y).OrderlineID;                  //Display Item Number                                        dsply    Order(x).OrderLine(y).ItemID;                       //Display Item Description                                   dsply    Order(x).OrderLine(y).ItemDescription;              //Display Item Quantity                                      dsply    Order(x).OrderLine(y).Quantity;                     //Display Item Price                                         dsply    Order(x).OrderLine(y).Price;                                                                                     y = y + 1;                                                enddo;                                                                                                                       x = x + 1;                                                                                                             enddo;                                                                                                                                                                               *inlr = *on;                                                                                                        /end-free                                            

Here is the Output for this program

DSPLY  Order# 101------------------------------------------      DSPLY  1                                                         DSPLY  MCP-X2                                                    DSPLY  Cuisinart MultiClad Pro                                   DSPLY  v                                                         DSPLY  10.00                                                     DSPLY  2                                                         DSPLY  DC25-X                                                    DSPLY  Dyson Vacuum Cleaner                                      DSPLY  1                                                  DSPLY  49.99                                                  DSPLY  Order# 102------------------------------------------   DSPLY  1                                                      DSPLY  MMM-X123                                               DSPLY  Houdini Hand Blown Crystal                             DSPLY  xx                                                     DSPLY  20.00                                                  DSPLY  2                                                      DSPLY  LG123456                                               DSPLY  French Door Refrigerator                               DSPLY  1                                                      DSPLY  800.12                                

Read to a greater extent than well-nigh New in addition to Improved XML-INTO
The next features are straight off available …

  • Namespace support
  • case=convert
  • Counter

Recommended Reading

Hosting Unlimited Indonesia


Sumber http://www.mysamplecode.com/