onClose is an inbuilt flash method (for xmlSocketServer as you said) and it Does turn blue, mine is onClosed though
I know yours in onClosed, but I was saying that the only information I could find was onClose and was for XMLSocetServer . I just worded it wrong⌠like I do a lot.
maybe I should have named it
DoThisWhenTheMenuIsClosed
naming can add a lot of meaning to the code⌠there are âinternalâ meanings - words that I personally would use to describe things, which may or may not be clear to other people. Things can also get confusing with all the menu variables heh. Ill have to keep that in mind on the next thing I do
actually I had a request to explain setInterval so I think thats next on the list⌠and I might come back and get into more XML here - not just editing but loading and using - explaining that a little more since this thread so far has explained what XML is and then how its edited in Flash but not really the methods of getting it into flash (or getting out for that matter).
Any extra info on XML is greatly welcomed.
I am moving this thread to the Best Of Kirupa section now
[size=4]XML to and from Flash[/size]
Previously discussed was a brief explanation of what XML was and how it can be manipulated within Flash. What wasnt discussed was how exactly you get existing XML, on the server for example, into Flash in the first place. If you were adept enough to follow through with the whole XML editing broo-haha, then this should be a breeze. Otherwise it should still be fairly easy to understand as it is typically the intial steps taking in learning XML-Flash integration anyway; and the act of loading XML into flash isnt difficult to understand anyway.
[size=4]XML to Flash[/size]
So youve got some XML laying around in the same directory as your flash movie and you want your flash movie to take advantage of that. What can you do? Load it in, thats what. Here are the steps to loading XML into Flash.
- Make a new XML object
my_xml = new XML(); - Load the XML url (where your XML file is) using the LOAD command.
my_xml.load(âmyXMLfile.xmlâ);
and thats it! Give it a second or two (depending on connection speed and size of the xml file) and your XML is in Flash ready to use and manipulate! Easy enough, right? The tricky thing is the previous statement, the one starting with âGive it a second or twoâŚâ Thats where you may run into some trouble.
Scripts in Flash are run âinstantaneouslyâ each frame they are called. Ok, not exactly, since your processor cant handle more than one instruction in any one instance (depending on how you define instance). So its not literally instantaneous but it is more or less all at once, as fast as Flash can - and thats fairly fast. The internet, or even harddrives for that matter, arent quite as fast, and at times can be downright slow, espeically those poor saps still on modems (such as myself). What that means is in any one block of code, if you attempt to load in anything from an external file, such as an XML file, there will be a delay before you can actually access information from that file. For example. Lets say you have a simple XML file called âinfo.xmlâ that has the following
**<INFO NUMBER=â5â>
In loading that file into Flash, there will be a dealy before it is actually within and referencable from Flash in actionscript. Consider the following
my_xml = new XML();
my_xml.load("info.xml");
trace(my_xml.toString());
the trace will be blank because the info.xml file hasnt had time to actually be loaded into Flash. Fear not, there are ways around this. The best one is through using xml.onLoad.
What onLoad is, is a function which gets run by an XML object after that XML object has fully loaded whatever external XML file it was instructed to load and has been able to parse (or understand) the loaded data. This is very similar to onEnterFrame and onMouseDown events except that this event is, instead, associated with the event of XML information being fully loaded and parsed within an XML object. It is within this function which you would put whatever operations you need to be run that use your loaded XML information. For instance, to get the previous example to fully trace the string representation of the XML, you would use onLoad like so:
my_xml = new XML();
my_xml.onLoad = function(){
trace(this.toString());
}
my_xml.load("info.xml");
Now, only when âinfo.xmlâ has fully loaded will the trace be called. Note that âthisâ is used within the function to represent the XML object. Thats because the scope of the function is whithin that object itself, so âthisâ within that onLoad references my_xml.
Also be aware that you dont have to define onLoad itself directly as a function. You can easily set it to be an existing function by saying:
my_xml.onLoad = someOtherFunction;
and you can change onLoad at anytime, for instance if you needed a new set of operations to be associated with the loading of new XML.
Success. Something I didnt mention earlier was a special argument that gets passed to an onLoad call called âsuccessâ. This argument is true or false depending on whether or not the XML document requested in the load call was found and successfully loaded. The success argument is true if the document was loaded successfully and false if not. Example:
my_xml = new XML();
my_xml.onLoad = function(success){
if (success){
trace(this.toString());
}else{
trace("There was an Error in loading the XML document");
}
}
my_xml.load("info.xml");
Though this argument is not necessary, it is suggested that you make full use of it to keep your actionscript aware and robust.
Something else that happens with an XML objectâs onLoad is the setting of an XML property - loaded. The loaded property that, on the load call is set to false and then becomes true with a successful onLoad. In essense, when the onLoad is called, the success parameter is saved into the xml object itself as the loaded property. If success is false, loaded will be false, since afterall the XML document didnt load. If there was success, loaded will be true. You can actually see this in the pre-set definition of the XML onData function as seen here:
XML.prototype.onData = function (src) {
if (src == undefined){
this.onLoad(false);
}else{
this.parseXML(src);
this.loaded = true;
this.onLoad(true);
}
}
onData is the real event that is called when the XML object has fully loaded its requested XML document. Its this function (which is defined as the above for all of your XML objects unless you specifically over-ride it by writing your own) which actually calls the onLoad function and sets the value of the XML objectâs loaded property. The src argument is the actual text of the XML file itself. Once that text has loaded its passed into onData (just like onData for a LoadVars object) and onData checks to see if src is defined or not. When it wont be defined is when theres an error in downloading the XML document. When thats the case, onLoad is called and passed a false (false for the success argument in onLoad). Otherwise, the XML data is parsed into the XML object so the XML object understands it, loaded is set to true and onLoad is called with a success argument of true. Chances are youâd never worry about changing or setting the onData function of your XML object since it is already made for you. All you need to do is write an onLoad to handle the XML object once the loaded XML has been fully loaded and parsed correctly into the XML object.
Also like a LoadVars object, XML objects have getBytesLoaded and getBytesTotal properties which let you check to see how much of how much of your XML document has been loaded. If your document is extremely large, you might even want to build a preloader for it
One other thing you might want to be aware of in loading XML into Flash is the status property. This is like a version of loaded but for the XML parsing, not loading. This tells you any errors that might have occured in the process of parsing an XML document to be understood properly by flash. The following outlines the values status can contain and what they mean:
-0 No error; parse was completed successfully.
-2 A CDATA section was not properly terminated.
-3 The XML declaration was not properly terminated.
-4 The DOCTYPE declaration was not properly terminated.
-5 A comment was not properly terminated.
-6 An XML element was malformed.
-7 Out of memory.
-8 An attribute value was not properly terminated.
-9 A start-tag was not matched with an end-tag.
-10 An end-tag was encountered without a matching start-tag.
I personally have never used status outside of debugging, but it could be very useful for keeping your Flash movie robust, especially in dealing with possibly ignorant clients who might write flicted code for your Flash movie powerhouse
[size=4]XML from Flash[/size]
Once you have loaded your XML into Flash, made sure it was loaded ok, made sure it parsed without a hitch, then maybe manipulated it some and called it a day, you then have the option of sending that XML back to the server from whence it came. You can have two general reasons for doing this.
[list]
[] to update an existing (or create a new) XML document to reside on the server
[] to initiate a server-side script which will read the passed data and perform an action, most likely returning data back to flash.
[/list]
The Flash side of this is not so hard at all. In fact there are only 2 methods for handling this and one is more or less the other with an added feature. What could be difficult is the server-side of it all - something which I wont get into here; thats what the server-side scripting forum is for
Those 2 functions mentioned above are
XMLobj.send(âurlâ);
XMLobj.sendAndLoad(âurlâ, targetXMLobject);
sendAndLoad is virtually send but allows you to put the server-returned data into another XML object in Flash as though it was loaded through calling the load function of that XML object, hense âsendAndLoadâ name.
For reason 1, sending XML to the server to be saved, send is most likely your method of choice. Simply, it sends the all the XML data of that XML object properly encoded to the server as text and allows whatever server-side script you put in the specified URL do its thing with that XML. With reason 1, that would be saving the XML as a file.
Reason 2 could also be used with send, but you also have here, the option to use sendAndLoad. This will send the XML data of the current XML object to the server then send the resulting XML data into the targetXMLobject to be parsed and stored there.
Both send and sendAndLoad also have optional parameters window which operates as a getURL target. The window specifies the window which the url is loaded into hense, where a browser responce can be seen. Examples:
my_xml = new XML("<**NODE>Value<**/NODE>");
i_xml = new XML();
i_xml.onLoad = function(){
_root.output.text = this.toString();
// WARNING: when working in Flash, sent XML data is transfered through
// GET and NOT POST which can interfer with your XML handling. You may
// want to test via the server and not in the Flash authoring environment
}
my_xml.sendAndLoad("xmlScript.php", i_xml);
Note Both send and sendAndLoad normally use POST as the method of information transfer (see Warning).
⌠if you are unsure of your XML server-side scripting skills or have little knowlesge in working with XML server-side, you can also (which may be the easiest thing to do for Reason 1) send your XML as a string through loadVariables associating it with an identifiable variable name which can then be easily referenced and used within your script.
Here is an updated XML driven menu, Ill go through its workings step by step in a following post:
Wow, that is excellent Sen.
â â â â your skillsâŚlol.
[size=4]XML Menu Example Explained[/size]
First the XML of the menu
<**?xml version="1.0"?>
<**MENU>
<**ITEM NAME="Menu Dropdown 1">
<**SUBITEM NAME="Submenu Link 1" URL="sublink11.html" />
<**SUBITEM NAME="Submenu Link 2" URL="sublink11.html" />
<**/ITEM>
<**ITEM NAME="Menu Dropdown 2">
<**SUBITEM NAME="Submenu Link 1" URL="sublink21.html" />
<**SUBITEM NAME="Submenu Link 2" URL="sublink22.html" />
<**SUBITEM NAME="Submenu Link 3" URL="sublink23.html" />
<**SUBITEM NAME="Submenu Link 4" URL="sublink24.html" />
<**/ITEM>
<**ITEM NAME="Menu Link 1" URL="link1.html" />
<**ITEM NAME="Menu Link 2" URL="link2.html" />
<**ITEM NAME="Menu Dropdown 3">
<**SUBITEM NAME="Submenu Link 1" URL="sublink31.html" />
<**SUBITEM NAME="Submenu Link 2" URL="sublink32.html" />
<**SUBITEM NAME="Submenu Link 3" URL="sublink33.html" />
<**/ITEM>
<**ITEM NAME="Menu Link 3" URL="link3.html" />
<**/MENU>
This is pretty straightforward, especially if youâve been following along with the XML explanations earlier. What we have is a main XML node in the XML called MENU which contains ITEM nodes. Now of these ITEM nodes we see two variations. 1)ITEM nodes which have no children and contain NAME and URL attributes, and then there are 2) the ITEM nodes which only contain a single attribute, NAME but also contains child nodes SUBITEMs which are like the forementioned ITME nodes having the NAME and URL attributes. Essentially, theses SUBITEM nodes are the first type of ITEM nodes, they are just contined within another ITEM node. It is these SUBITEMS which make our drop-down list in the example.
In terms of what XML is, it has done its job well, making it fairly easy to simply look at this file in a text editor or even on your screen here) and see exactly what it means and will accomplish.
What we will do in Flash is read this XML in using an XML object and the load method, then, using the onload event, take this gathered information and generate (dynamically) our menu. The menu will be made up of as many sections as there are ITEMS. Any one of those ITEMS which has a URL will be made as a link button, while the other ITEMS with SUBITEM nodes will be made as dropdown menus containing link buttons for each of those SUBITEMS with the appropriate display name and URL.
In this example, by far the easiest part is the XML itself. Its clear how this works and how it should interact with Flash to provide it the correct information. The hardest part is getting through the actual menu operation code. The difficulty here is the fact that is is completely dynamic and constructed completely within the onLoad event of the XML object, being created once the information has been successfully read into flash. As for the XML specifically, conceptiually I will lay that out in this post getting into further menu detail in a follwoing post (if/when I ever feel so ambitious ;)).
Once the XML is loaded into the XML object, myXML, it needs to be read and understood correctly to generate the menu. So what do we need to know how to make a menu? Well off the top of my head Im thinking:
[list]
[] How many menu items there will be
[] The names of each menu item
[] What that menu item will do (link or open submenu)
[] - if a submenu opening item, how many submenus to attach
[/list]
All this information is contained within the XML file. All we need to do then is extract it.
myXML.firstChild is <**MENU>. myXML.firstChild.childNodes is the array containing the ITEM nodes. With that array we can easily get our number of items just by checking its length
myXML.firstChild.childNodes.length
Great! now we have our menu items! All we would need to do then in Flash is loop through that many number of times and attach a button for each menu item. Fairly easy. Now, within that loop, every time you attach a button youâll need to assign to it its name and its url (if it has one) and we get that through the attributes of that item. For example, you can see this using the following loop:
for (i=0; i< myXML.firstChild.childNodes.length; i++){
menu_mc.attachMovieClip("menuitemID","menuItem_mc"+i, i);
menu_mc.name_txt.text = myXML.firstChild.childNodes[**i].attributes.NAME;
menu_mc.url = myXML.firstChild.childNodes[**i].attributes.URL;
}
This loop goes through for each ITEM in the <**MENU> node of the XML and attaches a movieclip and then assigns to theat moveiclip the NAME (input to a textField) and URL (just as a variable).
But what if the ITEM isnt a link item? Well then, wed first check for childNodes of that ITEM node. If they exist, then we wouldnt go through and give that menu item a URL, but rather give it a command to open the submenu - which itself Does exactly what is above, but just for the submenu. So the process is:
[list]
[] Loop through all ITEM nodes
[] If ITEM has no child nodes, assign it its own NAME and URL
[] If ITEM has children, give it its NAME and assign it an action to oepn a submenu and not a link
[] In creating a submenu (which is actually created at the time the sub menu item is clicked) perform the same process used to create your non submenu ITEMs to give the submenu ITEM names and functionality
[/list]
The details of making that happen, especially in the manner of the example can get a little overwhelming, as seen in the code. Understanding atleast how the XML fits in, doesnt have to be.
I get it⌠you have been a great help for me in learning XML in Flash Sen. It doesnât as difficult as people make it out to be.
Unless it gets more difficult depending on what your doing. I am up for any challenege in it though
Hi Lost/ Se, yerr I know this is an old thread, but I was going through the AS of the latest version of the xml menu and I was having a little trouble understanding the logic of the menu creation. Could someone plz summarise the logic ? Ie the AS and the function calls.
Thanks:P
heh, anything specifically? It can get a little involved (which is why I havent gotten around to explaining it all)
LOL, yes it looks quiet involved thats for sure!. I even noticed the use of â?â in some of the condtionals which I know is deffintly a âadvanced shortcutâ for people like you :P. Hmm well, iâll go through it again and see which parts I understand least and go from thereâŚbe back with my reply soon.
Actually Se eventually I want to be able to construct something like this
http://www.flashcomponents.net/component.cfm?nav=2&id=240
by myself (very nice isnt it?)
ah well there ya go, all packaged in a pretty component, you dont need to re-write it yourself
LOL, yes it is prettyâŚBut Im not a huge fan of components. They muck with preloaders and its somebody elseâs blodd sweat and tears :P. âŚI would rather the satisfaction of my own work, and the learning process on the way. Wouldnt you?
-As for getting back to you on which parts of the menu I dont understand Im to full with Univeristy study, as I have exams on atm