Hi, a really good XML/XMLList guide is
here by Senocular.
Quote:
| Originally Posted by Senocular XML instances contain one root node which can contain any number of child nodes. XMLList instances are a collection or list of XML instances or XML nodes. |
Deciding which to use depends on what you are doing, plus a lot of the functions return XMLLists. It's not really a case of choosing which to work with, you'll more than likely be working with both.
AS3 has a really powerful built in system for accessing XML data called E4X. I won't get into it too much as other people have explained it far better than I could. One such resource which has good examples is
this guide on Sephiroths site. Note that E4X is not just limited to AS3 it is used in several ECMAScript based languages like C# and JavaScript.
So an example of using it in a function:
Code:
public function loadTitle(xml:XML):String
{
return xml.channel.title.text();
}
Make sure to remove the event listeners when you are done with them:
Code:
private function xmlError(e:IOErrorEvent):void
{
xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, xmlError);
xmlLoader.removeEventListener(ProgressEvent.PROGRESS, progressListener);
xmlLoader.removeEventListener(Event.COMPLETE, xmlLoaded);
trace("IOError at the XML");
}
private function xmlLoaded(e:Event):void
{
xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, xmlError);
xmlLoader.removeEventListener(ProgressEvent.PROGRESS, progressListener);
xmlLoader.removeEventListener(Event.COMPLETE, xmlLoaded);
Wrapping the whole lot in its own class could be overkill. Say I make a slideshow that loads a list of image filenames from an XML file. I only need to load the XML file once. I would just have to the code to do that in my main class. Wrapping basic things can add overhead to your application as you are putting extra steps in there which can affect performance if you are heavily calling those functions over time. Then once I'm done with the XML, say after all the slideshow images have been loaded, I can remove references to the XML object such as event handlers, leaving it free to be cleared by the garbage collector.
On the other hand, if you are going to be reloading the XML frequently and then performing actions against it that you don't really want cluttering up your main class then wrapping it up is a good idea. Say for instance each time you get the title you want to do some checking with it:
Code:
public function petSounds(xml:XML):void
{
switch(xml.pets.animal)
{
case "dog":
trace("Dogs go quack");
break;
case "cat":
trace("Cats go moo");
break;
case "bird":
trace("Birds go ROOOOAAAARR!");
break;
}
}
Even then though, I'd only really use a wrapper class if I was going to have several functions like this. Sometimes, breaking things up into smaller and smaller pieces sucks, that's just my opinion.