Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 25th, 2012, 05:50 AM
Ralms Ralms is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 2 Ralms User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 27 sec
Reputation Power: 0
Question ActionScript 3 - 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.

Reply With Quote
  #2  
Old July 25th, 2012, 06:39 AM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 10 m
Reputation Power: 3163
Facebook MySpace
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.
__________________
Quis custodiet ipsos custodes?

Reply With Quote
  #3  
Old July 25th, 2012, 06:52 AM
Ralms Ralms is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 2 Ralms User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 27 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > ActionScript 3 - Managing XML in a class

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap