How to create CDATA with XmlTree


#1

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?


#2

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.


#3

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.