Monday, November 24, 2008

How to use Local XML for HTTPSERVICE

As we know that used for remote server application. But in case we have local XML file then how we can fetch recored from XML using
1. First create new project in Adobe Flex Builder 3.
File -> New -> Flext Project give some name (say Binod_ShareTrading) and click on Finish.
2. Paste you local XML into src folder (say addressbookdata.xml)



3. Right click on Binod_ShareTrading project and click on properties -> Flex Compiler and come to Additional compiler arguments: and put this info
-locale en_US -use-network=false and click on Apply and OK.
Now it will inform to httpserice that do not look on internet for the given XML file.

4. Now start your code in Binod_ShareTrading.mxml (May be different in your case)


<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();" color="#1A0B3C" themeColor="#FF7E00" cornerRadius="6">

<mx:XML id="employees" source="addressbookdata.xml" />
<mx:XMLListCollection id="empXMLList" source="{employees.contact}" />

<mx:Script>

<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.ValidationResultEvent;
import mx.rpc.xml.SimpleXMLDecoder;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.effects.easing.*;

[Bindable]
public var allEmp:ArrayCollection;
public var checked:Boolean=false;

private function addBlurFilter():void {
var bf:BlurFilter = new BlurFilter(0,0,0);
var myFilters:Array = new Array();
myFilters.push(bf);
Login_lbl.filters = myFilters;
Pass_lbl.filters = myFilters;
Button_Login.filters = myFilters;
loginPanel.filters = myFilters;
}
public function checkUserAndPassword(name:String,pass:String):Boolean{

for(var i:int=0;i<allEmp.length;i++){
var un:String = allEmp[i].userName;
var ps:String = allEmp[i].password;
if((name==un)&&(pass==ps)){
//Alert.show("Name :: "+name+" username in database "+un);
//Alert.show("Pass :: "+pass+" in database :: "+ps); checked=true;
}
}
return checked;
}

public function validate():void{
if (loginValidator.validate().type == ValidationResultEvent.VALID){
if (passwordValidator.validate().type == ValidationResultEvent.VALID){
myservice.send();
}
else{
Alert.show("Password can not be blank");
}
}
else{
Alert.show("Login ID can not be blank");
}
}

public function myfun(event:ResultEvent):void{
allEmp = event.result.contacts.contact;
var check:Boolean = checkUserAndPassword(Login_tx.text,Pass_Tx.text);
// Alert.show("Check value :: "+check);
if(check)
Alert.show("Welcome Mr. "+Login_tx.text);
else Alert.show("CHECK USER NAME OR PASSWORD");
}

//mylabel2.setStyle("showEffect", rotate);
//mylabel2.setStyle("hideEffect", fade);

public function showuser():void{
if (mygrid.visible==true){
mygrid.visible=false;
showAllUser.label="Show All User";
}
else{
mygrid.visible=true;
showAllUser.label="Hide the user Details";
}
}

private function init():void {
fx.play([loginPanel,Login_lbl]);
//Alert.show("INIT METHOD IS CALLING");
mygrid.setStyle("showEffect", rotate);
mygrid.setStyle("hideEffect", fadeOut);
}

</mx:Script>
<mx:Fade id="fade"/>
<mx:Fade id="fx" alphaFrom="0" alphaTo="1" duration="5000" />
<mx:Fade id="dLinkFadeIn" alphaTo="1.0" alphaFrom="0" duration="100"/> <mx:Fade id="dLinkFadeOut" alphaTo="0.0" alphaFrom="1.0" duration="100"/> <mx:Fade id="fadeOut" duration="1000" alphaFrom="1.0" alphaTo="0.0"/> <mx:Fade id="fadeIn" duration="1000" alphaFrom="0.0" alphaTo="1.0"/>

<mx:Rotate id="rotate" angleFrom="-180" angleTo="0" easingFunction="Elastic.easeInOut" duration="2000" />

<mx:HTTPService id="myservice" url="addressbookdata.xml" result="myfun(event)"/> <mx:StringValidator id="loginValidator" source="{Login_tx}" property="text" triggerEvent=""/>
<mx:StringValidator id="passwordValidator" source="{Pass_Tx}" property="text" triggerEvent=""/>

<mx:Canvas id="loginCanva">
<mx:Panel x="33" y="19" width="416" height="260" color="#57D5FF" layout="absolute" id="loginPanel" alpha="0.1" title="Customer Login" hideEffect="{fadeOut}" showEffect="{fadeIn}" creationComplete="addBlurFilter()" themeColor="#FF0048" fontSize="12" cornerRadius="14" fontWeight="bold">
<mx:Label x="41" y="36" text="Login ID :" id="Login_lbl" hideEffect="{fadeOut}" showEffect="{fadeIn}" creationComplete="addBlurFilter()"/>

<mx:Label x="41" y="77" text="Password : " id="Pass_lbl" hideEffect="{fadeOut}" showEffect="{fadeIn}" creationComplete="addBlurFilter()" />

<mx:TextInput x="128" y="34" id="Login_tx"/>
<mx:TextInput x="128" y="75" id="Pass_Tx"/>
<mx:Button x="93.95" y="123" label="Login Now" id="Button_Login" click="validate();" hideEffect="{fadeOut}" showEffect="{fadeIn}" creationComplete="addBlurFilter()" width="242.57576" height="31.515152" color="#FF6B57"/>

</mx:Panel>
</mx:Canvas>

<mx:DataGrid id="mygrid" x="485" y="119" width="600" dataProvider="{employees.contact}" visible="false">
<mx:columns>
<mx:DataGridColumn headerText="UserName " dataField="userName"/> <mx:DataGridColumn headerText="Location" dataField="location"/> <mx:DataGridColumn headerText="Email" dataField="email"/>
</mx:columns>
</mx:DataGrid>

<mx:Button x="485" y="72" id="showAllUser" label="Show All User" click="showuser();"/>
</mx:Application>

IMP : Here in the above code you can also find the conversation between XML file to ArrayCollection.




3 comments:

  1. Really it is very useful for my project

    ReplyDelete
  2. a bit confusing how you have formatted the sourcecode :(

    ReplyDelete
  3. Now I have re formatted the code. Hope you will not get confusion again. :)

    ReplyDelete

You can post your feedback on any question do you have.