ActionScript function to 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:


private function xml2array(xml:XMLDocument):ArrayCollection {
var ac:ArrayCollection = new ArrayCollection();
var xmlNodes:ArrayCollection = new ArrayCollection(xml.childNodes);
for (var i:int = 0; i < xmlNodes.length; i++) {
var thisRecord:Dictionary = new Dictionary();
var rowObj:Object = xmlNodes[i].childNodes;
for (var j:int = 0; j < rowObj.length; j++) {
var thisNodeName:String = rowObj[j].localName;
var thisNodeValue:String = '';
if (rowObj[j].childNodes && rowObj[j].childNodes.length > 0) {
if (rowObj[j].childNodes[0].nodeValue)
thisNodeValue = rowObj[j].childNodes[0].nodeValue;
else
thisNodeValue = '';
}
thisRecord[thisNodeName] = thisNodeValue;
}
ac.addItem(thisRecord);
}
return ac;
}

Comments

  1. Hey Eugene, thanks for the example code. It helped me with some utility functions I was working on.

    -Andy

    ReplyDelete
  2. How to use this code in flex application?

    ReplyDelete

Post a Comment

Popular posts from this blog

Conceptual design

Using Trello and Confluence to manage UX design project. Part 1

How do I use InVision