Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionScript 3.0 adding node to XML

i have

<myXML>
</myXML>

using

myXML.appendChild(<bla>text</bla>);

i get

<myXML>
  <bla>text</bla>
</myXML>

but how to do it if 'text' is actually something contained in a string variable? so how do i add a childnode that has some programmatically generated content?

so i have:

var myXML:XML = <myXML><myXML>
var myString:String = "Hello World"

i want to do something like

myXML.appendChild(<bla>mySring</bla>);

how to do that?

like image 407
Mat Avatar asked Sep 23 '26 01:09

Mat


2 Answers

You can also use the {} notation:

var myXML:XML = <myXML/>
var myString:String = "Hello World"
//...
myXML.appendChild(<bla>{myString}</bla>)

Update 1:

If you want to use the CDATA inside your string you can convert it to an XML node, and then add it to your current XML:

var myXML:XML = <myXML/>
var myString:String = "<![CDATA[Hello World]]>"
//...
myXML.appendChild(<bla>{new XML(myString)}</bla>)
like image 123
Patrick Avatar answered Sep 26 '26 01:09

Patrick


You are very close Mat, just missing a node to append to:

var myString:String = 'Hello World';
//this does not work
//var xml:XML = <myXML>myString</myXML>; 
//this works
var myXML:XML = <myXML />.appendChild(myString);
//test
trace(myXML,myXML.toXMLString());

It would nice if the value stored in myString would go straight into an XML declaration, without the need to appendChild.

Using your extra node(bla), you simply append the string variable to that node:

var myString:String = 'Hello World';
var myXML:XML = <myXML><bla /></myXML>;
myXML.bla.appendChild(myString);
//test
trace(myXML);
like image 23
George Profenza Avatar answered Sep 25 '26 23:09

George Profenza