PDA

View Full Version : Xml from php (mysql) into Array in AS question


jvankuijk
11-13-2008, 02:00 PM
Hey all,
I've been searching a while now how to fix my problem. I have an xml file returned from a php file with data populated from my MySQL database. I approach this data via the HttpService function in Flex. The xml output looks like this:
<?xml version="1.0"?>

<klanten>
<klant>
<naam>Semmie van Kuijk</naam>
<firmanaam>Retje inc</firmanaam>
</klant>
<klant>
<naam>Martine Gilson</naam>
<firmanaam>Rouann nv</firmanaam>
</klant>
<klant>
<naam>Jordy van Kuijk</naam>
<firmanaam>Jack the Rippers Web</firmanaam>
</klant>
<klant>
<naam>Maria Theunissen</naam>
<firmanaam>Future-World</firmanaam>
</klant>
<klant>
<naam>Haarie Don Aldena</naam>
<firmanaam>Don Vincendon Lenon</firmanaam>
</klant>
<klant>
<naam>Jif Kinky</naam>
<firmanaam>Jif kink products</firmanaam>
</klant>
<klant>
<naam>Don Kentucky</naam>
<firmanaam>Kip Inc</firmanaam>
</klant>
<klant>
<naam>Bla bla</naam>
<firmanaam>Jewels inc</firmanaam>
</klant>
</klanten>
Ok so that should be fine.
Now in my Flex project I have 2 AS classes, called Klant (customer) and Klanten (customers). I'll post the code of both classes here:

KLANT:

package{
public class Klant{

private var naam:String;
private var firmanaam:String;

public function Klant(name:String,compname:String){
naam = name;
firmanaam = compname;
}

public function toString():String{
var result:String = "" + naam + "\n";
result = result + "" + firmanaam;
return result;
}

public function equals(that:Klant):Boolean{
return (this.naam == that.naam);
}
}
}

KLANTEN:

package{

public class Klanten{
private var klanten:Array = new Array();


public function voegToe(klantIn:Klant):void{
var contains:Boolean = false;
for (var i:int =0; i < klanten.length; i++){
var temp:Klant = klanten[i];
if(temp.equals(klantIn)){
contains = true;
}
}
if(contains == false){
klanten.push(klantIn);
}
}

public function geef(nummer:int):Klant{
return klanten[nummer];
}

}
}

My purpose is to put the dat in the Xml file (Klant1,Klant2,Klant3,...) into a KlantenArray. I really have no idea how to do this, I tried the following:

[Bindable] private var Lijst:XMLListCollection;
private function showKlanten():void{
getKlanten.send();
var klantenlijst:Klanten = new Klanten();
var testArray:Array = new Array();
var xList:XMLList = new XMLList(getKlanten.lastResult as XML);
Lijst = new XMLListCollection(xList.children());

for each (var sData:String in xList.child('')){

testArray.voegToe(sData);
}

And here's my HTTPService:

<mx:HTTPService id="getKlanten" resultFormat="e4x" showBusyCursor="true" url="http://www.studex.be/Flex/ManagerPHP/getKlanten.php" useProxy="false" method="POST">
<mx:request xmlns="">
</mx:request>
</mx:HTTPService>

Any help is greatly appreciated!
Thanks in advance,

Jordy van Kuijk

ABM
11-13-2008, 06:58 PM
Hi Jordy,

I will try to help you to fix this issue!

The HTTPService has a result and fault function to handle the good result or a bad response something

<mx:HTTPService id="getKlanten" resultFormat="e4x" showBusyCursor="true" url="http://www.studex.be/Flex/ManagerPHP/getKlanten.php"
useProxy="false" method="POST" result="handleResult(event)" fault="handleFault(event)">
<mx:request xmlns="">
</mx:request>
</mx:HTTPService>



the handleResult is the handler function where you will try to populate you array with the data from you database and the faultHandler is the error handler if there are some problems in the request.

The handleResult and the faultHandler should be something like this:


public function handleResult(event:ResultEvent):void{

var klanten:Klanten = new Klanten();
for each(var row:XML in event.result..klant){
var klant:Klant = new Klant(row.naam, row.firmanaam);
klanten.voegToe(klant);
}
}

public function handleFault(event:FaultEvent) : void {

trace("Er is een fout opgetreden " + event.toString());
}

One other thing I see in Klant class you have a small bug but also very dangerous

public function Klant(name:String,compname:String){
naam = name;
firmanaam = compname;
}

should be

public function Klant(name:String,compname:String){
this.naam = name;
this.firmanaam = compname;
}


I hope it helps you...
Best Regards and Groetjes,

Adil

jvankuijk
11-14-2008, 12:52 AM
First of all, thanks for helping me out, I understand your code and when I implement it in my program, I get this error: Error #1009: Cannot access a property or method of a null object reference. So that must mean that the xml object that the HTTPService calls is null, or? I tried to call a local file named klanten.xml, but no change.

Thanks in advance,

Jordy van Kuijk

jvankuijk
11-14-2008, 01:10 AM
Oh I called the wrong method at my enterState so that's why the compiler was complaining...

Now it all works,

Thank you so much, again,

Greetings and groetjes,

Jordy