RPGLE XML Parser - XML-SAX and XML-INTO sample code Part 7

I am not a fan of RPGLE XML-INTO but will try to parse the XML Order.xml using the Op-Code. Here is a sample code how to do it. I find it cumbersome and its gets more tough as your XML layout complexity increases.
Click here to read from first 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 can operate in two different ways:

  • Reading XML data directly into an RPG variable
  • Reading XML data gradually into an array parameter that it passes to the procedure specified by %HANDLER(handlerProc).

The first operand specifies the target of the parsed data. It can contain a variable name or the %HANDLER built-in function.

The second operand must be the %XML built-in function, identifying the XML document to be parsed and the options controlling the way the parsing is done. See %XML (xmlDocument {:options}) for more information on %XML.

%XML (xmlDocument {:options})


%XML is used as the second operand of the XML-SAX and XML-INTO operation codes to specify the XML document to be parsed, and the options to control how the document is parsed. %XML does not return a value, and it cannot be specified anywhere other than for the XML-SAX and XML-INTO operation codes.



"Options" Parameter Options
Parameter
Options
Description
doc
doc=string
doc=file
string, the default, tells the %XML built-in function that the XMLdocument parameter contains a string or field of XML (i.e., not a file name).
file tells the %XML built-in function that the XMLdocument parameter is the name 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 will convert the XML to all uppercase when case=any is specified.
trim
trim=all
trim=none
This controls whether spaces (tabs, linefeeds, excess blanks) should be removed before assigning to variables. It applies only to character fields. Other data types always trim extra spaces.
allowmissing
allowmissing=no
allowmissing=yes
When you copy the XML values to a data structure, this option controls whether or not a node/element for each data structure subfield must exist in the XML. You probably want to set this to "yes" in most cases.
allowextra
allowextra=no
allowextra=yes
The complement of allowmissing, this option controls whether or not to allow extra nodes/elements in the XML that do not have a corresponding subfield name in the target data structure.
path
path=xmlnode/name
This is perhaps the most powerful feature of %XML. It allows you to directly retrieve an element in an XML document. If this option is not specified, the element retrieved is the same as the target field name (for standalone fields) and the names of the subfields (for data structures).

Link for Sample Order.xml used in the example 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               s             10i 0                            
D y               s             10i 0                            
D $xml_file       s           1000a   varying                    
D $options        s            100a   varying                    
D $divider        s             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) and 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) and                          
                  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  5                                                        
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  20                                                    
DSPLY  20.00                                                 
DSPLY  2                                                     
DSPLY  LG123456                                              
DSPLY  French Door Refrigerator                              
DSPLY  1                                                     
DSPLY  800.12                                


Read more about New and Improved XML-INTO
The following features are now available ...
  • Namespace support
  • case=convert
  • Counter

Recommended Reading

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.