<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Anselm Bradford &#187; AS2</title>
	<atom:link href="http://blog.anselmbradford.com/category/flash-platform/as2/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.anselmbradford.com</link>
	<description>wrangling the Internet's wildest</description>
	<lastBuildDate>Mon, 27 Jun 2011 19:17:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Recursively convert XML into multidimensional array (AS2)</title>
		<link>http://blog.anselmbradford.com/2008/09/04/recursively-convert-xml-into-multidimensional-array-as2/</link>
		<comments>http://blog.anselmbradford.com/2008/09/04/recursively-convert-xml-into-multidimensional-array-as2/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 21:18:05 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=64</guid>
		<description><![CDATA[This is a code snippet I wrote awhile back that recursively works through an XML file and converts it from XML into a multidimensional array. This was from the days before E4X was a part of ActionScript, and as such this is an ActionScript 2.0 example, so it may not be as useful as it [...]]]></description>
			<content:encoded><![CDATA[<p>This is a code snippet I wrote awhile back that recursively works through an XML file and converts it from XML into a multidimensional array. This was from the days before <a href="http://en.wikipedia.org/wiki/E4X" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/E4X?referer=');">E4X</a> was a part of ActionScript, and as such this is an ActionScript 2.0 example, so it may not be as useful as it once was. But nonetheless, maybe someone will find it of use. Here it is (usage example is at the end):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//recursive function to convert an XML file to an array</span>
<span style="color: #000000; font-weight: bold;">function</span> XMLtoArray<span style="color: #66cc66;">&#40;</span>node:<span style="color: #0066CC;">XMLNode</span>, arr:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> nodelen:<span style="color: #0066CC;">Number</span> = node.<span style="color: #0066CC;">childNodes</span>.<span style="color: #0066CC;">length</span>;<span style="color: #808080; font-style: italic;">//number of children nodes within a particular node</span>
	<span style="color: #000000; font-weight: bold;">var</span> resultValue:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;<span style="color: #808080; font-style: italic;">//result of a recursive call to this function</span>
	<span style="color: #000000; font-weight: bold;">var</span> count:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//index number of the current node relative to its siblings</span>
&nbsp;
	node = node.<span style="color: #0066CC;">firstChild</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//loop through node children</span>
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i=<span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> nodelen; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the first child of that node does not have children</span>
		<span style="color: #808080; font-style: italic;">//create an associative array with the node name equal to the node value</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>node.<span style="color: #0066CC;">firstChild</span>.<span style="color: #0066CC;">hasChildNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			arr<span style="color: #66cc66;">&#91;</span>node.<span style="color: #0066CC;">nodeName</span><span style="color: #66cc66;">&#93;</span> = node.<span style="color: #0066CC;">firstChild</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//else node has more nodes inside, make a recursive call to walk down the XML tree</span>
			arr<span style="color: #66cc66;">&#91;</span>count<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
			resultValue = XMLtoArray<span style="color: #66cc66;">&#40;</span>node, arr<span style="color: #66cc66;">&#91;</span>count<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>resultValue <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				arr.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>resultValue<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//push result into array</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//when loop is on its last iteration return the compiled array</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>i == <span style="color: #66cc66;">&#40;</span>nodelen-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> arr;
		<span style="color: #66cc66;">&#125;</span>
		count++;
		node = node.<span style="color: #0066CC;">nextSibling</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">undefined</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> imgXML:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&lt;cat&gt;&lt;img&gt;&lt;filename&gt;1.jpg&lt;/filename&gt;&lt;caption&gt;cap1&lt;/caption&gt;&lt;/img&gt;&lt;img&gt;&lt;filename&gt;2.jpg&lt;/filename&gt;&lt;caption&gt;cap2&lt;/caption&gt;&lt;/img&gt;&lt;/cat&gt;&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> xmlArray:<span style="color: #0066CC;">Array</span> = XMLtoArray<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span>imgXML<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>xmlArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;caption&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//xmlArray[0] goes into first node &lt;cat&gt;</span>
<span style="color: #808080; font-style: italic;">//xmlArray[0][0] goes into second node &lt;img&gt;</span>
<span style="color: #808080; font-style: italic;">//xmlArray[0][0][&quot;caption&quot;] retrieves contents of &lt;caption&gt;cap1&lt;/caption&gt;</span>
&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>xmlArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;filename&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//outputs the filename of the second image</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/09/04/recursively-convert-xml-into-multidimensional-array-as2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

