I was scanning twitter yesterday and I saw a note from a frustrated developer who couldn’t find any really good simple example of the difference between XML, XMLList and XMLListCollection. Being the handy and informative (read nosey) guy that I am I decided to respond to the tweet to discuss this.

The problem here is that I only had 140 chars to explain and I only got through XML and XMLList. So I decided to make a quick post and cover this topic with a couple examples.

So lets start at the beginning. Everyone here is gonna have worked with XML before. We are talking about data that is described via nodes and attributes. Nodes are the tag based elements that surround some content. The trick with XML is that there can only be one root node. And by the way that is not optional, you must have a root node. An example of some xml might look like this.

1
2
3
4
5
6
7
8
9
10
<users>
	<user id="1">
		<first>Sam</first>
		<last>Guye</last>
	</user>
	<user id="2">
		<first>Joe</first>
		<last>Schmoe</last>
	</user>
</users>

In this example the users node is our root and each user has an id attribute as well as first and last sub-nodes. So thats great, now you know exactly what you new before. But hey, I have some code for us to build new examples from, so lets carry on.

XMLList is much like an XML object and in fact the two can be interchanged in many places. The trick is that an XMLList does not have the restriction of only having 1 root node. And XMLList can contain XML fragments. So an XML object is an XMLList with a length of one. So to see an example of an XMLList and where it might come from we will use the descendent operator from E4X (..).

So assigning our XML from above to a variable called users and running the operation users..user will yield

1
2
3
4
5
6
7
8
<user id="1">
	<first>Sam</first>
	<last>Guye</last>
</user>
<user id="2">
	<first>Joe</first>
	<last>Schmoe</last>
</user>

Notice now that we have no root node. This is by all accounts a list of xml objects. Or as we have typed it so far and XMLList! Woo hoo! Ok, but there’s more. You can use the descendent operator to go get a list of the first name nodes too! So users..first yields:

1
2
<first>Sam</first>
<first>Joe</first>

And this is something we to populate data based controls like List or ComboBox. So now we are really seeing not only the relationship between the XML and XMLList data types, we are now seeing how powerful they can be. When used together they allow us to search and filter data in our application.

So how does XMLListCollection fit into this? Well XMLListCollection does for XMLList what ArrayCollection does for the Array. That is it implements the ICollectionView interface which includes methods to allow searching and accessing of the data beyond the what is available via ordinal access. The XMLListCollection class also adds the underlying code needed to support binding in the Flex framework.

Ok, not 140 characters but I think I got out what I wanted to say. This should cover the relationship between XML, XMLList, and XMLListCollection for the time being.