PDA

View Full Version : Setting CDATA in Actionscript


Ruben
04-06-2007, 08:07 AM
I can't seem to set CDATA values in XML through actionscript, does anybody have any suggestions? I'd much appreciate it..

- Ruben

panel
04-06-2007, 04:48 PM
Mabye it will help

A CDATA tag begins with <![CDATA[ and it ends with ]]>.
<example><![CDATA[a < b]]></example>
Text node would be retrieved as a < b, not <![CDATA[a < b]]>

Ruben
04-06-2007, 05:02 PM
Your point being..?

- Ruben

senocular
04-06-2007, 05:32 PM
as in:

var my_xml:XML = <example><![CDATA[a < b]]></example>;
trace(my_xml[0]); "a < b"

?

Ruben
04-06-2007, 06:15 PM
Alright, entirely my bad, I should've been more specific. I know it does work when you do it like you said senocular, but what if I want to insert the value of a variable in the cdata?

var alibaba:String = "forty thieves";
var myXML:XML = <example><![CDATA[alibaba]]></example>;
trace(myXML[0]); // outputs "alibaba", not "forty thieves"

By the way, my apologies for the rudeness, thanks to both of you, panel and sen..

:confused: - Ruben

senocular
04-06-2007, 06:24 PM
you can use {} for escaped text nodes.

var myXML:XML = <example>{alibaba}</example>;

Ruben
04-06-2007, 06:27 PM
Doesn't work either, I've tried it before.. Oh by the way, I really love the way you keep helping out man..

- Ruben

senocular
04-06-2007, 06:30 PM
just tried it and it seems to work ok
var alibaba:String = "forty thieves";
var myXML:XML = <example>{alibaba}</example>;
trace(myXML.toXMLString()); // <example>forty thieves</example>

Ruben
04-06-2007, 06:36 PM
I know it works for normal xml, but not for cdata:var alibaba:String = "forty thieves";
var myXML:XML = <example><![CDATA[{alibaba}]]></example>;
trace(myXML.toXMLString()); // <example><![CDATA[{alibaba}]]></example>

- Ruben

senocular
04-06-2007, 06:37 PM
if you need it to be in CDATA then you have to make the node from a string

var myXML:XML = XML("<example><![CDATA["+alibaba+"]]></example>");

Ruben
04-07-2007, 12:23 PM
Oh god, this was what I initially tried, so I went and tried to find out whether my problem was in another place, and indeed it was.
The TextArea from which I was taking the text had its text property set to a data-bound value. I had made the TextArea editable, so the user could adjust its text-value. However, when a script asked for its text-value (even after editing the text), it would just refer to its original value (the data in the bound source)..

Nevertheless I enormously appreciate the time and effort, thanks!

- Ruben

senocular
04-07-2007, 12:26 PM
np ; )