简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM