Saturday 11 June 2011

Common Mistakes: XML Namespaces and E4X

From time to time I stumble upon application code that has an unusual way of handling XML namespaces. The most common case is where a regular expression substitution (or similar) is used to strip out namespace declarations and references from the raw XML prior to being parsed into an XML object. Whilst this may function correctly accordion to the defined application behaviour, there are more elegant ways to handle namespaces in E4X. Consider the following XML snippet:

<root xmlns:space="http://www.something.com">
 <space:tag>
  <node>Hello, world!</node>
 </space:tag>
</root>

Assuming that this XML has been parsed into an XML variable named xml, we could determine the contents of the node node using the following code:

namespace space = "http://www.something.com";

var helloWorld: String = xml.space::tag.node.toString(  );

That is, we are able to declare a namespace identifier by using the namespace keyword, and then reference it in our E4X statements to select the appropriate nodes (using the same notation that we would use to reference custom namespaces in ActionScript 3). Note that the identifier we declare does not have to match the one used in the XML document - only the namespace URI has to match. The example below is equally correct:

namespace different = "http://www.something.com";

var helloWorld: String = xml.different::tag.node.toString(  );

Using this technique avoids the need to manipulate XML content before it is parsed; it is simpler and avoids the chance of incorrectly preprocessing the data.

No comments:

Post a Comment