# How to create CDATA with XmlTree

**URL:** <https://discourse.libcinder.org/t/how-to-create-cdata-with-xmltree/1382>\
**Category:** Developing Cinder\
**Created:** [December 13, 2018, 1:03am UTC](https://discourse.libcinder.org/t/how-to-create-cdata-with-xmltree/1382 "2018-12-13T01:03:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![morphogencc](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@morphogencc](https://discourse.libcinder.org/u/morphogencc)\
**Post date:** [December 13, 2018, 1:03am UTC](https://discourse.libcinder.org/t/how-to-create-cdata-with-xmltree/1382/1 "2018-12-13T01:03:49Z")

</div>

This feels like a pretty straightforward question, but for the life of me I can’t create an XML Object with CDATA.

I’ve been using:

```
XmlTree Scene("Scene", "");
XmlTree cdata("cdata", "");
cdata.setValue("This is a test.");
cdata.setNodeType(cinder::XmlTree::NodeType::NODE_CDATA);
Scene.push_back(cdata);
Scene.write(writeFile("ui.xml"));

```

And the XML tree returned is:

```
<?xml version="1.0" encoding="utf-8"?>
<Scene>
    <![CDATA[

    ]]>
</Scene>

```

which clearly has empty CDATA.

I even tried entering the entire xml file as a string:

```
std::string xmlString = "<Scene><![CDATA[" + "this is a test" + "]]></Scene>";
XmlTree Scene(xmlString);
Scene.write(writeFile("ui.xml"));

```

But the XML this outputs lacks the CDATA entirely:

```
<?xml version="1.0" encoding="utf-8"?>
<Scene>this is a test</Scene>

```

How would I go about entering data into the CDATA field?

---

<div class="post-metadata">

**Author:** ![xumo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/xumo/32/75_2.png) [@xumo](https://discourse.libcinder.org/u/xumo)\
**Post date:** [December 17, 2018, 5:21pm UTC](https://discourse.libcinder.org/t/how-to-create-cdata-with-xmltree/1382/2 "2018-12-17T17:21:33Z")

</div>

I took a look, but the only thing I found is the option XmlTree::ParseOptions that has a collapseCData flag, but it crashes when using ti to construct a XmlTreeNode with the “expected \<” error from rapidxml.

---

<div class="post-metadata">

**Author:** ![morphogencc](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@morphogencc](https://discourse.libcinder.org/u/morphogencc)\
**Post date:** [December 24, 2018, 6:31pm UTC](https://discourse.libcinder.org/t/how-to-create-cdata-with-xmltree/1382/3 "2018-12-24T18:31:58Z")

</div>

That should be the expected behavior – the `XmlTree::ParseOptions` object is used for parsing an XML string in the constructor; if you pass that object, the constructor is expecting XML (not to /create/ XML). That’s why it crashes.

Unfortunatey, that also doesn’t resolve why we can’t write CDATA to the XML file.
