XMLParserService 2 Node traversal

LANSA Integrator

XMLParserService 2 (Node traversal)

This is a more complex XMLParserService example that demonstrates the node traversal of received xml. The direct binding of received xml to LANSA fields and lists obviates the need to perform node traversal in most situations (refer to HTTPService service). This example parses xml and allows traversal of the nodes in the DOM tree to load node attribute values.

 

********** Beginning of RDML commands ********** 
FUNCTION   OPTIONS(*DIRECT)                                    
**********                                                     
DEFINE     FIELD(#JSMSTS) TYPE(*CHAR) LENGTH(20)               
DEFINE     FIELD(#JSMMSG) TYPE(*CHAR) LENGTH(255)              
DEFINE     FIELD(#JSMCMD) TYPE(*CHAR) LENGTH(255)
**********                                                     
DEFINE     FIELD(#MESSAGE) TYPE(*CHAR) LENGTH(255)             
DEF_LIST   NAME(#MSGLST) FIELDS((#MESSAGE)) TYPE(*WORKING)     
**********                                                     
********** 'Open service'                                      
**********                                                     
USE        BUILTIN(JSM_OPEN) TO_GET(#JSMSTS #JSMMSG)           
EXECUTE    SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG) 
**********                                                        
********** 'Load service'                                          
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('SERVICE_LOAD SERVICE(XM 
           LPARSERSERVICE)') TO_GET(#JSMSTS #JSMMSG)               
EXECUTE    SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)           
**********                                                        
********** 'Load XML source'                                       
**********                                                        
********** Load from a file 
CHANGE     FIELD(#JSMCMD) TO('LOAD METHOD(*FILE) FILE(order.xml)') 
USE        BUILTIN(JSM_COMMAND) WITH_ARGS(#JSMCMD) TO_GET(#JSMSTS  
           #JSMMSG)                                                
EXECUTE    SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)           
**********                                                        
********** 'Parse XML'                                             
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('PARSE') TO_GET(#JSMSTS  
           #JSMMSG)                                                
EXECUTE    SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)           
**********                                                        
********** 'Get ROOT tag'                                          
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('GET OBJECT(*ROOTTAGNAME 
           )') TO_GET(#JSMSTS #JSMMSG)                             
DISPLAY    FIELDS((#JSMSTS) (#JSMMSG))                             
**********                                                  
********** Get warning and error messages                         
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('GET OBJECT(*MESSAGES) S
           ERVICE_LIST(MESSAGE)') TO_GET(#JSMSTS #JSMMSG #MSGLST) 
**********                                                        
SELECTLIST NAMED(#MSGLST)                                         
DISPLAY    FIELDS((#MESSAGE))                                     
ENDSELECT                                                         
********** 'Get node value'                                       
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('GET NODE(/SALESORDER/CU
           STOMER/CUSTNAME)') TO_GET(#JSMSTS #JSMMSG  )           
DISPLAY    FIELDS((#JSMSTS) (#JSMMSG))                            
**********                                                        
********** 'Collect SALESORDER/LINE elements'                     
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('FOREACH NODE(/SALESORDE
           R/LINE)') TO_GET(#JSMSTS #JSMMSG)                      
DISPLAY    FIELDS((#JSMSTS) (#JSMMSG))                            
**********                                                        
 
Part 2

********** 'Loop through Line elements'                           
**********                                                        
BEGIN_LOOP                                                        
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('NEXT OBJECT(*NODE)') TO
           _GET(#JSMSTS #JSMMSG)                                  
**********                                                        
IF         COND('#JSMSTS *NE OK')                                 
GOTO       LABEL(BRK)                                             
ENDIF                                                             
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('GET NODE(*CURRENT) ATTR
           IBUTE(LineNumber)') TO_GET(#JSMSTS #JSMMSG)            
**********                                                        
DISPLAY    FIELDS((#JSMSTS) (#JSMMSG))                            
**********                                                        
CHANGE     FIELD(#JSMCMD) TO('GET NODE(PART) ATTRIBUTE(PARTNUMBER)
                ')                                                
USE        BUILTIN(JSM_COMMAND) WITH_ARGS(#JSMCMD) TO_GET(#JSMSTS  
           #JSMMSG)                                                
DISPLAY    FIELDS((#JSMSTS) (#JSMMSG))                             
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('GET NODE(PART/DESCRIPTI 
           ON)') TO_GET(#JSMSTS #JSMMSG)                           
DISPLAY    FIELDS((#JSMSTS) (#JSMMSG))                             
**********                                                        
END_LOOP                                                           
BRK ********** 'Loop breakout                                    
**********                                                       
********** 'Unload service'                                       
**********                                                        
USE        BUILTIN(JSM_COMMAND) WITH_ARGS('SERVICE_UNLOAD') TO_GET
           (#JSMSTS #JSMMSG)                                      
EXECUTE    SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)          
**********                                                        
********** 'Close service'                                        
**********                                                        
USE        BUILTIN(JSM_CLOSE) TO_GET(#JSMSTS #JSMMSG)             
EXECUTE    SUBROUTINE(CHECK) WITH_PARMS(#JSMSTS #JSMMSG)          
**********                                                        
********** SUB ROUTINES                                           
**********                                                        
SUBROUTINE NAME(CHECK) PARMS((#JSMSTS *RECEIVED) (#JSMMSG *RECEIVE
           D))                                                    
**********                                                        
IF         COND('#JSMSTS *NE OK')                                 
DISPLAY    FIELDS((#JSMSTS) (#JSMMSG))                            
**********                                                        
USE        BUILTIN(JSM_CLOSE) TO_GET(#JSMSTS #JSMMSG)             
**********                                                        
MENU       MSGTXT('Java service error has occurred')               
**********                                                        
ENDIF                                                             
**********                                                        
ENDROUTINE