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

Click here to read from first Chapter
Lets start with a sample XML data of Orders coming to your system from Third party software.
<?xml version="1.0" encoding="UTF-8"?>
<Order Type="Sales">
<CustPONumber>TEST PO - 123</CustPONumber>
<ItemDescription>Cuisinart MultiClad Pro</ItemDescription>
<ItemDescription>Dyson Vacuum Cleaner</ItemDescription>
<Order Type="Sales">
<CustPONumber>TEST PO - 126</CustPONumber>
<ItemDescription>Houdini Hand Blown Crystal</ItemDescription>
<ItemDescription>French Door Refrigerator</ItemDescription>

Now to put this XML into a DB2 database we need to create the Order Header and Detail Files that will get populated when we parse this XML input data.

Member      Type        Text                
ODETL       PF          Order Detail file    

OHEAD       PF          Order Header file    

DDS Source for Order Detail Physical File
A                                      UNIQUE                    
A          R ODETLR                                              
A            X2DEL          1A         COLHDG('Status')          
 *                                     A = Active                
 *                                     B = XML Processed         
A            X2CMP          3S 0       COLHDG('Company No.')     
A            X2ORD          9S 0       COLHDG('Order Number')    
A            X2SEQ          4S 0       COLHDG('Order Line No.')  
A            X2ITEM        20A         COLHDG('Item Number')     
A            X2DESC        30A         COLHDG('Item Description') 
A            X2QTY          8S 0       COLHDG('Order Quantity')  
A            X2PRIC        11S 2       COLHDG('Item Price')      
A          K X2CMP                                               
A          K X2ORD                                               
A          K X2SEQ  

DDS Source for Order Header Physical File
A                                      UNIQUE                       
A          R OHEADR                                                 
A            X1DEL          1A         COLHDG('Status')             
 *                                     A = Active                   
 *                                     B = XML Processed            
A            X1CMP          3S 0       COLHDG('Company No.')        
A            X1ORD          9S 0       COLHDG('Order Number')       
A            X1TYPE         1A         COLHDG('Transaction Type')   
 *                                     O = Sales Order              
 *                                     C = Credit                   
A            X1ORDT         8S 0       COLHDG('Order Date')         
A            X1CUST        10A         COLHDG('Customer Number')    
A            X1PO          20A         COLHDG('PO Number')          
A            X1OTOT        11S 2       COLHDG('Order Total')        
A          K X1CMP                                                  
A          K X1ORD 

Now that we have our input (XML) and output physical files, we are ready to parse. We can now write the parser program but I have added a step here to ease the Coding. Lets say you want to map the ItemID to a field in the RPGLE program, then you have to reference the whole path /Orders/Order/OrderLine/ItemID  in the program to get the value. So what I do is create a Cross-Ref mapping file for use in the parser program.

DDS source for XML Cross-Reference File (VXMLXRF)

A                                      UNIQUE                   
A                                      REF(VAIREF)              
A          R XMLXRFR                                            
A            X1DEL     R               REFFLD(##DEL)            
A            X1FILEID      20A         COLHDG('XML File ID')    
A            X1UNID        20A         COLHDG('Short Name')     
A            X1FLDID     2000A         COLHDG('XML Field ID')   
A                                      VARLEN                   
A          K X1FILEID                                           
A          K X1FLDID 

DDS source for XML Cross-Reference Logical File (VXMLXRF1)

A          R XMLXRFR                   PFILE(VXMLXRF)       
A          K X1FILEID                                       
A          K X1UNID    

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.