Macromedia Flex Macromedia Flex
Modify Loaded XML Data
  Home

Mar 19, 2007 - Modify Loaded XML Data
Modify loaded XML data in the RESULT event handler using the "for each" loop.

Here is a simple way to modify loaded data. This can be used in modifying the path of image filenames. For example, you have thumbnail or reduced-size images as well as the full size images and both have the same filenames but stored in different folders (ex. "thumbs/" for the thumbnails and "full/" for the full-size images).

Our XML file or data (stored in xml/images.xml):

<images>
 <image>
  <id>1</id>
  <filename>image1.jpg</filename>
 </image>
 <image>
  <id>2</id>
  <filename>image2.jpg</filename>
 </image>
 <image>
  <id>3</id>
  <filename>image3.jpg</filename>
 </image>
 <image>
  <id>4</id>
  <filename>image4.jpg</filename>
 </image>
 <image>
  <id>5</id>
  <filename>image5.jpg</filename>
 </image>
</images>

And here's our code:

import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;

private var images:ArrayCollection = new ArrayCollection();

private function loadData():void {
 var data:HTTPService = new HTTPService();
 data.addEventListener(ResultEvent.RESULT,
  // we create an anonymous function to handle the RESULT event of the HTTPService object
  function(event:ResultEvent):void {
   images = event.result.images.image;
   var a:Array = new Array();
   // here's the important part, using the for each loop to access the loaded data
   for each (var item:Object in images) {
    item['filename'] = 'new_path/' + item['filename'];
    a.push(item['filename']);
   }
   trace(a);
  }
 );
 data.url = "xml/images.xml";
 data.send();
}

Output:

new_path/image1.jpg,new_path/image2.jpg,new_path/image3.jpg,new_path/image4.jpg,new_path/image5.jpg

File Details
Created On Mar, 19, 2007 by Rico Zuniga
Last Modified On Mar, 19, 2007 by Rico Zuniga
Group: Tips and Articles
Flex Versions: 2.0
Category: Actionscript
Type: Tip
Difficulty: Beginner
Keywords: for each, XML
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80