# XML and CDATA sections

**URL:** <https://forum.xojo.com/t/xml-and-cdata-sections/9897>\
**Category:** General\
**Created:** [July 28, 2013, 1:41am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897 "2013-07-28T01:41:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik\_Pevec](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@Erik\_Pevec](https://forum.xojo.com/u/Erik_Pevec)\
**Post date:** [July 28, 2013, 1:41am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/1 "2013-07-28T01:41:54Z")

</div>

I am working on a small tool that will allow me to open an xml file, do some value changes to nodes and save it back in xml. However the opened XML contains some CDATA sections which are converted into text nodes - all fine, but after saving the doc some characters are changed.

Source:  
\<![CDATA[

my text goes here

 ]]\>

After save I get this:

my text goes heres

 

Is there any good way to avoid this?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![Marc\_Zeedar](https://forum.xojo.com/user_avatar/forum.xojo.com/marc_zeedar/32/1007_2.png) [@Marc\_Zeedar](https://forum.xojo.com/u/Marc_Zeedar)\
**Post date:** [July 28, 2013, 1:58am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/2 "2013-07-28T01:58:55Z")

</div>

To keep it as CDATA, you have to explicitly add it back as a CDATA node before you save. At least, that’s what I recall – it’s been a while since I used CDATA but when I did, I remember running into that. I think Xojo thinks that converting illegal characters to entities is enough and it doesn’t need to be CDATA any more, or maybe it just doesn’t know it was originally CDATA once it’s been loaded into the XML tree: it just knows it’s text data, so when it writes it back out, it’s just text.

---

<div class="post-metadata">

**Author:** ![Erik\_Pevec](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@Erik\_Pevec](https://forum.xojo.com/u/Erik_Pevec)\
**Post date:** [July 28, 2013, 2:19am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/3 "2013-07-28T02:19:10Z")

</div>

Thanks for the answer. What is the best practice to add it back? Setting of type is not permitted (to force the CDATA type), and adding the value back as a: child.Value="" doesn’t really change anything. Do you know of a way?

---

<div class="post-metadata">

**Author:** ![Marc\_Zeedar](https://forum.xojo.com/user_avatar/forum.xojo.com/marc_zeedar/32/1007_2.png) [@Marc\_Zeedar](https://forum.xojo.com/u/Marc_Zeedar)\
**Post date:** [July 28, 2013, 2:27am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/4 "2013-07-28T02:27:21Z")

</div>

> [@23083:@Erik Pevec](#):
>
> Setting of type is not permitted (to force the CDATA type)

You can use `XMLDocument.CreateCDATASection` to create a CDATA section, so you should be able to read in the text, make your changes, and then write it back as a CDATA section. If all your text is CDATA, you could write your own wrapper to do that for text nodes automatically.

---

<div class="post-metadata">

**Author:** ![Erik\_Pevec](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@Erik\_Pevec](https://forum.xojo.com/u/Erik_Pevec)\
**Post date:** [July 28, 2013, 2:41am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/5 "2013-07-28T02:41:46Z")

</div>

Actually there are 2 different nodes with CDATA value repeating throughout the xml document. They appear as “defaulttext” and “textdetail”. Do you think it would be better to modify the xml.tostring value and write it as a text output stream or is there any way to modify node by node? I am not very sure how to use the createCDATASection.

---

<div class="post-metadata">

**Author:** ![Marc\_Zeedar](https://forum.xojo.com/user_avatar/forum.xojo.com/marc_zeedar/32/1007_2.png) [@Marc\_Zeedar](https://forum.xojo.com/u/Marc_Zeedar)\
**Post date:** [July 28, 2013, 2:48am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/6 "2013-07-28T02:48:26Z")

</div>

I wouldn’t modify the xml.tostring value – if you’re going that route, you might as well process the entire XML as text instead of converting to an XML tree.

Working with the createCDATASection is easy – it works just like writing a text node, except you use the createCDATASection command.

If those two text types are the only ones that are CDATA, you could check for those type names and then write the data as CDATA. It might be the simplest to do this by creating two XML trees: the original document that you read in, and a new one that you write out. You’d make any modifications you’d want in the interim. Then you’d just need a routine that steps through all the children of the original XML and copies it to the new XML document, making sure to make your mods and write those “defaulttext” and “textdetail” sections as CDATA. A bit of a hack, to workaround Xojo losing the CDATA, but not too hard to do, depending on the complexity of the original XML.

---

<div class="post-metadata">

**Author:** ![Erik\_Pevec](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@Erik\_Pevec](https://forum.xojo.com/u/Erik_Pevec)\
**Post date:** [July 28, 2013, 3:34am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/7 "2013-07-28T03:34:08Z")

</div>

Thanks for the great explanation and I am sorry for my lack of knowledge with xojo Xml CDATA sections . Anyway, when I read in the xml, all the CDATA nodes are converted to text nodes which is fine for me as I can modify the values as needed and all the data is correct. The problem appears in saving the text node.

Based on above and your explanation your suggested approach is to read the xml in, do all the necessary changes to nodes as they are and copy the source xml to an new xml by stepping through the nodes and converting the text nodes to CDATA sections. I did a quick test and my saved xml contained correct data:

\<?xml version="1.0" encoding="UTF-8"?\>\<![CDATA[12]]\>

Now the problem is with preserving line breaks. Is there any way to do so? Or would you more suggest to convert the nodes to CDATA sections when reading the xml?

---

<div class="post-metadata">

**Author:** ![Erik\_Pevec](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@Erik\_Pevec](https://forum.xojo.com/u/Erik_Pevec)\
**Post date:** [July 28, 2013, 4:03am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/8 "2013-07-28T04:03:31Z")

</div>

I just wanted to post that I have found a solution based on above suggestions. Might help also somebody else. By reading the node I know that the nodes with certain names should be CDATA sections and below snippet will create a CDATA section instead of original text node. Line breaks are preserved and xmldoc.savexml works perfect.

```
    if child.name="defaultText" or child.name="textDetail" then
      dim n as xmlnode
      dim cd as XmlCDATASection
      n=child.AppendChild (xmldoc.CreateCDATASection( child.Child(0).Value))
      child.RemoveChild(child.Child(0))
    end if

```

Thanks for your help

---

<div class="post-metadata">

**Author:** ![Marc\_Zeedar](https://forum.xojo.com/user_avatar/forum.xojo.com/marc_zeedar/32/1007_2.png) [@Marc\_Zeedar](https://forum.xojo.com/u/Marc_Zeedar)\
**Post date:** [July 28, 2013, 5:17am UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/9 "2013-07-28T05:17:19Z")

</div>

> [@23089:@Erik Pevec](#):
>
> By reading the node I know that the nodes with certain names should be CDATA sections

I believe you could also write this generically by using the `XMLCDATASection` and `isa` to see if the section is a CDATA and then use the rest of your code to preserve the CDATA.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:46pm UTC](https://forum.xojo.com/t/xml-and-cdata-sections/9897/10 "2020-10-29T16:46:34Z")

</div>


