Reputation: 91
I need to add an attribute to all nodes in an XML dynamically.My xml is as follows:
<root>
<item>
<item>Americas</item>
<item>Canada</item>
<item>Ottawa</item>
<item>Category 1</item>
<item>Product 01</item>
<item>4171.132339235787</item>
<item>4181.132339235787</item>
</item>
</root>
For which I need to add an attribute named "name" to each node in above XML as:
<root>
<item name="">
<item name="Americas"/>
<item name="Canada"/>
<item name="Ottawa"/>
<item name="Category 1"/>
<item name="Product 01"/>
<item name="4171.132339235787"/>
<item name="4181.132339235787"/>
</item>
</root>
How can this be achieved in Flex XML?
Upvotes: 0
Views: 775
Reputation: 78
You need an attribute named label? But your final XML does not have any such attributes. Anyways you can use the following to add the "name" attribute to each child element of the xml:
<mx:Script>
<![CDATA[
private var newLoad:URLLoader;
private var link:String = "xl.xml";
private var req:URLRequest = new URLRequest(link);
loadU();//Place this call in the creation complete handler of the Application's CreationComplete Event
private function load(e:Event):void
{
var xm:XML = XML(e.target.data);
for each(var node:XML in xm.item.item)
{
node.@name = node;
}
var file:FileReference = new FileReference();
file.save(xm,"x1.xml");//Save the output file
}
private function loadU():void
{
newLoad = new URLLoader();
newLoad.addEventListener(Event.COMPLETE,load);
newLoad.load(req);
}
]]>
</mx:Script>
Let me know if this is what you want.
Upvotes: 1