E4X – Updating multiple node values

December 27, 2009 · Posted in Flash, XML · Comment 

Something that is often required when processing XML documents is to update a bunch of nodes that are spread throughout the document.

Consider the following XML:

<gallery base="images/projects/">
	<project id="1" >
		<item type="image" thumb="image1.jpg">
			<path>l_image1.jpg</path>
		</item>
	</project>
	<project id="2">
		<item type="image" thumb="image2.jpg">
			<path>l_image2.jpg</path>
		</item>
	</project>
	<project id="3">
		<item type="image" thumb="image1.jpg">
			<path>l_image1.jpg</path>
		</item>
		<item type="image" thumb="image3.jpg">
			<path>l_image3.jpg</path>
		</item>
		<item type="image" thumb="image4.jpg">
			<path>l_image4.jpg</path>
		</item>
		<item type="image" thumb="image5.jpg">
			<path>l_image5.jpg</path>
		</item>
	</project>
</gallery>

Lets assume that we need to load the thumbnails of each item specified by the thumb attribute. I often use a base path for the images. It make a lot of sense to include it at document level rather than repeating it in each property value. Doing so reduces file size and also makes path updates quick and painless.

Once the XML document is loaded, the thumb path can be updated with a single line of code:

galleryXML..item.(@thumb = baseUrl + @thumb);

where baseUrl variable holds the base path. Or we can directly substitute with the value of base property like so:

galleryXML..item.(@thumb = galleryXML.@base.toString() + @thumb);

A trace of galleryXML will show that the path of each thumb attribute is prefixed with the base path.

AS3 E4X Rundown

June 18, 2008 · Posted in AS3, Tutorials · Comment 

AS3 E4X Rundown – a good E4X reference/refresher

http://www.zeuslabs.us/2007/06/29/getting-advanced-with-e4x/

http://developer.yahoo.com/flash/articles/e4x-beginner-to-advanced.html

http://freerpad.blogspot.com/2007/07/more-hierarchical-sorting-e4x-xml-for.html