3

I want to create an XML string in Flex 3 in a similar way to you might approach it in Java. I only want a small piece of XML in the format

<slide thumb="http://www.thumbs.com/thumb.jpg" type="static" blah="bleh" />

The only type of code I can find for doing this seems ridiculous....

private function createXML(): void
{
var xm:XML = <Relyon></Relyon>;
var nodeName:String = “EMPLOYEENAME”;
var nodeValue:String = “KUMAR”;
var xmlList:XMLList = XMLList(”<”+nodeName+”>”+nodeValue+”</”+nodeName+”>”);
xm.appendChild(xmlList);
Alert.show(xm);
}

I would like to do something like...

var x:XMLNode = new XMLNode("slide");
x.setAttribute("thumb", thumbURL);
x.setAttribute("type", "static");

This is surely possible?

3 Answers 3

13

Stay away from XMLNode if you're using as3, it's a legacy class, the new XML and XMLList classes are the ones that support the excellent E4X. Using those it's as easy as this:

var myXML:XML = <slide />;
myXML.@thumb="http://www.thumbs.com/thumb.jpg";
myXML.@type="static";
myXML.@blah="bleh";
trace("myXML", myXML.toXMLString());

The @ means it's an attribute, not using that would add child nodes instead.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't edit your answer, but I think you mean "attribute", not "property".
I would even remove the new XML entirely and just say: var myXML:XML = <slide />;
1

This would probably be easier:

var thumb:String = "http://www.thumbs.com/thumb.jpg";

var type:String = "static";

var blah:String = "bleh";

var xml:XML =        

          <Relyon>
             <slide thumb={thumb} type={type} blah={blah} />
          </Relyon>;


trace( xml.toXMLString() );

//traces out

 <Relyon>
     <slide thumb="http://www.thumbs.com/thumb.jpg" type="static" blah="bleh"/>
 </Relyon>

Comments

0

Also, if you are using a variable as the attribute name you can use

var _attName:String = "foo";
myXML.@[_attName] = "bar";

but you would need to read it back out as

myXML.@[_attname][0];

Comments

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.