<?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>GhostWire Studios &#187; XML</title>
	<atom:link href="http://www.ghostwire.com/blog/archives/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ghostwire.com/blog</link>
	<description>Flash UI Components</description>
	<lastBuildDate>Tue, 23 Aug 2011 03:51:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[AS3] Embedding Binary XML</title>
		<link>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 06:12:54 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1554</guid>
		<description><![CDATA[This post is a supplement to &#8220;Saving XML As Binary&#8221;. I neglected to mention in that post that you can, if so desired, embed the binary XML within your SWF instead of loading it during run-time. That means you still get to keep the XML externally, not as part of your application code, so that [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>.  I neglected to mention in that post that you can, if so desired, embed the binary XML within your SWF instead of loading it during run-time.  That means you still get to keep the XML externally, not as part of your application code, so that the code and data can still be kept separate and maintained more easily.</p>
<p>However, it must be mentioned that embedding a text XML within SWF will get it compressed as part of the SWF compression anyway.  Therefore, if compression is the only motivation, don&#8217;t convert the XML to binary and embed the binary version &#8211; it does not make sense.  But if you wish to do some data encryption, then the additional work may be appropriate.</p>
<p><span id="more-1554"></span><br />
<strong>Embedding External Data</strong><br />
You can embed any external source, including any external XML file, within your SWF using the [Embed] tag:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// assuming you have an XML file named &quot;data.xml&quot; in folder &quot;assets/data/&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/data/data.xml&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const binaryXML:<span style="color: #000000; font-weight: bold;">Class</span>;</pre></div></div>

<p><!-- --><br />
<strong>Back To XML</strong><br />
You can then instantiate the binary data and convert it back to XML in your code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> binaryXML<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
	xmlData = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>NOTE: The above code will be able to handle both binary XML and text XML files.</p>
<p><!-- --><br />
<strong>ROT128 Encrypted Binary XML</strong><br />
If you had performed <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/">ROT128 encryption</a> on the binary XML, you decrypt it accordingly before calling <code>uncompress()</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> binaryXML<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
	<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">// END ROT128</span>
	bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	xmlData = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>NOTE: The above code assumes the data is binary and ROT128 encrypted; it will not be able to handle text XML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Applying ROT128 Encryption On Binary XML</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 11:05:12 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1381</guid>
		<description><![CDATA[This is Part III of our discussion on ROT128 Encryption. Part I: &#8220;Applying ROT128 Encryption On ByteArray&#8221; Part II: &#8220;Applying ROT128 Encryption On Embedded/Module SWFs&#8221; In &#8220;Saving XML As Binary&#8221;, we looked at how text XML can be stored in a ByteArray object so that it can be compressed and made non-human-readable. In this post, [...]]]></description>
			<content:encoded><![CDATA[<p>This is Part III of our discussion on ROT128 Encryption.</p>
<p>Part I: <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">&#8220;Applying ROT128 Encryption On ByteArray&#8221;</a><br />
Part II: <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/">&#8220;Applying ROT128 Encryption On Embedded/Module SWFs&#8221;</a></p>
<p>In <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>, we looked at how text XML can be stored in a ByteArray object so that it can be compressed and made non-human-readable.  In this post, we look at how you can integrate ROT128 into the XML-to-ByteArray-to-XML routines.  Binary XML is used only as an example; you can definitely apply the same concept to other binary data.</p>
<p><span id="more-1381"></span><br />
<strong>XML-to-ByteArray:</strong><br />
ROT128 is applied after the XML is stored and compressed as ByteArray.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// xmlData is original XML string</span>
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span>
<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;bindata.xml&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;bindata.xml&quot;</span></pre></div></div>

<p><!-- --><br />
<strong>ByteArray-to-XML:</strong><br />
With ROT128 applied, the saved binary file is no longer a valid compressed ByteArray &#8211; it cannot be uncompressed and converted back to XML without first reversing the encryption on the raw data.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// event handler for Event.COMPLETE of URLLoader loading external XML</span>
<span style="color: #000000; font-weight: bold;">function</span> on_XML<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #66cc66;">*</span> = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is ByteArray<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">try</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
			<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// END ROT128</span>
			<span style="color: #0066CC;">data</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// handle data as XML</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Partial Encryption</strong><br />
In order to make the process of reversing ROT128 encryption less predictable, you could vary the conditions under which the encryption is to be applied.</p>
<p>Here is an example (alternate bytes):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
	j--; <span style="color: #808080; font-style: italic;">// skip next byte</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span></pre></div></div>

<p>Another example (first 1024 bytes):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> : <span style="color: #cc66cc;">1024</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span></pre></div></div>

<p>Correspondingly, your applications would reverse the encryption under the same conditions.  Effectively, these conditions determine the complexity of your cipher and become the &#8220;secret key&#8221; necessary to decrypt the data.</p>
<p><!-- --><br />
<strong>Conclusion</strong><br />
ROT128 is a weak encryption technique that you should not use to protect sensitive data.  However, the light-weight algorithm can be used to make binary blobs inaccessible to unauthorized parties while allowing your applications to easily restore the data when needed.</p>
<p>This method of encryption may seem amateurish, but it does what it is supposed to do well enough while remaining light-weight and flexible.</p>
<p>It worked well enough for Julius Caesar, after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Simple Editor For Loading And Saving XML As Binary</title>
		<link>http://www.ghostwire.com/blog/archives/as3-simple-editor-for-loading-and-saving-xml-as-binary/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-simple-editor-for-loading-and-saving-xml-as-binary/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 02:35:22 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1312</guid>
		<description><![CDATA[This post is a supplement to &#8220;Saving XML As Binary&#8221;. As mentioned in the previous post, you can save XML in binary and get the benefits of compression, but doing so would make it impossible to edit the data through a text editor. This post explores a simple tool you can create easily using Flash/ActionScript [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>.</p>
<p>As mentioned in the previous post, you can save XML in binary and get the benefits of compression, but doing so would make it impossible to edit the data through a text editor.</p>
<p>This post explores a simple tool you can create easily using Flash/ActionScript 3, a tool that will allow you (or your clients and end-users) to load, read, edit and save XML in compressed binary format.  As shown below, this is a minimalist approach &#8211; feel free to beautify and/or customize it to fit your own requirements.</p>
<p>You can find the source code at the end of this post.  The code does not use any UI component, not from Aspire UI, Flex or Flash, just plain native Flash Player classes so you can compile the AS3 class alone without any additional library.</p>
<p><span id="more-1312"></span><br />
(480&#215;480 SWF, 2KB)<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_BinaryXMLTool_497127671"
			class="flashmovie"
			width="480"
			height="480">
	<param name="movie" value="/swf/BinaryXMLTool.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/swf/BinaryXMLTool.swf"
			name="fm_BinaryXMLTool_497127671"
			width="480"
			height="480">
	<!--<![endif]-->
		
<p><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>
<ul>
<li>You need Flash Player 10+ to view the above SWF.</li>
<li>Click the &#8220;Browse&#8221; button to open a native local filesystem dialog &#8211; choose an XML file to load.</li>
<li>Once loaded, the XML will be displayed in the input text field above.</li>
<li>After that, click the &#8220;Save&#8221; button to open a native local filesystem dialog &#8211; choose location and file name.</li>
<li>Look for the saved file in your local storage &#8211; notice that the file size is much smaller than the original.</li>
<li>If you open the saved file in a text editor, you will see garbage text because it is in compressed binary format.</li>
<li>You can load a binary XML file into the above tool (if it was saved in &#8220;.xml&#8221; extension).</li>
<li>You can edit the XML in the text field before saving.</li>
</ul>
<p><!-- --><br />
<strong>BinaryXMLTool Class</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileFilter</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileReference</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">xml</span>.<span style="color: #006600;">XMLDocument</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BinaryXMLTool <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** minimalist buttons **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> browseButton:<span style="color: #0066CC;">TextField</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> saveButton:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** minimalist editor **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xmlText:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** browse/load/save **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> xmlFile:FileReference;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BinaryXMLTool<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">else</span> addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// entry point</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span>			= StageAlign.<span style="color: #0066CC;">LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span>			= StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">showDefaultContextMenu</span>	= <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist text editor **</span>
			xmlText				= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			xmlText.<span style="color: #0066CC;">multiline</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			xmlText.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			xmlText.<span style="color: #0066CC;">backgroundColor</span>		= 0xEEEEEE;
			xmlText.<span style="color: #0066CC;">type</span>			= TextFieldType.<span style="color: #006600;">INPUT</span>;
			xmlText.<span style="color: #0066CC;">width</span>			= <span style="color: #cc66cc;">400</span>;
			xmlText.<span style="color: #0066CC;">height</span>			= <span style="color: #cc66cc;">400</span>;
			xmlText.<span style="color: #0066CC;">border</span>			= <span style="color: #000000; font-weight: bold;">true</span>;
			xmlText.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - xmlText.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			addChild<span style="color: #66cc66;">&#40;</span>xmlText<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist browse button **</span>
			browseButton			= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			browseButton.<span style="color: #0066CC;">autoSize</span>		= TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			browseButton.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			browseButton.<span style="color: #0066CC;">backgroundColor</span>	= 0x000000;
			browseButton.<span style="color: #006600;">defaultTextFormat</span>	= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Tahoma&quot;</span>, <span style="color: #cc66cc;">14</span>, 0xFFFFFF, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
			browseButton.<span style="color: #0066CC;">selectable</span>		= <span style="color: #000000; font-weight: bold;">false</span>;
			browseButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;BROWSE&quot;</span>;
			browseButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - browseButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			browseButton.<span style="color: #006600;">y</span>			= <span style="color: #cc66cc;">420</span>;
			addChild<span style="color: #66cc66;">&#40;</span>browseButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist save button **</span>
			saveButton			= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			saveButton.<span style="color: #0066CC;">autoSize</span>		= TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			saveButton.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			saveButton.<span style="color: #0066CC;">backgroundColor</span>	= 0xAAAAAA;
			saveButton.<span style="color: #006600;">defaultTextFormat</span>	= browseButton.<span style="color: #006600;">defaultTextFormat</span>;
			saveButton.<span style="color: #0066CC;">selectable</span>		= <span style="color: #000000; font-weight: bold;">false</span>;
			saveButton.<span style="color: #0066CC;">text</span>			= <span style="color: #ff0000;">&quot;SAVE&quot;</span>;
			saveButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - saveButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			saveButton.<span style="color: #006600;">y</span>			= <span style="color: #cc66cc;">460</span>;
			addChild<span style="color: #66cc66;">&#40;</span>saveButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** button listeners **</span>
			browseButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, on_buttonClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			saveButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, on_buttonClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse or save
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">TextField</span> = evt.<span style="color: #0066CC;">target</span> as <span style="color: #0066CC;">TextField</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;BROWSE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					xmlFile = <span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					xmlFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_xmlSelect, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
					xmlFile.<span style="color: #006600;">browse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> FileFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;XML Documents&quot;</span>,<span style="color: #ff0000;">&quot;*.xml&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;SAVE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>xmlFile<span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#123;</span>
						<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>xmlText.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>
						<span style="color: #66cc66;">&#123;</span>
							<span style="color: #808080; font-style: italic;">// ** saving as binary **</span>
							<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlText.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;bin&quot;</span> + xmlFile.<span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span>;
						<span style="color: #66cc66;">&#125;</span>
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse, load XML file
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_xmlSelect<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			xmlFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_xmlSelect<span style="color: #66cc66;">&#41;</span>;
			xmlFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_xmlComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			xmlFile.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle load, check if it is binary, uncompress, display XML in editor
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_xmlComplete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			xmlFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_xmlComplete<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			saveButton.<span style="color: #0066CC;">backgroundColor</span> = 0x000000;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #66cc66;">*</span> = FileReference<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is ByteArray<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">try</span>
				<span style="color: #66cc66;">&#123;</span>
					ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
				<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
			xmlText.<span style="color: #0066CC;">text</span> = <span style="color: #0066CC;">data</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-simple-editor-for-loading-and-saving-xml-as-binary/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>[AS3] Saving XML As Binary</title>
		<link>http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 07:44:53 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1032</guid>
		<description><![CDATA[This could be useful if you have an external large, verbose XML file which your application must load during run-time. By saving the XML as binary, you can compress the data and get a much smaller file. Of course, the amount of compression you can get depends on the complexity of your data, but it [...]]]></description>
			<content:encoded><![CDATA[<p>This could be useful if you have an external large, verbose XML file which your application must load during run-time.  By saving the XML as binary, you can compress the data and get a much smaller file.  Of course, the amount of compression you can get depends on the complexity of your data, but it would typically be over 50% (conservative estimate).</p>
<p>Admittedly, having the XML data in compressed binary format contradicts the original intention of using XML in the first place &#8211; to allow human-readable data.  Therefore, you will have to decide what exactly is important for your application before proceeding.  Perhaps having cryptic external data <code>is</code> indeed what you want &#8211; allowing data to be externalized so they can be changed without requiring the SWF to be recompiled, and yet would prefer the data to be non-human-readable.</p>
<p><span id="more-1032"></span><br />
<strong>Converting XML To Binary Data (ByteArray)</strong><br />
First, we look at how to store the XML data in a <code>ByteArray</code>.  The following code assumes that you already have the XML data stored as a <code>String</code> value in a variable named &#8220;xmlData&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// xmlData is original XML string</span>
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And that does it &#8211; the XML data is now in compressed binary.</p>
<p><!-- --><br />
<strong>Saving to File</strong><br />
Now that you have the binary data as a <code>ByteArray</code> object, you can save that object to file as raw data.  You can do that either by<br />
(i) posting to a server script;<br />
(ii) using AIR API to save to local storage; or<br />
(iii) using Flash Player 10 API to save to local storage.</p>
<p>We will look at (iii), because that is most accessible to every Flash developer.  The class you will use is <code>flash.net.FileReference</code>.  The <code>FileReference.save()</code> method is available when targeting Flash Player 10 &#8211; it allows you to save the <code>data</code> in the <code>FileReference</code> object to a local file.</p>
<p>As a security measure, the <code>save()</code> method will only work in the Flash Player if you call it in response to a user event (for example, <code>MouseEvent.CLICK</code> event).  Therefore, you need to create a button, attach a listener to it, and call the <code>save()</code> method in the listener.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// xmlData is original XML string</span>
	<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;bindata.xml&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;bindata.xml&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The <code>save()</code> method will open a native dialog box &#8211; choose a file name and location and you will save the XML data in your local filesystem.  Notice that you can save the file in the &#8220;.xml&#8221; extension, but you will no longer get a human-readable XML file &#8211; you will see garbage text if you open the file in a text editor because the data is in compressed binary.</p>
<p><!-- --><br />
<strong>Loading Binary XML</strong><br />
You can load the binary file the same way you would a normal XML file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:URLLoader	= <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">dataFormat</span>	= URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>; <span style="color: #808080; font-style: italic;">// ** make sure you do this **</span>
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_XML<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, on_XML<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToXMLFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Therefore, the code for loading is the same as for normal text XML files, except that you need to set <code>dataFormat</code> to <code>URLLoaderDataFormat.BINARY</code>.  Even if you end up loading a normal text XML file, the above code will work just fine.</p>
<p><!-- --><br />
<strong>Converting Binary Data To XML</strong><br />
After the binary XML file is loaded, you convert the binary data back to text XML.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> on_XML<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">type</span> == Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #66cc66;">*</span> = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is ByteArray<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		xmlData = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>With the code above, your application will be able to handle both text XML and binary XML files.  Therefore, this code is something you can use even if you are not currently using XML stored as compressed binary data.  If at a later time you decide to compress your XML in binary format, the application will be able to handle it just fine.</p>
<p>In the next post, we will look at a simple tool you can create easily using Flash/ActionScript 3 to load, read, edit and save XML files in compressed binary format.</p>
<p><!-- --><br />
<strong>Note:</strong><br />
The above technique can be used for any text file, not just XML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

