#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Question Managing XML in a class


    Hi there everyone,

    Ive been doing a school work that requires the usage of XML all the time. And I though that the best way could be to have it in a class althought Im still learning and getting practice with classes.

    Im a newbie programmer trying to do stuff the rigth way and well optimized although that sometimes is hard XD

    So what I've tried was having the XML in main, parse everything to Arrays and then use the arrays as functions needed it. But I was wondering if that was the best practice.

    So now Im trying to have a class managing everything related to XML. So in the constructor he loads etc, and then the idea was to have functions that would return the values required in Strings.

    This is what I have so far in the XML class.
    Code:
    package { 
    
    import flash.net.URLRequest; 
    import flash.net.URLLoader; 
    import flash.display.Loader; 
    import flash.events.Event; 
    import flash.events.IOErrorEvent; 
    import flash.events.ProgressEvent; 
    
    public class xmlLoader { 
    
    var xmlData:XML = new XML(); 
    
    public function xmlLoader() 
    { // constructor code 
    trace("xmlLoader contructor."); 
    var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlError);
    xmlLoader.addEventListener(ProgressEvent.PROGRESS, progressListener);
    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    xmlLoader.load(new URLRequest("dados.xml")); 
    } 
    
    private function progressListener(e:ProgressEvent):void 
    { 
       trace("Loading XML " + Math.round((e.bytesLoaded*100)/e.bytesTotal) + " %"); 
    } 
    
    private function xmlError(e:IOErrorEvent):void 
    { 
       trace("IOError at the XML"); 
    } 
    
    private function xmlLoaded(e:Event):void 
    { 
       trace("XML loaded"); 
       xmlData = new XML(e.target.data); 
       for each (var item:XML in xmlData.elements()) 
       { 
          recursive(item); 
       } 
    } 
    
    public function recursive(xml:XML) 
    { 
       trace("NAME: "+xml.name()); 
       if (xml.children()==xml) 
       { 
          trace("VALUE: "+xml.children()); 
          trace("-----------------"); 
       }
       else 
       { 
          trace("This node has children:"); 
          for each (var item:XML in xml.children()) 
          { 
             recursive(item); 
          } 
       } 
    } 
    }
    Can you guys tell me how can I access the data later with functions etc??

    Another question is what is the diference between XMLList and XML?

    Thanks for all,
    Ralms.
  2. #2
  3. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    Hi, a really good XML/XMLList guide is here by Senocular.
    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.
    Quis custodiet ipsos custodes?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0
    Hi there,

    thanks a lot for the quick reply.

    About the listeners I allways remove them, only those in specific I was in doubt and I dont see people remove them that oftem. At the lest the Complete event one I think it removes it self in the end. Anyway I will remove them just to be sure.

    About what Im doing with the XML:

    Im creating an "engine" that creates a virtual tour over my school. So what it does is gets all the videos, pictures paths and what not, from the XML. As well it checks the XML for destinations, arrows direction, etc.
    So in the tour the user gets this process:

    1st: picture with arrows and he chooses the next destination from the arrows.
    2nd: The "engine" removes everything, go to the XML to check the destination, loads a small transition video and gets the next picture, arrow locations, direction, etc.

    And it loops all the functions over and over again until the user gets bored and gets back to the menu or something.

    So basically I access the XML data all the time.

    My first version of this work was working fine in a local machine, because I wasnt using any loading control over it. I would just load. And I was doing every, I mean EVERYTHING in the first frame of the fla project.

    I will take a look at the tutorials later and I will see what I can do.

    Thanks for all.

IMN logo majestic logo threadwatch logo seochat tools logo