Company Blog RSS Feed

Populating a DataGrid with Remote XML Data in Flex 4

August 30, 2010
Last Friday, I posted an article about how to populate a DataGrid using inline XML in Flex 4. This is a short tutorial explaining how to clean up your code a bit by saving your XML code outside of the .MXML file and doing a remote call using code similar to the previous example.

The following code inside the declaration block contains an XML block which contains your data. Your data was accessed using the following snippet:

<fx:Declarations>
    <fx:XML id="people">
        <people>
            <person>
                <firstName>Joshua</firstName>
                <lastName>Rowe</lastName>
                <email>support@nvisionweb.com</email>
            </person>
            <person>
                <firstName>John</firstName>
                <lastName>Doe</lastName>
                <email>johndoe@somedomain.com</email>
            </person>
        </people>
    </fx:XML>
</fx:Declarations>


To retrieve this data remotely, simply remove the XML root node and all its' contents, only keeping the Declarations block and the XML block. Create a new file, name it "people.xml", and place it in an "assets" directory. Remove the closing XML tag </fx:XML> and change the opening tag to a self-closing tag. As a final step, add a "source" parameter to your XML tag and set the source as "assets/people.xml". The following code block should look like this:

<fx:Declarations>
    <fx:XML id="people" source="assets/people.xml" />
</fx:Declarations>


This will help keep your code separate from your data and make finding things a little easier.



Add A Comment





Subscribe to this blog