<?xml version="1.0" ?>
<FAMILY surname= "Smith" location= "New York">
<SON name="Dave" bday= "08/10" />
<SON name="Bob" bday= "05/06" />
<DAUGHTER married_name="Perone">
<GRANDCHILD age="5" />
<GRANDCHILD age="6" />
</DAUGHTER>
</FAMILY>
Downloading XML from a server is pretty much like any other thing.
loader = new URLLoader(new URLRequest("http://www.example.com/family.xml"));
loader.addEventListener(Event.COMPLETE, xmlDownloaded);
Once we've downloaded the XML though, Actionscript 3 has a bunch of convenient things built right into the language to access the XML elements in various ways.
function xmlDownloaded(event:Event):void {
var xmlData:XML = new XML(event.target.data);
var surname:String= xmlData.@surname;
var location:String= xmlData.@location;
// get the first granddaughter's age of the first daughter
var grandchild_age:Number= Number(xmlData.DAUGHTER.GRANDCHILD.@age);
// search through all elements inside FAMILY
for each (var object:XML in levelData.*) {
if (object.name() == "SON") {
var bday:String= object.@bday;
} else
if (object.name() == "GRANDCHILD") {
var age:Number= object.@age; // will automatically convert to Number
} else
if (object.name() == "DAUGHTER") {
var num_grandchildren:uint= object.*.length();
}
}
// search through all child nodes of DAUGHTER
for each (var:grandchildren:XML in levelData.DAUGHTER.*) {
var ageStr:String= grandchildren.@age;
}
}
Accessing properties of an element is as easy as using the '.@' operator and accessing children is just simply typing .whateverchildname (you can also get any XML elements's name and attributes with the .name() and .attribute() functions). To access all elements at some point in the tree you simply use the '*' wildcard operator.One important thing to notice above is the xmlData.DAUGHTER.GRANDCHILD.@age. There are multiple GRANDCHILD's and potentially multiple DAUGHTERS (if we so choose to add more). What does it mean to get the age attribute then?
What's going on here is that you can simplify indexing into the first child element of a node by omiting the [0]. This was done to simplify cases where a node only has 1 child.
One final important function is the .parent() which gets the parent node of the element.
Let's take a look at how to change the XML data and upload it back to the server.
function uploadXML():void {
var family:XMLList= new XMLList();
// add a son named Will to the end of FAMILY
family += <SON name="Will" bday="01/15" />
family += <DAUGHTER married_name="Potter" />
var grandchildren:XMLList = new XMLList();
grandchildren += <GRANDCHILD age="1" />
// associate the grandchildren with the last child
family[family.length()-1].setChildren(grandchildren);
// upload the XML to a php script
var loader:UrlLoader = new URLLoader();
var request:URLRequest= new URLRequest("http://www.example.com/uploadscript.php");
request.contentType = "text/xml";
request.data = family.toXMLString(); // convert to a string
request.method = URLRequestMethod.POST;
loader.load(request);
}
Pretty easy huh? All you have to do is a += to add new XML nodes. You can also deliberately set the children to a collection of nodes (XMLList) by calling setChildren and there is even insertChildAfter and insertChildBefore (not demonstrated here).The easiest way I've found to upload the XML data to the server is to set the content type as text/xml and just upload it as the data rather than setting up a URLVariable to be equal to the text.
<?php
$fh = fopen("family.xml", 'w') or die("can't open file");
fwrite($fh, $HTTP_RAW_POST_DATA);
fclose($fh);
?>
One last caveat is when we upload in the aforementioned fashion, the PHP script needs to read from the $HTTP_RAW_POST_DATA variable instead of one of the $_POST or $_GET variables. This example takes the posted XML data and writes family.xml from with it.That's pretty much most of what you will work with when using XML in Actionscript 3 although there are more methods to work with. You can find the XML documentation on the Adobe Flex 2 Language Reference. A complete reference for using XML in Actionscript 3 can also be found at the ECMA 357 Standard (E4X).








SUBSCRIBE


The fopen("family.xml", 'w') in the PHP code is set to overwrite mode; to append onto the file instead, use 'a' instead of 'w'.