<?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>alecmce.com &#187; xml</title>
	<atom:link href="http://alecmce.com/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>http://alecmce.com</link>
	<description></description>
	<lastBuildDate>Fri, 18 May 2012 02:54:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>XML Constants Gotcha</title>
		<link>http://alecmce.com/as3/xml-constants-gotcha</link>
		<comments>http://alecmce.com/as3/xml-constants-gotcha#comments</comments>
		<pubDate>Thu, 13 Aug 2009 12:11:03 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[mutable]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=259</guid>
		<description><![CDATA[In my head, XML slips between the cracks of being an Object and a primitive type. It shouldn&#8217;t, and when I&#8217;m concentrated it doesn&#8217;t. But still, there is something primitive-type about it. and are intuitively the same sorts of things. XML is like an advanced sort of String. We encode it as we encode text, [...]]]></description>
			<content:encoded><![CDATA[<p>In my head, XML slips between the cracks of being an Object and a primitive type. It shouldn&#8217;t, and when I&#8217;m concentrated it doesn&#8217;t. But still, there is something primitive-type about it.</p>
<pre class="brush: as3; title: ; notranslate">private const DATA:XML = &lt;data message=&quot;Hello World!&quot; /&gt;;</pre>
<p>and</p>
<pre class="brush: as3; first-line: 2; title: ; notranslate">private const DATA:String = &quot;Hello World!&quot;;</pre>
<p>are intuitively the same sorts of things. XML is like an advanced sort of String. We encode it as we encode text, and we convert it to and from Strings without difficulty. I don&#8217;t construct XML, at least it doesn&#8217;t <em>feel</em> like I do. In my preferred coding environment (<a href="http://fdt.powerflasher.com/">FDT</a>) by default they&#8217;re even similarly coloured.</p>
<p>However, they are clearly not the same, because XML is mutable:</p>
<pre class="brush: as3; first-line: 3; title: ; notranslate">DATA.@message = &quot;This message has been changed!&quot;;</pre>
<p>Needless to say, that doesn&#8217;t work on a String! Somehow by labelling the XML a &#8216;constant&#8217; I expect the XML to be immutable.</p>
<p>On reflection I know this about XML, but it <em>feels</em> wrong. This &#8216;gotcha&#8217; does not identify a language bug, but suggests a mental corrective: when you think about XML, make sure that you don&#8217;t mistake it for a primitive type. When I&#8217;m not concentrating I do, and it occassionally trips me up.</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/xml-constants-gotcha/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>XML Gotcha &#8211; new XML(null)</title>
		<link>http://alecmce.com/as3/xml-gotcha-new-xmlnull</link>
		<comments>http://alecmce.com/as3/xml-gotcha-new-xmlnull#comments</comments>
		<pubDate>Thu, 06 Aug 2009 17:20:52 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=175</guid>
		<description><![CDATA[Without trying it, how would you expect this to behave? Personally I don&#8217;t think that constructing XML from a null object should work, but it does. I would have expected an error, like if I try to do this: That&#8217;s about as clear a case of error throwing as you can get, I think. Why [...]]]></description>
			<content:encoded><![CDATA[<p>Without trying it, how would you expect this to behave?</p>
<pre class="brush: as3; title: ; notranslate">var xml:XML = new XML(null);</pre>
<p>Personally I don&#8217;t think that constructing XML from a null object should work, but it does. I would have expected an error, like if I try to do this:</p>
<pre class="brush: as3; title: ; notranslate">var xml:XML = new XML(&quot;&lt;bad&gt;hello world&lt;/tags&gt;&quot;);</pre>
<p>That&#8217;s about as clear a case of error throwing as you can get, I think. Why not for null?</p>
<p>It turns out that null is interpreted as a sort of weird XML singularity. It has one node which has no name, to which you can add attributes and children. However much you add, always the length is <code>1</code> and the <code>toXMLString()</code> is an empty string. Happily, this latter fact can save you from being consumed by the singularity: you can test for it by checking whether</p>
<pre class="brush: as3; title: ; notranslate">xml.toXMLString() == &quot;&quot;;</pre>
<p>and take appropriate steps if it does!</p>
<p>I haven&#8217;t thought through all the consequences of this behaviour but it took me completely by surprise&#8230; if you can explain to me why Adobe considered this the preferred behaviour, please let me know! This is another of those hard-to-spot XML bugs like <a href="http://alecmce.com/as3/gotcha-booleans-in-xml">this one I found earlier</a>. Someone, <a href="http://blog.vizio360.co.uk/2009/08/parsing-xml-good-practice/">preferably Simone since he&#8217;s already on it</a>, should wrap fixes for this stuff into a helper class!</p>
<h3>Update</h3>
<pre class="brush: as3; title: ; notranslate">var xml:XML = new XML(null);</pre>
<p>is &#8211; of course &#8211; the same as</p>
<pre class="brush: as3; title: ; notranslate">var xml:XML = new XML();</pre>
<p>The <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/">documentation</a> doesn&#8217;t indicate that you can pass an empty value into the XML constructor, but as <a href="http://vizio360.co.uk">my colleague Simone</a> pointed out, playerglobal.swc indicates that you can. The documentation meanwhile says that the constructor parameter for an XML object should be</p>
<p>
<blockquote>Any object that can be converted to XML with the top-level XML() function.</p></blockquote>
<p>and the top-level XML function documentation states that if you pass a null into it:</p>
<p>
<blockquote>null: A runtime error occurs (TypeError exception).</p></blockquote>
<p>However, that doesn&#8217;t throw an error either!</p>
<p>I started this update having seen the playerglobal.swc definition about to eat some humble pie, but this further investigation has served to strengthen my understanding of how weird this behaviour is. Be warned, this stuff is out there waiting to spoil your afternoon&#8217;s coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/xml-gotcha-new-xmlnull/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

