midrange.com code scratchpad
Name:
Sample XMLi program
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
01/19/2011 12:13:22 am
IP:
Logged
Description:
This program uses XMLi to build some XML
Code:
  1.      H OPTION(*NODEBUGIO:*NOEXPDDS:*SRCSTMT)                                                        
  2.      H DFTACTGRP(*NO) ACTGRP('QILE')                                                                
  3.                                                                                                     
  4.       **************************************************************************                    
  5.       * This example uses XMLi to write XML to a variable within this program.                      
  6.       *                                                                                             
  7.       * To use XMLi you simply tell it to use your variable and then tell it                        
  8.       * what you want to write. XMLi works out where to write and how to format                     
  9.       * the XML for you.                                                                            
  10.       * When you have created your XML simply tell XMLi to no longer use your                       
  11.       * variable.                                                                                   
  12.       *                                                                                             
  13.       * This example will print the contents of the variable to the console.                        
  14.       **************************************************************************                    
  15.                                                                                                     
  16.       // BNDDIR for XMLI1...                                                                        
  17.      H BNDDIR('XMLILIB/XMLI1')                                                                      
  18.                                                                                                     
  19.       // BNDDIR for displaying example results (DO NOT USE IN PRODUCTION)                           
  20.      H BNDDIR('QC2LE')                                                                              
  21.                                                                                                     
  22.       **********************************************************************                        
  23.       // Copybook for XMLI1...                                                                      
  24.       /include xmlilib/qrpglesrc,xmli1_h                                                            
  25.       *********************************************************************                         
  26.                                                                                                     
  27.       // Program variables...                                                                       
  28.      d char1024        s           1024a                                                            
  29.                                                                                                     
  30.      d i               s             10i 0                                                          
  31.       *********************************************************************                         
  32.       /free                                                                                         
  33.                                                                                                     
  34.        // Set the XML format to simple...                                                           
  35.        xmli_setFormat(XML_FORMAT_SIMPLE);                                                           
  36.                                                                                                     
  37.        // Initialise the variable and tell xmli you want to write xml to it...                      
  38.        char1024 = *blanks;                                                                          
  39.        xmli_useVariable(char1024);                                                                  
  40.                                                                                                     
  41.        // Build some XML...                                                                         
  42.        xmli_openTag('Library');                                                                     
  43.          xmli_addAttribute('version' : 'version1.2.0');                                             
  44.          xmli_addAttribute('timestamp' : %char(%timestamp));                                        
  45.                                                                                                     
  46.          // List a few categories...                                                                
  47.          xmli_openTag('Categories');                                                                
  48.          for i = 1 to 3;                                                                            
  49.            xmli_addElement('Category': %char(i*3));                                                 
  50.          endfor;                                                                                    
  51.          xmli_closeTag('Categories');                                                               
  52.                                                                                                     
  53.          // List a few authors...                                                                   
  54.          xmli_openTag('Authors');                                                                   
  55.          for i = 1 to 3;                                                                            
  56.            xmli_openTag('Author');                                                                  
  57.            xmli_addAttribute('index': %char(i*2));                                                  
  58.            xmli_addData('Author Name Goes Here');                                                   
  59.            xmli_closeTag('Author');                                                                 
  60.          endfor;                                                                                    
  61.          xmli_closeTag('Authors');                                                                  
  62.                                                                                                     
  63.          // List a few books...                                                                     
  64.          xmli_openTag('Books');                                                                     
  65.          for i = 1 to 3;                                                                            
  66.            xmli_openTag('Book');                                                                    
  67.            xmli_addAttribute('isbn' : %editc(i:'X'));                                               
  68.              xmli_openTag('Details');                                                               
  69.                xmli_addElement('Author' : %char(i*2));                                              
  70.                xmli_addElement('Category' : %char(i*3));                                            
  71.                xmli_addElement('Blurb'                                                              
  72.                               :'Something about book ' + %editc(i:'X'));                            
  73.              xmli_closeTag('Details');                                                              
  74.            xmli_closeTag('Book');                                                                   
  75.          endfor;                                                                                    
  76.        xmli_closeTag('Books');                                                                      
  77.                                                                                                     
  78.        xmli_closeTag('Library');                                                                    
  79.                                                                                                     
  80.        // !! The XML has now been written to the char1024 variable...!!                             
  81.                                                                                                     
  82.        // Tell XMLi you don't want it to use the variable anymore...                                
  83.        xmli_freeVariable(char1024);                                                                 
  84.                                                                                                     
  85.        *INLR = *on;                                                                                 
  86.        return;                                                                                      
  87.                                                                                                     
  88.       /end-free                                                                                      
© 2004-2019 by midrange.com generated in 0.008s valid xhtml & css