Blog Archive

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"?>
<Orders>
<Order Type="Sales">
<OrderID>101</OrderID>
<CustNumber>ABC</CustNumber>
<OrderDate>01-05-2011</OrderDate>
<CustPONumber>TEST PO - 123</CustPONumber>
<OrderTotal>99.99</OrderTotal>
<OrderLine>
<OrderlineID>1</OrderlineID>
<ItemID>MCP-X2</ItemID>
<ItemDescription>Cuisinart MultiClad Pro</ItemDescription>
<Quantity>5</Quantity>
<Price>10.00</Price>
</OrderLine>
<OrderLine>
<OrderlineID>2</OrderlineID>
<ItemID>DC25-X</ItemID>
<ItemDescription>Dyson Vacuum Cleaner</ItemDescription>
<Quantity>1</Quantity>
<Price>49.99</Price>
</OrderLine>
</Order>
<Order Type="Sales">
<OrderID>102</OrderID>
<CustNumber>ABC</CustNumber>
<OrderDate>03-05-2011</OrderDate>
<CustPONumber>TEST PO - 126</CustPONumber>
<OrderTotal>1212.12</OrderTotal>
<OrderLine>
<OrderlineID>1</OrderlineID>
<ItemID>MMM-X123</ItemID>
<ItemDescription>Houdini Hand Blown Crystal</ItemDescription>
<Quantity>20</Quantity>
<Price>20.00</Price>
</OrderLine>
<OrderLine>
<OrderlineID>2</OrderlineID>
<ItemID>LG123456</ItemID>
<ItemDescription>French Door Refrigerator</ItemDescription>
<Quantity>1</Quantity>
<Price>800.12</Price>
</OrderLine>
</Order>
</Orders>

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                                                               
A          K X1FILEID                                           
A          K X1FLDID 

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

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

Click here for next Chapter

Recommended Reading