Create
and Edit XML in Flash
        by senocular

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.

  • 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()


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:

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.


 

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:


 

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:


 

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:


 

Notice how the last invader doesn't 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:


 

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:


 

Now, with these tools and newly gained knowledge, you can create, edit and manage XML information directly within Flash. When dealing with server side 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 server side scripting usage (only that needed to save and retrieve the XML text).

Feel free to post any questions you may have on the forums.

senocular

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.