Search This Blog

Saturday, June 10, 2017

How to generate XML file from SQR program?

Like any other file, the basic logic to opening a file / writing data and then closing file is same even for XML file generation.

Of-course, the approach followed to implement these basic stuff will be slightly different. Here we need to make use of  xml elements. So thoughtful of time needs to be spent on the design and layout of the XML file.

Approach to design XML file and implement the logic:

Once we are clear with the required information, we should next proceed to implementing the logic.

For instance, let's consider we need to generate XML file which has Address Details of an Employee.

Step 1 would be to first categorize elements. So here we can select Employee as outer element.
Step 2 would be to list out the data elements like here it is Address1,City,State,Country

XML will have to look something like below. 
Note, intentionally excluded the tag - <  in the below example as the below structure auto-converts without tags when viewed online.

?xml version="1.0" encoding="Windows-1252"?>
Employee>
Address>
Address1>"Hitec City"/Address1>
City>"Hyderabad"/City>
State>"TS"/State>
Country>"India"/Country>
/Address>
Employee>

To implement the above logic in SQR, first you need to initialize xml tags.

let $xmlcntrl_begin  = '<'
   let $xmlcntrl_end    = '>'
   let $xmltag_end      = '/'
   let $xmltext_delim   = '"'
   let $xmltag          = ''
   let $xmlcontent      = ''
   let $xmloutput_line  = ''
etc..

Below is sample code to write content.

let $xmloutput_line = $xmlcntrl_begin || $xmltag || $xmlcntrl_end  ||
                               $xmlcontent ||
                               $xmlcntrl_begin || $xmltag_end || $xmltag || $xmlcntrl_end
         write #outputfile  from $xmloutput_line

if we decode above statement it would resolve to something like.

<City>"Hyderabad"</City>

Note: if there are nested elements, ensure the tags are properly closed.

Once the data is written, we need to close XML file.

Execution:
Once the SQR is run , the file will be seen posted in the server path mentioned while opening the file.

No comments: