Blog Archive

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


Click here to read from first Chapter
Before writing the Parsing program or the XML Cross-Ref generator I would like to create the copy source needed in both programs and we don't have to put the code again and again in all programs. It better to do it this way and its the best practice.

Member      Type        Text                        
PARSER      RPGLE       Parse XML Data - Routine    
PARSER_H    RPGLE       Parse XML Data - Header    

Header Copy source for XML Parsing

FQSYSPRT   O    F  132        PRINTER                                 
                                                                      
d PrintMe         ds                  qualified                       
d   name                        21a                                   
d   data                       111a                                   
                                                                      
 * Date/Time Stamps                                                   
d                 ds                                                  
d @datetime                       z                                   
d  @dtestmp                       d   overlay(@datetime : 1)          
d                                     datfmt(*iso)                    
d  @timstmp                       t   overlay(@datetime : 12)         
                                                                      
d mySaxHandler    PR            10i 0                                 
d   ignore                       1a                                   
d   event                       10i 0 value                           
d   string                        *   value                           
d   stringLen                   20i 0 value                           
d   exceptionId                 10i 0 value                           
                                                                       
d myDataHandler   PR            10i 0                     
d stackname                  65535a   varying             
d                                     value               
d stackval                   65535a   varying             
d                                     value               
                                                          
d @date           s               d                       
d @time           s               t                       
d inputFile       s            500a                       
d fileId          s             20a                       
d fileName        s             50a                       
d filePath        s            250a                       
d fieldId         s           2000a   varying             
d xml             s            500a   varying             
d ignoreMe        s              1a                       
                                                          
d                sds                                      
d  pgmnam           *proc                                 
d  prmnbr           *parms                                
d  job                  244    253                        
d  userid               254    263     

Routine Copy source for XML Parsing

P mySaxHandler    B                                            
d mySaxHandler    PI            10i 0                          
d   ignore                       1a                            
d   event                       10i 0 value                    
d   string                        *   value                    
d   stringLen                   20i 0 value                    
d   exceptionId                 10i 0 value                    
                                                               
d value           s          65535a   based(string)            
d ucs2val         s          16383c   based(string)            
                                                               
d MAX_DEPTH       c                   32                       
d depth           s             10i 0 inz(0) static            
d stackname       s          65535a   varying inz('')          
d                                     dim(MAX_DEPTH)           
d                                     static                   
d stackval        s          65535a   varying inz('')          
d                                     dim(MAX_DEPTH)                    
d                                     static                            
                                                                        
 /free                                                                  
         select;                                                        
         when event = *XML_START_DOCUMENT;                              
            depth = 0;                                                  
            stackname(*) = '';                                          
            stackval(*) = '';                                           
                                                                        
         when depth >= MAX_DEPTH                                        
            and ( event = *XML_START_ELEMENT                            
               or event = *XML_ATTR_NAME );                             
             return -1;                                                 
                                                                        
         when event = *XML_START_ELEMENT;                               
            depth += 1;                                                 
            if depth = 1;                                               
               stackname(depth) = '/' + %subst(value:1:stringLen);      
            else;                                                       
               stackname(depth) = stackname(depth-1) + '/'              
                                + %subst(value:1:stringLen);            
            endif;                                                      
            stackval(depth)  = '';                                      
                                                            
         when event = *XML_ATTR_NAME;                       
            depth += 1;                                     
            stackname(depth) = stackname(depth-1) + '/@'    
                             + %subst(value:1:stringLen);   
            stackval(depth) = '';                           
                                                            
         when event = *XML_END_ELEMENT                      
           or event = *XML_END_ATTR;                        
            myDataHandler(stackname(depth):stackval(depth));
            depth -= 1;                                     
                                                            
         when event = *XML_CHARS                            
           or event = *XML_PREDEF_REF                       
           or event = *XML_ATTR_CHARS                       
           or event = *XML_ATTR_PREDEF_REF;                 
            stackval(depth) += %subst(value:1:stringLen);   
                                                            
         when event = *XML_UCS2_REF                         
           or event = *XML_ATTR_UCS2_REF;                   
            stackval(depth) += %char(%subst( ucs2val : 1    
                                    : %div(stringLen:2) )); 
         endsl;                                             
                       
         return 0;     
 /end-free             
P                 E                   

Click here for next Chapter


Recommended Reading