Creating XML string in Flex (AS3) using DOM functions

I want to create an XML string in Flex 3 in the same way that you can access it in Java. I want only 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 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");

Is it possible?

+3
source share
3 answers

Stay away from XMLNode if you use as3, this is an inherited class, the new XML and XMLList classes are those that support excellent E4X. Using them is easy:

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

@ means this is an attribute, not a use of it, add child nodes instead.

+13
source

This will probably be simpler:

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>
+1

, ,

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

myXML.@[_attname][0];
0

Source: https://habr.com/ru/post/1703341/


All Articles