Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Sunday, 25 September 2011

Quick Tip: Ignoring XML Namespaces

This is another small follow up to Common Mistakes: XML Namespaces and E4X: sometimes you may wish to completely ignore the namespace information, rather than explicitly traverse it. Consider the following example:

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

It is possible to reference node containing Hello, world! using the following:

var node: XML = xml.*::space.node;

Sunday, 14 August 2011

Quick Tip: Checking for XML nodes with a namespace

This is a small follow up to Common Mistakes: XML Namespaces and E4X: when working with namespaces, calling hasOwnProperty() may not work as expected. Consider the following example:

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

trace( xml.hasOwnProperty( "tag" ) );

In this case, hasOwnProperty() will return false as the 'tag' tag is part of the 'space' namespace. In order to overcome this, use the QName construct, e.g.:

xml.hasOwnProperty( new QName( "http://www.something.com", "tag" ) )

Alternatively, if you have declared a namespace previously, you can do the following:

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

// ...

xml.hasOwnProperty( new QName( space, "tag" ) )

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.