Submitted by edwardwright on Mon, 03/31/2008 - 00:00
Forums

Hi

I understand I can parse an ILOB to get a pointer to the DOM root of the document by using ILOB_httpRequest.

I can then output XML verbatim doing this:

ILOB_SetWriteBinary(respILOB : *ON);
ResponseWriteILOB(respILOB);

However, can I get a pointer to an element within the DOM tree?

The reason is, when forwarding XML to the client, I wish to be able to insert a stylesheet reference after the xml header line, so if I could gain reference to the root element of the document's data, that'd be cool.

Many thanks...

Edward

edwardwright

Mon, 03/31/2008 - 00:00

I've found the some answers in ASP header file XMLPARSER.RPGLE, the functions Xml_Locate and friends. However, I'll no longer be outputting the ILOB verbatim, so I still wish to know how to send to the client an XML sub-tree starting from a given node.

Thanks!

P.S. What does Xml_Dump do?

I've managed to do this:

ILOB_SubILOB(respILOB : respILOB : 22 :
ILOB_GetLength(respILOB) - 22 : 0);

ILOB_SetData(respILOB : ILOB_GetLength(respILOB) - 22 : respILOB);

%><?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?><%

Which kind of crops 22 characters from the XML (the header!), and then replaces it with a new header including a stylesheet reference. Not respILOB contains XML to begin with.

Seems like a hack though, especially the "ILOB cropping". Hmmm.

Hi edward;

Cropping is not the best way ..

The XML parser has a feature that produces an output from a specific node, which was locatet using X-path

xml_writeStmf(node : outfile);

This will not render the responseobject directly, but in combination with loading a stream file to an ILOB and the place that ILOB in the response you are good to go;

 
  * ------------------------------------------------------------- *         
* This sample shows the use of the buildin XML parser in                  
* BlueSeries                                                              
*                                                                         
* Look at the header source file "QRPGHDR" in the BlueSeries library      
* for a complete description of the functionality                         
*                                                                         
* The aim is to produce a XML document based on an element location from  
* a input XML document.                                                   
*                                                                         
* Step 1)                                                                 
* When using the XML parser - always bind your program to the XMLPARSER   
* bind directory - either by the CRTPGM command or add the "H" spec below:
*                                                                         
* Step 2)                                                                 
* Include the prototypes for the xml parser found in member "XMLPARSER"   
* in the source file "QRPGHDR" in the BlueSeries library                  
*                                                                         
* Step 3)                                                                 
* Parse your XML file by caling the "XML_Parse" function. It returns           
* logical a pointer to an XML object tree.                                     
* Examine if the parser ran ok with xml_Error() which return *ON if an         
* error did occur. In that case you can examine the reason with                
* the function "XML_GetMsg()" to retrive the error and/or                      
* use the function "XML_Dump()"   to display the xml-tree contents             
*                                                                              
* Step 4)                                                                      
* Locate each element node by the function "XML_Locate".                       
* the location can be relative or from the root by prefixinig the location     
* with an "/". Each level is separated by a "/" e.g.                           
* "/MyRoot/MyElement"  is "myelement" found within "myroot"                    
*                                                                              
* Step 5)                                                                      
* When a element node is sucessfully fetched, you can write                    
* a new XML document with this element as a root element.                      
* Use the XML_writeStmf(pElm: FileName : Ccsid)  to produce that task          
* the ccsid can be other than the source XMl document. it will                 
* convert it to the selected ccsid. Note: Comments not reproduced.             
*                                                                              
 * Step 6)                                                                            
 * Last - remember to close the XML tree with "XML_Close"                             
 *                                                                                    
 *                                                                                    
 * The XML file we want to parse in this sample look like:                            
 *                                                                                    
 *   <?xml version="1.0" encoding="ISO-8859-1"?>                                      
 *   <Myroot>                                                                         
 *      <Myelement Myattribute1="My Company name" Myattribute2="1">abc</Myelement>    
 *      <Myelement Myattribute1="Another name"    Myattribute2="0">def</Myelement>    
 *   </Myroot>                                                                        
 *                                                                                    
 * which will produce:                                                                
 *                                                                                    
 *   <?xml version="1.0" encoding="UTF-8"?>                                           
 *   <Myelement Myattribute1="Another name" Myattribute2="0">def</Myelement>          
 *                                                                                    
 * ------------------------------------------------------------- *                    
H BndDir('XMLPARSER') dftactgrp(*NO) ACTGRP(*CALLER)    
D pXml            s               *                     
D pElem           s               *                                         
D msg             s             50    varying                               
 /include qrpghdr,xmlparser                                                 
 /free                                                                      
   pXml = xml_ParseFile('/udv/expert/xmlsample2.xml':'syntax=LOOSE');       
   if Xml_Error(pXml) ;                                                     
      msg = xml_Message(pXml);                                              
   else;                                                                    
      pElem = xml_locate(pXml:'/MyRoot/MyElement[1]');                      
      xml_writeStmf(pElem : '/udv/expert/xmlsampleout.xml' : 1208);         
   endif;                                                                   
   xml_Close(pXml);                                                         
   *inlr = *on;