<?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


I have done combinations of that AND URLVariables before for various reasons.
<pre>
header("Content-Type: application/xml; charset=ISO-8859-1");
</pre>
You can use whatever charset is appropriate.
var loader:URLLoader = new URLLoader(new URLRequest("family.xml"));
loader.addEventListener(Event.COMPLETE, xmlDownloaded);
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 xmlData.*) {
if (object.name() == "SON") {
var bday:String= object.@bday;
trace (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();
trace (num_grandchildren);
}
}
// search through all child nodes of DAUGHTER
for each (var grandchildren:XML in xmlData.DAUGHTER.*) {
var ageStr:String= grandchildren.@age;
}
}
But, assume you have an arithmetic expression as a node content. how do you evaluate and return result instead of a string (b'cos: trace(node.exp) will return a string not the result of evaluation - see xml below)
<node>
<exp>10 + 5 * 20</exp>
<node>
Thanks for the library. Its not actually for the stated type of problem. But, I'm still on a way out for a solution.
Toks
But: say I want to allow users to view a list of images from database and to upload their images to db. I'm using Flex,grails and mysql. Any idea will be welcome.
Thank you,
kwame.
he says it will do this but how it goes there is anyones guess.
Thanks for the effort - but would be great if you checked the code before posting.
i have a xml :
<xm>
<user>Andy</user>
<pass>123</pass>
</xml>
how to i add new data in this xml?
tq..
I corrected the syntax errors and uploaded the xml and php files to my server and ran the fla file on my system. All seems to be working fine but there is no change happening to the xml file at all.
Please help! I need such a code for an important project I'm working on...
Aditya