Saturday, April 19, 2008

How to create XMLListCollection from XML in flex 3

There are times when the data that you obtain is in the form of XML or a string that contains XML tags. For displaying the data in datagrid, list, trees we desire to convert the data in the XMLListCollection. We need to perform the following steps to get the XMLListCollection.

1. Lets assume that we want to convert the following string to the XMLListCollection

var xmlStr:String="<root>
<person>
<name>Rohit</name>
<surname>Agarwal</surname>
<phone>5551234</phone>
<age>24</age>
</person>
<person>
<name>Richa</name>
<surname>Mittal</surname>
<phone>5552341</phone>
<age>23</age>
</person>
<person>
<name>Puneet</name>
<surname>Jain</surname>
<phone>5553412</phone>
<age>23</age>
</person>
</root>";

2. Convert the string to XML

private var pat:XML ;

pat=new XML(xmlstr);

3. Convert the XML to XMLList. We want to have individual persons as seperate XML objects

private var patList:XMLList;
patList = pat.person;

4. Convert the XMLList to XMLListCollection

[Bindable]
private var patListCol:XMLListCollection;
patListCol = new XMLListCollection(patList);

In this way the XMLListCollection is created from the XML





How to convert a XML to ArrayCollection in Flex

I wanted to use AdvancedDataGrid control of Flex 3, for which the best way to represent data is in the form of an ArrayCollection. The problem was that the data available to me was in the form of an string(XML serialized as string) and not objects. So I wanted to convert the XML to ArrayCollection.

Steps
1. The following is the string that I wanted to convert:

var xmlStr:String="<root>
<person>
<name>Rohit</name>
<surname>Agarwal</surname>
<phone>5551234</phone>
<age>24</age>
</person>
<person>
<name>Richa</name>
<surname>Mittal</surname>
<phone>5552341</phone>
<age>23</age>
</person>
<person>
<name>Puneet</name>
<surname>Jain</surname>
<phone>5553412</phone>
<age>23</age>
</person>
</root>";

2. Convert the string to XMLDocument

var xmlDoc:XMLDocument = new XMLDocument(xmlStr);


3. Convert the XMLDocument to object by using SimpleXMLDecoder

var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
var resultObj:Object = decoder.decodeXML(xmlDoc);

4. Declare an ArrayCollection
[Bindable]
private var patArrayListCollection:ArrayCollection = new ArrayCollection();

5. Add the resultobj to the ArrayCollection

patArrayListCollection.addItem(resultObj.root.person);

The important thing to note here is the composition of the patArrayListCollection is in the form
[0] --main object
[0]-sub object 1
[1]-subobject 2
[2]-subobject 3

This is not in a form that we require to make it a data provider for the AdvancedDataGrid. The Grid requires the ArrayCollection to have
objects in the form
[0]-object 1
[1]-object 2
[2]-object 3


So to get this form the following step is done

6. var tempArray:ArrayCollection=patArrayListCollection.getItemAt(0)as ArrayCollection;

7. Finally assign tempArray to the AdvancedDataGrid
myADG.dataProvider=tempArray;






Sunday, April 13, 2008

ActionScript Error #2148:
SecurityError: Error #2148: SWF file file:///C:/Documents and Settings/UserProfile/Desktop/flexstore/bin-release/flexstore.swf cannot access local resource myFile.swf. Only local-with-filesystem and trusted local SWF files may access local resources.


This runtime error is thrown in Flex 3 if some where in the application you are trying to do a httpservice and use a resource from the local folder.
<mx:HTTPService id="fillTreeByCatRPC" url="categories.xml" resultFormat="e4x" />

I have seen this error only in Flex 3, Flex 2 never gave this error.

I did some google and found some solutions. Almost all the solutions suggests that one should change the flex compiler settings.

"add these arguments to the compiler (via Properties - Flex Compiler) : -use-network=false " http://curtismorley.com/2007/08/31/flash-cs3-flex-2-as3-error-2148/#comment-3714

The problem with this solution is that once the settings are changed then the application will not be able to read any remote resource. so the below request will fail
<mx:HTTPService id="fillTreeByCatRPC" url="http://www.resources.com/categories.xml" resultFormat="e4x" />

After doing further searches on Internet, I found that it is the setting problems of the flash player that cause the problem. Basically one needs to set the security settings of the flash player so that it allows the swf file to access the resources on the local system. This is done by visiting the following page
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html

Make sure that the Global security settings are set to Always Allow

The following screenshot is what we are looking for
Once the settings has been done, Restart the browser and Flex 3 IDE

In all probability the error is gone.