3

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?

2 Answers 2

9

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>)
Sign up to request clarification or add additional context in comments.

2 Comments

oh that's great - thanks! how to do it if my String contains '<![CDATA[...]]>' ?
@Mat i have updated the answer. If you have other question you should accept one of these answers and then open a new one.
1

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

1 Comment

mmmh - but how to do it such that the result will be <myXML><bla>Hello World</bla></myXML>?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.