Actionscript3 & PHP & MySQL & XML
I’m gonna post series of tutorials:
Getting data from XML, AS3
Getting data from MySQL, AS3 + PHP + MySQL file
Uploading file with AS3 + PHP
Send email with AS3 + PHP
How to parse a simple XML file. This is our structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="utf-8" ?>
<data>
<users>
<user>
<id>18</id>
<name>bobanman</name>
<avatar><![CDATA[]]></avatar>
<registerData>13-07-2008</registerData>
<email>bobanman@msn.com</email>
</user>
<user>
<id>20</id>
<name>ilya</name>
<avatar><![CDATA[]]></avatar>
<registerData>15-07-2008</registerData>
<email>ilya@asymmetric.co.il</email>
</user>
</users>
</data> |
First we will build XML parser. This is my favorite ways to parse it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| // parce XML
private function parseXML($rawData_:String):void {
var xmldoc:XMLDocument = new XMLDocument();
xmldoc.ignoreWhite = true;
xmldoc.parseXML($rawData_);
var out:Array = [];
const imax:int = xmldoc.firstChild.firstChild.childNodes.length;
for (var i:int = 0; i < imax; i++ ) {
//collect data from XML
out[i] = getUserDTO(xmldoc.firstChild.firstChild.childNodes[i]);
}
// all user data goes here :)
trace(out);
}
// create UserDTO from XMLNode
private function getUserDTO($xmlNode_:XMLNode):UserDTO {
return new UserDTO(int(XMLNode($xmlNode_.childNodes[0]).firstChild.toString()),
XMLNode($xmlNode_.childNodes[1]).firstChild.toString(),
XMLNode($xmlNode_.childNodes[2]).firstChild.toString(),
XMLNode($xmlNode_.childNodes[3]).firstChild.toString(),
XMLNode($xmlNode_.childNodes[4]).firstChild.toString());
} |
You can also parse it using XML class, but like I say I prefer to do it this way
so with this script we will retrieve all the data from XML file.
This is one way also to get data from DB, create with server side language xml files like this and to parce them. Other options are AMFPHP or ASQL but we will cover these two in future topics.
So the we have to load the XML and get set all data into lets say array object.
So full script goes like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package {
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.xml.XMLDocument;
import flash.xml.XMLNode;
[SWF(frameRate = '30')]
public final class DocumentClass extends Sprite {
public function DocumentClass():void {
loadXML();
}
// load external XML
private function loadXML():void {
var ulloader:URLLoader = new URLLoader(new URLRequest("data.xml"));
ulloader.addEventListener(Event.COMPLETE, completeHandler);
}
// parce XML
private function parseXML($rawData_:String):void {
var xmldoc:XMLDocument = new XMLDocument();
xmldoc.ignoreWhite = true;
xmldoc.parseXML($rawData_);
var out:Array = [];
const imax:int = xmldoc.firstChild.firstChild.childNodes.length;
for (var i:int = 0; i < imax; i++ ) {
//collect data from XML
out[i] = getUserDTO(xmldoc.firstChild.firstChild.childNodes[i]);
}
// all user data goes here :)
trace(out);
}
// create UserDTO from XMLNode
private function getUserDTO($xmlNode_:XMLNode):UserDTO {
return new UserDTO(int(XMLNode($xmlNode_.childNodes[0]).firstChild.toString()), XMLNode($xmlNode_.childNodes[1]).firstChild.toString(), XMLNode($xmlNode_.childNodes[2]).firstChild.toString(), XMLNode($xmlNode_.childNodes[3]).firstChild.toString(), XMLNode($xmlNode_.childNodes[4]).firstChild.toString());
}
// exterenal xml loaded. Parse it then
private function completeHandler(info:Event):void {
info.target.removeEventListener(Event.COMPLETE, completeHandler);
parseXML(info.target.data);
}
}
} |
class UserDTO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package {
public final class UserDTO {
private var _id:int;
private var _name:String;
private var _avatarURL:String;
private var _regDate:String;
private var _email:String;
public function UserDTO($id_:int, $name_:String, $avatarURL_:String, $regDate_:String, $email_:String):void {
_id = $id_;
_name = $name_;
_avatarURL = $avatarURL_;
_regDate = $regDate_;
_email = $email_;
}
public function get id():int {
return _id;
}
public function get name():String {
return _name;
}
public function get avatarURL():String {
return _avatarURL;
}
public function get regDate():String {
return _regDate;
}
public function get email():String {
return _email;
}
public function toString():String {
return "[ UserDTO id='" + _id + "' name='" + _name + "' avatarURL='" + _avatarURL + "' regDate='" + _regDate + "' email='" + _email + "' ]";
}
}
} |
So now lets create PHP file that give us the date from MySQL with same structure.
Getting data from Mysql, AS3 + PHP + MySQL file
all files in this tutorial