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 开发者_如何学Goi 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?
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>)
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);
精彩评论