[size=4]Creating and Editing XML in Flash[/size]
Now that you finally know what XML is and how Flash sees XML you can start taking it apart and screwing around with its structure from within Flash. If not then you cheated and skipped the previous sections and need to go back and read them
Straight out, here are the methods used to make/edit your XML. We’ll go through each one and see how they work.
[list]
[]XML Object - new XML(“xml text”)
[]XMLobj.parseXML(“xml text”)
[]XMLobj.createElement(“node name”)
[]XMLobj.createTextNode(“text value”)
[]XMLobj/XMLnode.appendChild(childNode)
[]XMLobj/XMLnode.insertBefore(childNode, beforeNode)
[]XMLobj/XMLnode.cloneNode(deep)
[]XMLnode.removeNode()
[/list]
new XML(“xml text”)
Number 1 is the XML object itself, the constructor - how you make a new XML object to use in Flash. Chances are, if you’ve used XML in Flash, you’ve used this. What you might not have done was passed XML into the constructor to generate an XML object based off that XML text. Example:
my_xml = new XML("<**LETTER><**TO>Senocular<**/TO><**MESSAGE>Get a life<**/MESSAGE><**/LETTER>");
trace(my_xml.firstChild.nodeName); // traces LETTER
trace(my_xml.firstChild.firstChild.nodeName); // traces TO
trace(my_xml.firstChild.firstChild.firstChild.nodeValue); // traces Senocular
Easy! …next
note: using _xml at the end of your XML object name will give you code hints.
XMLobj.parseXML(“xml text”)
All this does is the same thing the XML object constructor does when passed XML text, but instead, it adds the XML to an already existant XML object replacing its current contents with the new.
my_xml = new XML();
my_xml.parseXML("<**LETTER><**TO>Senocular<**/TO><**MESSAGE>Get a life<**/MESSAGE><**/LETTER>");
trace(my_xml.firstChild.nodeName); // traces LETTER
trace(my_xml.firstChild.firstChild.nodeName); // traces TO
trace(my_xml.firstChild.firstChild.firstChild.nodeValue); // traces Senocular
Again, easy. partparseXML, however, can only be used on an XML object and not used on any one specific node. So if you want to parse some XML into an existing XML object, you would need to create a new XML object, and either using the constructor method, or parseXML, load the XML in then clone in or append the results to the current XML object.
XMLobj.createElement(“node name”)
This one is fairly self explanatory. What this does is creates an Element (node) with the given node name. The weird and possibly confusing thing is that this does NOT place the created node into the XML object, this only creates and returns a “free floating” node with the given name which can then be inserted into an XML structure anywhere you want using appendChild(). And yes this is a method of an XML object instance and not the XML object itself.
XMLobj.createTextNode(“text value”)
This is just like createElement but it creates a text node instead with the given text passed in. It too returns a “free floating” node which exists no where until you put it somewhere.
XMLobj.appendChild(childNode)
Here is where all the action comes into play. What this does is adds the passed node into the current XML object. This can be used by the XML object itself or any of its nodes adding the passed node into that specific XML or node object. Example:
my_xml = new XML();
my_xml.appendChild( my_xml.createElement("PICKUP") );
my_xml.firstChild.appendChild( my_xml.createElement("TRUCK") );
trace(my_xml.toString()); // traces <**PICKUP><**TRUCK /><**/PICKUP>
my_xml.firstChild.firstChild.appendChild( my_xml.createTextNode("Drive Me!") );
trace(my_xml.toString()); // traces <**PICKUP><**TRUCK>Drive Me!<**/TRUCK><**/PICKUP>
So basically, what appendChild does is just tacks on the new node onto the end of the childNodes array of the given XML or node object. It then gets assimilated into the XML and becomes one with the XML object
XMLobj.insertBefore(childNode, beforeNode)
insertBefore is similar to appendChild, infact if the beforeNode is undefined or null, insertBefore will actually be equivalent to appendChild. insertBefore just allows you to position where you want a new node object to be placed in your childNodes array. Example:
my_xml = new XML("<**BASEBALLFIELD />");
my_xml.firstChild.appendChild( my_xml.createElement("FIRSTBASE") );
my_xml.firstChild.appendChild( my_xml.createElement("THIRDBASE") );
my_xml.firstChild.insertBefore( my_xml.createElement("SECONDBASE"), my_xml.firstChild.childNodes[1] );
trace(my_xml.toString()); // traces <**BASEBALLFIELD><**FIRSTBASE /><**SECONDBASE /><**THIRDBASE /><**/BASEBALLFIELD>
XMLobj.cloneNode(deep)
This method takes an existing node or XML object, copies it and returns it as a new node. The deep argument determines whether or not all of the child nodes of the cloned node are also copied and cloned. If deep is true, child nodes ARE copied, if false, they are NOT copied. Example:
my_xml = new XML("<**ALIENS><**INVADER>SuperKiller<**/INVADER><**/ALIENS>");
my_xml.firstChild.appendChild( my_xml.firstChild.firstChild.cloneNode(true) );
my_xml.firstChild.appendChild( my_xml.firstChild.firstChild.cloneNode(false) );
trace(my_xml.toString());
// traces <**ALIENS><**INVADER>SuperKiller<**/INVADER><**INVADER>SuperKiller<**/INVADER><**INVADER /><**/ALIENS>
notice how the last invader doesnt have a “SuperKiller” text node in it. This is because the deep argument was false on the second clone meaning no children were copied over into the returned clone node.
XMLobj.removeNode(node)
Last but not least, removeNode. It should be pretty apparent what this method does. Its the equivalent to MovieClip.removeMovieClip, only it removes nodes instead of movieclips. This is how you get rid of unwanted nodes in your XML object or other nodes within. Example:
my_xml = new XML("<**FUN /><**MONEY /><**FRIENDS /><**TAXES />");
trace(my_xml.toString()); // traces <**FUN /><**MONEY /><**FRIENDS /><**TAXES />
my_xml.childNodes[3].removeNode();
trace(my_xml.toString()); // traces <**FUN /><**MONEY /><**FRIENDS />
What about Attributes?
Attributes are not forgotten. They are just more “manual”. The attributes object can be manually edited just as any other object in Flash. This makes them easy to work with, without having to go through other XML methods. Example:
my_xml = new XML("<**FRIENDS />");
my_xml.firstChild.attributes.GIRLFRIEND = "Julie"
trace(my_xml.toString()); // traces <**FRIENDS GIRLFRIEND="Julie" />
delete my_xml.firstChild.attributes.GIRLFRIEND
trace(my_xml.toString()); // traces <**FRIENDS />
Now, with these tools and newly gained knowledge, you can create, edit and manage XML information directly within Flash. When dealing with serverside hosted XML, all you need to do is load the original data, manipulate it within Flash as shown above, then send your newly edited XML back to a server for an update with only minimal serverside scripting usage (only that needed to save and retrieve the XML text).