Pages

Thursday, December 31, 2009

CDATA Tag in XML

It was once that I had to export the contents of all the (Action) Buttons in a lotus notes application into a DXL file, ( a Domino XML file) and parse them back to create Agents containing the same code as present in the Buttons.

The operation ran smoothly until I encountered a specific issue. And the issue was with certain characters like "<", ">", etc...

To give you a clear picture, let me say that I have a button named Source

The code in that button be,

...code fragment...
if(x<y) then
  ...do some operation
else if (x>y) then
   do some thing else
end if
...code fragment...

So my exporter shall export it into a DXL as follows (say)

<dxl>
 <buttons>
  <button>
   <name> Source </name>
   <code>
    ...code fragment...
    if(x<y) then
      ...do some operation
    else if (x>
y) then
       do some thing else
    end if
    ...code fragment...
   </code>
  <button>
 </buttons>
</dxl>


When I parse this resultant file to obtain the code, the code that has been highlighted in bold was considered as a tag accoring to the simple plain xml rule... any thing inbetween < and > is considered as a tag. And that created a lot of trouble for me when I attempted to parse the file.

So, eventually I ended up searching for a solution and ended up by discovering the usage of the CDATA tag.

Any thing that is put in between a CDATA tag is not parsed and thus it prevented my dxl from breaking up.

The bug fixed code will look like the following,

<dxl>
 <buttons>
  <button>
   <name> Source </name>
   <code>
    <![CDATA[
     ...code fragment...
     if(x<y) then
       ...do some operation
     else if (x>y) then
        do some thing else
     end if
     ...code fragment...
    ]]>

   </code>
  <button>
 </buttons>
</dxl>

No comments:

Post a Comment