Wednesday, December 9, 2009

ActionScript3 Xml - Update nested element

Today, I had yet another XML challenge in ActionScript. Previously, I needed to update a child that was directly under the root. This time I needed to update a child that was deeply nested. This was pretty tricky and the answer was my last straw before pulling out my hair. As a last ditch effort, I used the equal sign, instead of using the replace function. This time I just could not get the replace function to do what I wanted it to do. I'm not sure why, but I finally have a working solution that I'm happy with.

sample data:

<root>
<category id="1">
<books>
<book id="1" name="junk"/>
<book id="2" name="junk2"/>
</books>
</category>
</root>

var varCategoryId:int = 1;
var varBookId:int = 2;
var updateXMl = "";

var i:int = rootXML..category.(@id == varCategoryId)..book.(@id == varBookId).childIndex();

if(i > -1)
rootXML..category.(@id == varCategoryId)..book[i] = updateXml;

Hope that helps someone!

No comments:

Post a Comment