Feeds:
Posts
Comments

Posts Tagged ‘convert XML into Array’

To allow the grouping and summary rows in DataGrid, I decided to convert existing report (old fashion DataGrid) to AdvancedDataGrid, which is a new feature of Flex 3. And which is turned out that raw XML I receive from backend returns an error. I needed more control of the data. As a part of the fix I wrote the conversion function which returns associated array collection based on input xml. Enjoy:

  1. private function xml2array(xml:XMLDocument):ArrayCollection {  
  2.     var ac:ArrayCollection = new ArrayCollection();  
  3.     var xmlNodes:ArrayCollection = new ArrayCollection(xml.childNodes);  
  4.     for (var i:int = 0; i < xmlNodes.length; i++) {  
  5.         var thisRecord:Dictionary = new Dictionary();  
  6.         var rowObj:Object = xmlNodes[i].childNodes;  
  7.         for (var j:int = 0; j < rowObj.length; j++) {  
  8.             var thisNodeName:String = rowObj[j].localName;  
  9.             var thisNodeValue:String = ;  
  10.             if (rowObj[j].childNodes && rowObj[j].childNodes.length > 0) {  
  11.                 if (rowObj[j].childNodes[0].nodeValue)  
  12.                     thisNodeValue = rowObj[j].childNodes[0].nodeValue;  
  13.                 else   
  14.                     thisNodeValue = ;  
  15.             }  
  16.             thisRecord[thisNodeName] = thisNodeValue;  
  17.         }  
  18.         ac.addItem(thisRecord);  
  19.     }  
  20.     return ac;  
  21. }  

Read Full Post »