Wednesday, March 30, 2011

Load (multiple) XML file using URLLoader and extract data

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="" layout="absolute" creationComplete="LoadXML()">
    import mx.collections.ArrayCollection;

    private var NameXML:XML=new XML();
    public var FManager_Names:ArrayCollection=new ArrayCollection();
    private var FManager_Id:ArrayCollection=new ArrayCollection();
    public var Manager_Names:ArrayCollection=new ArrayCollection();
    private var Manager_Id:ArrayCollection=new ArrayCollection();
             private function LoadXML():void
                var LoaderObj:URLLoader=new URLLoader();
                LoaderObj=new URLLoader();
                LoaderObj.load(new URLRequest("FManagerName.xml"));
                LoaderObj.addEventListener(Event.COMPLETE,                                                function(e:Event){ParseXML(e,FManager_Names,FManager_Id);});

                LoaderObj=new URLLoader();
                LoaderObj.load(new URLRequest("ManagerName.xml"));
                LoaderObj.addEventListener(Event.COMPLETE,                                                                         function(e:Event){ParseXML(e,Manager_Names,Manager_Id);});
    private function ParseXML(e:Event,Type:ArrayCollection,Id:ArrayCollection)
                 var currentNode:XML;
                  for( var i:int = 0; i < NameXML.children().length(); i++)
    <mx:Label  text="Functional Manager Names:"/>
    <mx:ComboBox id="FManager" dataProvider="{FManager_Names}" />
    <mx:Label  text="Functional Manager ID:"/>
    <mx:ComboBox id="FManagerID" dataProvider="{FManager_Id}"  />
    <mx:Label  text="Manager Names:"/>
    <mx:ComboBox id="Manager" dataProvider="{Manager_Names}" />
    <mx:Label  text="Manager ID:"/>
    <mx:ComboBox id="ManagerID" dataProvider="{Manager_Id}"/>

//required XML files
<?xml version="1.0" encoding="utf-8"?>
<FManager FMan_id="1" FMan_Name="Dheeraj"/>
<FManager FMan_id="2" FMan_Name="Karthick"/>
<FManager FMan_id="3" FMan_Name="Dinesh"/>

<?xml version="1.0" encoding="utf-8"?>
<Manager Man_id="1" Man_Name="P.Ananth"/>
<Manager Man_id="2" Man_Name="Gokulakrishnan"/>
<Manager Man_id="3" Man_Name="Stani"/>

 LoadXML( )-   You can use the load( ) method of the URLLoader and URLRequest classes to load data of an XML file, text file...When the load event is completed,you can wrap your function call with arguements (to parse mulitple xml files) to a function  .The function also takes the complete load event as its parameter to get the data from it.

ParseXML( )- You can store the data in an XML structure and you can parse it using the children() and attributes() method of XML class.
Do not keep the same name for your xml file and the root tag in it.

1 comment:

  1. Hello sowmya,
    I would like to know how can i fetch data from PHP page that is on server,I am new to Tablet and developing this for Blackberry playbook i've done this for Blackberry smartphones but messing up here with flex,what i am trying to do is user enters the username and password and on Button click , i am supposed to send this username and password to that PHP page and tht Page will return me 1 and _uniqueId on successful login and 0 when it is unsuccessfull....Please help