<?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>ria-coder.com &#187; xml</title>
	<atom:link href="http://ria-coder.com/blog/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>http://ria-coder.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 21 Apr 2010 19:35:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Parsing XML in PHP5 with SimpleXML</title>
		<link>http://ria-coder.com/blog/parsing-xml-in-php5-with-simplexml</link>
		<comments>http://ria-coder.com/blog/parsing-xml-in-php5-with-simplexml#comments</comments>
		<pubDate>Sat, 16 May 2009 13:21:59 +0000</pubDate>
		<dc:creator>Danny Kopping</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php5.parse]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://ria-coder.com/blog/?p=292</guid>
		<description><![CDATA[PHP5 includes a library for parsing XML data called SimpleXML. This class (and associated classes) is a fantastic way to parse XML data in PHP5.]]></description>
			<content:encoded><![CDATA[<p>PHP5 includes a library for parsing XML data called SimpleXML. This class (and associated classes) is a fantastic way to parse XML data in PHP5. Parsing of XML comes into almost every project i work on, whether it be creating/manipulating RSS feeds, sending data between Flex and PHP, and whatever else the client wants really <img src='http://ria-coder.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Below is the XML data i will be working with in this tutorial:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;languages<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;language</span> <span style="color: #000066;">year</span>=<span style="color: #ff0000;">&quot;1999&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ActionScript 3.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ActionScript is a scripting language based on ECMAScript. ActionScript is used primarily for the development of websites and software using the Adobe Flash Player platform (in the form of SWF files embedded into Web pages).<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/language<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;language</span> <span style="color: #000066;">year</span>=<span style="color: #ff0000;">&quot;1995&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PHP<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PHP is a scripting language originally designed for producing dynamic web pages. It has evolved to include a command line interface capability and can be used in standalone graphical applications.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/language<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;language</span> <span style="color: #000066;">year</span>=<span style="color: #ff0000;">&quot;1995&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Java<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/language<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;language</span> <span style="color: #000066;">year</span>=<span style="color: #ff0000;">&quot;2007&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>LOLCODE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>LOLCODE is an esoteric programming language inspired by the language expressed in examples of the lolcat Internet meme. The language was created in 2007 by Adam Lindsay, researcher at the Computing Department of Lancaster University.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/language<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/languages<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Now, say we wanted to parse this XML file using PHP, and extract the name of each of these languages&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$languages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;languages.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$languages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$language</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$language</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In the above code, one can extrapolate that SimpleXML will go through your XML file and assign the nodes to an array, and in that array it will create an untyped Object with the data assigned to properties of that Object.</p>
<p>Simple enough, right?</p>
<p>Now, what if you wanted to access an attribute of each of these languages (in this scenario &#8211; the <em>year</em> attribute of each language)?<br />
That&#8217;s also really easy with SimpleXML! You can reference the attributes of each node by using array notation&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$languages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;languages.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$languages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$language</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$language</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;year&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Ok. So what if you wanted your returned data to be mapped to a class? I.e. each element in the array that is returned must be <strong>strongly typed</strong> to a class, rather than the <strong>unspecific stdClass Object</strong> that&#8217;s returned by default&#8230; This can also be done with SimpleXML!</p>
<p>Here&#8217;s the Language.php class i created for the returned array elements to be typed as (note that it ,<u>must</u> extend the SimpleXMLElement class):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">class</span> Language <span style="color: #000000; font-weight: bold;">extends</span> SimpleXMLElement
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$year</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$description</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>&#8230;and the code to return each array element as a Language Object:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Language.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$languages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;languages.xml&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Language&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$languages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$language</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$language</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>If you look at the output, you will see that the year attribute is not assigned to $year property of the Language class, but rather to a @attributes property. However, if you add this function to the Language.php class, the attributes will be assimilated to their respective properties:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">class</span> Language <span style="color: #000000; font-weight: bold;">extends</span> SimpleXMLElement
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$year</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$description</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> attrToProp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$prop</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$prop</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>&#8230;and run the script again, but adding this line of code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Language.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$languages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;languages.xml&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Language&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$languages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$language</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$language</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attrToProp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// -- add this line</span>
		<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$language</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://ria-coder.com/blog/parsing-xml-in-php5-with-simplexml/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating XML-based menus with Flash CS4 and Flex</title>
		<link>http://ria-coder.com/blog/creating-xml-based-menus-with-flash-cs4-and-flex</link>
		<comments>http://ria-coder.com/blog/creating-xml-based-menus-with-flash-cs4-and-flex#comments</comments>
		<pubDate>Tue, 17 Mar 2009 20:49:56 +0000</pubDate>
		<dc:creator>Danny Kopping</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Lessons]]></category>
		<category><![CDATA[flash cs4]]></category>
		<category><![CDATA[flex component kit]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml menu]]></category>

		<guid isPermaLink="false">http://ria-coder.com/blog/?p=174</guid>
		<description><![CDATA[In this tutorial, i'll be showing you how to use the Flex Component Kit, XML, Flash CS4 and Flex 3 to build an extensible, aesthetically pleasing and interactive menu system.]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, i&#8217;ll be showing you how to use the <em>Flex Component Kit</em>, XML, Flash CS4 and Flex 3 to build an extensible, aesthetically pleasing and interactive menu system. Each ingredient in this tutorial is of equal important (with the exception of XML which can easily be replaced with JSON, CSV, etc).</p>
<p>First, a preview of what i will be covering in this tutorial:</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_menu_1148415079"
			class="flashmovie"
			width="300"
			height="400">
	<param name="movie" value="http://ria-coder.com/temp/wp-content/uploads/2009/03/menu.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://ria-coder.com/temp/wp-content/uploads/2009/03/menu.swf"
			name="fm_menu_1148415079"
			width="300"
			height="400">
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>In the SWF file above, an XML file is being processed and <em>x</em> amount of menu items are being created at runtime according to the XML specification of the menu. In the SWF above, i&#8217;m using the following XML file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;The Dark Side Of The Moon&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Wish You Were Here&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Delicate Sound Of Thunder&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;The Division Bell&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Pulse&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Click <a title="Project Files" href="http://ria-coder.com/blog/wp-content/uploads/2009/03/xml-menu.zip" target="_blank">here</a> to download the exercise files for this tutorial.<br />
You will also need to download and install the <strong><em>Flex Component Kit</em></strong><em> </em>from <a title="Flex Component Kit" href="https://www.adobe.com/cfusion/entitlement/index.cfm?e=flex_skins" target="_blank">here</a>.</p>
<p>Alright, so let&#8217;s dive right in.</p>
<p>So what are we exactly trying to achieve here? We&#8217;re trying to:</p>
<ol>
<li>take a symbol that we have created in Flash CS4 and export it</li>
<li>import the symbol into Flex</li>
<li>create an XML-based menu with instances of the symbol</li>
</ol>
<h3>Step 1</h3>
<p>Open the <em>flash </em>folder in the exercise files and open the <strong>MenuItem.fla</strong> file inside of Flash CS4. (Note: There is nothing Flash CS4-specific in this file, so if you&#8217;d like a version of the FLA that&#8217;ll work with CS3, pop me an email)</p>
<p>On the stage you will find one symbol &#8211; the <strong>MenuItem</strong> symbol. In the Library, there is a <strong>MenuItem</strong> symbol that has been exported for Flex, the <strong>FlexComponentBase</strong> and two other symbols that are used inside the <strong>MenuItem</strong> symbol.<a href="http://ria-coder.com/temp/wp-content/uploads/2009/03/flex-component.png"><img class="size-medium wp-image-190 alignright" title="flex-component" src="http://ria-coder.com/blog/wp-content/uploads/2009/03/flex-component-300x180.png" alt="flex-component" width="300" height="180" /></a></p>
<p>All that the <strong>MenuItem</strong> symbol is is a MovieClip with a little animation, a little masking, a dynamic text field and a background MovieClip. The reason why i made the background its own symbol is because you may want to do a color transform tween or something once you roll over the <strong>MenuItem</strong> symbol. I have not done this in the tutorial, but nonetheless &#8211; give it a go if you feel the need.</p>
<p>Once you have installed the <em>Flex Component Kit</em>, you will see that two new items are available in the <strong>Commands</strong> menu: <em>Convert Symbol to Flex Component </em>and <em>Convert Symbol to Flex Container</em>. Once you have created a symbol inside of Flash, you will select it and select the <em>Convert Symbol to Flex Component</em> from the <strong>Commands</strong> menu. This will do all the heavy lifting that is required to make it compatible with Flex.</p>
<p>There are a couple of things to note before you export the symbol:</p>
<p>Firstly, you will need to export your symbol for ActionScript by right-clicking the symbol in the library and selecting <strong>Properties</strong>. Check the box next to <em>Export for ActionScript</em>. Next, choose a name for your symbol to be used in Flex. I have chosen <strong>MenuItem</strong> as the <em>Class</em>, and i have chosen <strong>MenuItemClass</strong> as the <em>Base</em> <em>Class</em>.</p>
<p><a href="http://ria-coder.com/temp/wp-content/uploads/2009/03/document-class.png"><img class="size-medium wp-image-186 alignleft" style="margin: 5px;" title="document-class" src="http://ria-coder.com/blog/wp-content/uploads/2009/03/document-class-228x300.png" alt="document-class" width="228" height="300" /></a>I will be coming back to this in a moment. For the moment, you can set the <em>Base Class</em> value to <strong>mx.flash.UIMovieClip</strong> &#8211; this class is a modified implementation of the usual <strong>MovieClip</strong> class you use in Flash, and will make your symbol compatible with Flex. Now select the <em>Convert Symbol to Flex Component </em>option from the <strong>Commands </strong>menu.</p>
<h3>Step 2</h3>
<p>Now we&#8217;re going to import the newly prepared Flash symbol for Flex. Flex makes use of <strong>.SWC </strong>files when importing external libraries, so we&#8217;re going to export our Flash symbol to a <strong>.SWC</strong> file. We do this by publishing the FLA. Go over to Flash and hit <strong>Shift+F12</strong>, or select <strong>Publish </strong>from File-&gt;Publish. This will export our symbol as a SWC file, as well as a SWF file. We will not be using the SWF file.</p>
<p>Now, go over to Flex and import the project in the exercise files. Go to File-&gt;Import-&gt;Flex Project, and navigate to the path where you exported the exercise files.</p>
<p style="text-align: center;"><a href="http://ria-coder.com/temp/wp-content/uploads/2009/03/create-project.png"><img class="size-medium wp-image-185 aligncenter" title="create-project" src="http://ria-coder.com/blog/wp-content/uploads/2009/03/create-project-300x259.png" alt="create-project" width="300" height="259" /></a></p>
<p>Create the Flex project and then go to the <strong>Project</strong> menu, and select <strong>Properties</strong>. Find the <strong>Flex Build Path</strong> option and select the <strong>Library Path</strong> tab. In this tab, click on the <strong>Add SWC</strong> button. Navigate to the <em>flash</em> folder in the project and find the SWC file you have recently published.</p>
<p><a href="http://ria-coder.com/temp/wp-content/uploads/2009/03/build-path.png"><img class="aligncenter size-medium wp-image-189" title="build-path" src="http://ria-coder.com/blog/wp-content/uploads/2009/03/build-path-284x300.png" alt="build-path" width="284" height="300" /></a></p>
<p>Now you&#8217;re ready to use your symbol inside of Flex! Remember the name of the class you specified in your symbol linkage in Flash? I used <strong>MenuItem</strong>. Go over to your main application file in Flex, open a new MXML tag and start typing <strong>MenuItem</strong>. You will see that Flex picks up your Flash symbol in the MXML declaration. Now we&#8217;re ready for some real action.</p>
<h3>Step 3</h3>
<p>As you can see from my Flex example, i am loading in an XML file called <strong>structure.xml</strong>. This XML file contains a very basic structure &#8211; it&#8217;s merely 5 items, each with <em>label</em> attributes &#8211; and all relating to one of the greatest bands i&#8217;ve had the pleasure of listening to&#8230; <strong>Pink Floyd <img src='http://ria-coder.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </strong></p>
<p>If you open up the <em>flash </em>folder of the project, you will find a <strong>MenuItemClass.as</strong> file. This will act as the base class for your Flash symbol. Go back to Flash CS4, right click on your symbol in the library and click <strong>Properties</strong>. Now set the <em>Base Class</em> value to <strong>MenuItemClass.as</strong>. Here&#8217;s an explanation of the <strong>MenuItemClass.as </strong>file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>flash<span style="color: #000066; font-weight: bold;">.</span>UIMovieClip<span style="color: #000066; font-weight: bold;">;</span>	<span style="color: #009900; font-style: italic;">// UIMovieClip extends the functionality of the standard flash.display.MovieClip class, so you can use play(), gotoAndStop(), etc to control your Flash symbol's timeline like you normally would in Flash</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MenuItemClass <span style="color: #0033ff; font-weight: bold;">extends</span> UIMovieClip
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MenuItemClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>		<span style="color: #009900; font-style: italic;">// Inheret the super-class' properties and functions</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900; font-style: italic;">// Setter function for the label dynamic textfield</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> label<span style="color: #000000;">&#40;</span>label<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			textField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = label<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900; font-style: italic;">// Getter function for the label dynamic textfield</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> label<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> textField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Once you have changed your Flash symbol in Flash CS4, remember to re-publish it so that the SWC will be updated in Flex.</p>
<p>Here&#8217;s an explanation of the logic in the <strong>Menu.mxml</strong> file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> createMenu<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> items<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">XMLList</span> = menuXML<span style="color: #000066; font-weight: bold;">..</span>item<span style="color: #000066; font-weight: bold;">;</span>		<span style="color: #009900; font-style: italic;">// Search for all items in the XML</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> item<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #0033ff; font-weight: bold;">in</span> items<span style="color: #000000;">&#41;</span>		<span style="color: #009900; font-style: italic;">// Loop through all items</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> menuItem<span style="color: #000066; font-weight: bold;">:</span>MenuItem = <span style="color: #0033ff; font-weight: bold;">new</span> MenuItem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>	<span style="color: #009900; font-style: italic;">// Create new instance of the Flash symbol we imported</span>
		menuItem<span style="color: #000066; font-weight: bold;">.</span>label = item<span style="color: #000066; font-weight: bold;">.</span>@label<span style="color: #000066; font-weight: bold;">;</span>		<span style="color: #009900; font-style: italic;">// Set the text property of the dynamic text field to the label of the current item in the XML</span>
		menuItem<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">45</span><span style="color: #000066; font-weight: bold;">;</span>
		menuItem<span style="color: #000066; font-weight: bold;">.</span>xml = item<span style="color: #000066; font-weight: bold;">;</span>		<span style="color: #009900; font-style: italic;">// Add the XML of the current item to the xml property of the menuItem variable. Note: this can be named anything since MenuItem's superclass is UIMovieClip which is a dynamic class. You could use menuItem.myBigToe = item if you liked</span>
		<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>menuItem<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>		<span style="color: #009900; font-style: italic;">// Add the item to the stage</span>
&nbsp;
		menuItem<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> showDetails<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And that&#8217;s it! That&#8217;s how you can use Flash symbols inside of Flex.</p>
]]></content:encoded>
			<wfw:commentRss>http://ria-coder.com/blog/creating-xml-based-menus-with-flash-cs4-and-flex/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

