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

Click here to read from first Chapter

Generate Parser Cross-Ref File for use in Parsing the XML data

h option(*nodebugio) dftactgrp(*no)      
fvxmlxrf   if a e           k disk                                   
fvxmlxrf1  if   e           k disk    rename(xmlxrfr:record1)        
f                                     prefix(#)                      
 /copy *libl/qrpglesrc,parser_h                                      
d $counter        s              5s 0 inz(0)                         
 * Main Routine                                                      
c     *entry        plist                                            
c                   parm                    fileId            
c                   parm                    filePath          
       XML = %trim(filePath);                                 
       xml-sax %handler(mySaxHandler: ignoreMe)               
               %XML(XML: 'doc=file');                         
       *inlr = *on;                                           
 /copy *libl/qrpglesrc,parser                                 
 * Data Manipulation                                          
P myDataHandler   B                                           
d myDataHandler   PI            10i 0                         
d stackname                  65535a   varying                 
d                                     value                   
d stackval                   65535a   varying                 
d                                     value                   

       eval fieldId = stackname;                              
       Chain (fileId:fieldId) Vxmlxrf;                        
       If not %found(Vxmlxrf);                                
       clear xmlxrfr;                                         
       eval x1fileid = fileId;                                
       eval x1fldid  = fieldId;                               
       dou not %found(Vxmlxrf1);                              
       eval $counter = $counter + 1;                          
       eval x1unid   = 'XML_' + %trim(%editc($counter:'Z'));  
       Chain (fileId:x1unid) Vxmlxrf1;                        
       write xmlxrfr;                                         
   return 0;                                                  
P                 E   

Command to Call the Parser Cross-Ref Program

              CMD        PROMPT('Generate Parser Cross Ref File')          
             PARM       KWD(FILEID) TYPE(*CHAR) LEN(20) MIN(1) +          
                          MAX(1) PROMPT('File Id')                        
             PARM       KWD(FILEPATH) TYPE(*CHAR) LEN(250) MIN(1) +       
                          MAX(1) PROMPT('File Path')                      

Create the command using CRTCMD

CRTCMD CMD(command_name) PGM(program_name)     

Now run the command over the XML file giving it an ID

command_name FILEID(file_id) FILEPATH(file_path)

I used the file id as MyOrders so here is my output in VXMLXRF file after running that command.

XML File ID           Short Name            XML Field ID                           
MyOrders              XML_1                 /Orders/Order/@Type                    
MyOrders              XML_2                 /Orders/Order/OrderID                  
MyOrders              XML_3                 /Orders/Order/CustNumber               
MyOrders              XML_4                 /Orders/Order/OrderDate                
MyOrders              XML_5                 /Orders/Order/CustPONumber             
MyOrders              XML_6                 /Orders/Order/OrderTotal               
MyOrders              XML_7                 /Orders/Order/OrderLine/OrderlineID    
MyOrders              XML_8                 /Orders/Order/OrderLine/ItemID         
MyOrders              XML_9                 /Orders/Order/OrderLine/ItemDescription
MyOrders              XML_10                /Orders/Order/OrderLine/Quantity       
MyOrders              XML_11                /Orders/Order/OrderLine/Price          
MyOrders              XML_12                /Orders/Order/OrderLine                
MyOrders              XML_13                /Orders/Order                          
MyOrders              XML_14                /Orders  

IMPORTANT :  Never clear the VXMLXRF file if you get a new layout for the same XML, just run the command again over the new XML file and it will find any new fields and map them. Otherwise if you clear the file it may name the old fields to new id and then you have to go change your Parsing program to reference the new Ids.

Click here for next Chapter

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.