ArrayCollection to XML Conversion

This post have three significant solutions:

   1. ArrayCollection output into XML
   2. Converting any MySQL DB values into XML using PHP
   3. Simple Login form using PHP and MXML

The below php code converts db output as xml dbtoconnect.php

<?php
define(“HOSTNAME”,”localhost”);
define(“USERNAME”,”root”);
define(“PASSWORD”,””);
define(“DB_NAME”,”sample”);
define(“TABLE_NAME”,”login”);

mysql_connect(HOSTNAME, USERNAME, PASSWORD);
mysql_select_db(DB_NAME);

$result = mysql_query(“SELECT * FROM “.TABLE_NAME);//Get the number of rows
$num_row = mysql_num_rows($result);
echo ‘<?xml version=”1.0″ encoding=”iso-8859-1”?>’;//Start the output of XML
echo “<data>”;
echo ‘<num>’ .$num_row. ‘</num>’;
if (!$result) {
die(‘Query failed: ‘ . mysql_error());
}
/* get column metadata – column name */
 $i = 0;
 while ($i < mysql_num_fields($result)) {
    $meta = mysql_fetch_field($result, $i);
  $ColumnNames[] = $meta->name;  //place col name into array
  $i++;
 }
$specialchar = array(“&”,”>”,”<“);  //special characters
$specialcharReplace = array(“&amp;”,”&gt;”,”&lt;”); //replacement
/* query & convert table data and column names to xml*/

$w = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo “<row>”;
foreach ($line as $col_value){
 echo ‘<‘.$ColumnNames[$w].’>’;
 $col_value_strip = str_replace($specialchar, $specialcharReplace, $col_value);
 echo $col_value_strip;
 echo ‘</’.$ColumnNames[$w].’>’;
 if($w == ($i – 1)) { $w = 0; }
 else { $w++; }
   }
echo “</row>”;
}
echo “</data>”;
mysql_free_result($result);

?>

The below mxml file converts php ArrayCollection output into XML dbconnect.mxml

 <?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” horizontalAlign=”center” verticalAlign=”middle” creationComplete=”init()”>
 <mx:Style source=”css/halo.css”/>
 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.rpc.events.ResultEvent;
   import mx.rpc.events.FaultEvent;
   import mx.rpc.events.ResultEvent;
   import mx.collections.ArrayCollection;
   
   public var xmlList:XMLList;
   
   public var userBoolean:Boolean;
   public var passBoolean:Boolean;
   
   private function init():void
   {
    httpserviceId.send();
   }
  
   public function remoteloginValidation(event:ResultEvent):void
   {
    xmlList = xmlConvertion(event.result.data.row);
    //Alert.show(“remoteloginValidation :”+xmlList);

   }
   public function remoteLoginFaultHandler(event:FaultEvent):void
   {
    Alert.show(event.fault.faultString, ‘Error’);
   }
   private function xmlConvertion(xmlArrayCol:ArrayCollection):XMLList
   {
    var xmlString:String = “<?xml version=\”1.0\” encoding=\”utf-8\”?>\n<data>”;
    for each(var xmlObj:Object in xmlArrayCol)
    {
     xmlString += “<record>”;
     xmlString += “<UserId>”+xmlObj.UserId+”</UserId>”;
     xmlString += “<Loginname>”+xmlObj.Loginname+”</Loginname>”;
     xmlString += “<Password>”+xmlObj.Password+”</Password>”;
     xmlString += “</record>”;
    }
    xmlString += “\n</data>”;
    return new XMLList(xmlString);
   }
   private function loginHalo(event:Event):void
   {
    var _userName:String = userNameText.text;
    var _passName:String = passNameText.text;
    
    var xmlListValidation:XMLList = xmlList.record.(Loginname == _userName)
    
    //Alert.show(“loginHalo :”+xmlListValidation.Loginname);
    userBoolean = String(xmlListValidation.Loginname)==_userName;
    passBoolean = String(xmlListValidation.Password)==_passName;
    
    Alert.show(“userBoolean :”+userBoolean+” , passBoolean :”+passBoolean);
    if((!userBoolean) && (!passBoolean))
    {
     Alert.show(“UserName & Password Wrong”);
    }
    else if((userBoolean) && (!passBoolean))
    {
     Alert.show(“Password Wrong”);
    }    
   }   
   private function forgetPass(event:Event):void
   {
   }  
  ]]>
 </mx:Script>

 <mx:HTTPService id=”httpserviceId” url=”http://localhost/dptoconnect.php” useProxy=”false” showBusyCursor=”true” result=”remoteloginValidation(event)” fault=”remoteLoginFaultHandler(event)”/>   
 <mx:Panel id=”panelId” width=”420″ height=”220″> 
  <mx:VBox width=”100%” height=”70%” backgroundColor=”#E0E0E0″ horizontalGap=”0″ verticalGap=”0″>
   <mx:Form id=”loginForm” textAlign=”left”>   
    <mx:FormItem label=”User Name:”>
              <mx:TextInput id=”userNameText” />
          </mx:FormItem>
          <mx:FormItem label=”Password:”>
              <mx:TextInput id=”passNameText” displayAsPassword=”true”  />
          </mx:FormItem>
          <mx:FormItem>
              <mx:CheckBox label=”Always login me with current credential”/>
          </mx:FormItem>
          <mx:HBox>
           <mx:FormItem width=”60″>
              </mx:FormItem>
           <mx:FormItem>
               <mx:Button id=”loginId” label=”LOGIN” styleName=”LoginButton” click=”loginHalo(event)” />
              </mx:FormItem>
           <mx:FormItem >
               <mx:LinkButton label=”I forget my password” rollOverColor=”#EFE9D9″ click=”forgetPass(event)”/>
           </mx:FormItem>
    </mx:HBox>        
      </mx:Form>
  </mx:VBox> 
  <mx:HBox width=”100%” height=”30%” verticalGap=”0″ horizontalGap=”0″ backgroundColor=”#F0F0F0″ verticalAlign=”middle” borderColor=”#BABABA”>
   <mx:VBox width=”100%” horizontalAlign=”center” verticalAlign=”middle” borderColor=”#BABABA” verticalGap=”0″>
    <mx:HBox width=”100%” horizontalAlign=”center”>
     <mx:Label text=”Problems with the application?” fontSize=”12″/>
     <mx:LinkButton label=”HALOSupport@emantras.com” textDecoration=”underline” color=”#AE2324″ selectionColor=”#BFBFBF” rollOverColor=”#F0F0F0″/>
    </mx:HBox>
    <mx:HBox width=”100%” horizontalAlign=”center”>
     <mx:LinkButton label=’Copyright @ 2008, Emantras Inc.’ textDecoration=”underline” color=”#AE2324″ selectionColor=”#BFBFBF” rollOverColor=”#F0F0F0″ />
    </mx:HBox>
   </mx:VBox>
  </mx:HBox>     
 </mx:Panel>
</mx:Application>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: