how do I manipulate elements inside a xml in action script?
What are the inbuilt functions available in flex,actionscript that I can use 开发者_StackOverflow社区to find a node with a specific name inside xml variable and what functions could help me manipulate it? Something like Xquery in SQL! I dont want to use for loop everytime I want to manipulate a xml variable. For example, if I want to "cut" node inside this xml and add another attribute to it named enabled pro grammatically. Or how could I find "Find Next" node and delete it?
<menuitem label="Edit">
<m label="Cut"/>
<m label="Copy"/>
<m label="Paste"/>
<m type="separator"/>
<m label="Find"/>
<m label="Find Next"/>
</menuitem>
Thanks guys.
Actionscript supports e4x. Also, read about XML class.
1) To find the "cut" node:
There's an easy way to find the "cut" node, but it'll require you to remove the <m type="separator" />
as it will throw an error with this code. To find it simply:
var cut:XMLList = xml.m.(@label == "Cut");
If you can't remove the "seperator" node, then you'll have to loop through to find it.
Once you have it, then to add a new attribute, it's simply:
cut.@someOther = true; // adds the attribute "someOther"
2) Deleting the "Find Next" node:
Deleting XML nodes in as3 is a bit awkward. You can try numerous ways, but you'll invariably end up with one error or another. See http://bottomupflash.wordpress.com/2008/03/26/deleting-xml-nodes-harder-than-it-looks/ for a bit more info.
The only piece of code that I could come up with that would delete the "Find Next" node is:
var i:int = 0;
for each( var x:XML in xml.m )
{
if ( x.@label == "Find Next" )
delete xml.m[i];
i++;
}
Basically, we're looping through the structure, to find the index of the "Find Next" node, then we're deleting it using the child selector.
精彩评论