Xml – Flex 3 – How to read data dynamically from XML

apache-flexflex3xml

I'm new at Flex and I wanted to know how to read an xml file to pull its data into a chart, using Flex Builder 3.

Even though I've read and done some tutorials, I haven't seen any of them loading the data dynamically. For example, I'd like to have an xml like the following:

<data>
    <result month="April-09">
        <visitor>
            <value>8</value>
            <fullname>Brian Roisentul</fullname>
            <coid>C01111</coid>
        </visitor>
        <visitor>
            <value>15</value>
            <fullname>Visitor 2</fullname>
            <coid>C02222</coid>
        </visitor>
        <visitor>
            <value>20</value>
            <fullname>Visitor 3</fullname>
            <coid>C03333</coid>
        </visitor>
    </result>
    <result month="July-09">
        <visitor>
            <value>15</value>
            <fullname>Brian Roisentul</fullname>
            <coid>C01111</coid>
        </visitor>
        <visitor>
            <value>6</value>
            <fullname>Visitor 2</fullname>
            <coid>C02222</coid>
        </visitor>
        <visitor>
            <value>12</value>
            <fullname>Visitor 3</fullname>
            <coid>C03333</coid>
        </visitor>
    </result>
    <result month="October-09">
        <visitor>
            <value>10</value>
            <fullname>Brian Roisentul</fullname>
            <coid>C01111</coid>
        </visitor>
        <visitor>
            <value>14</value>
            <fullname>Visitor 2</fullname>
            <coid>C02222</coid>
        </visitor>
        <visitor>
            <value>6</value>
            <fullname>Visitor 3</fullname>
            <coid>C03333</coid>
        </visitor>
    </result>   
</data>

and then loop through every "visitor" xml item and draw their values, and display their "fullname" when the mouse is over their line.

If you need some extra info, please let me just know.

Thanks,

Brian

Best Answer

The line chart example in the livedocs would help you to get started. They are using an array collection - you can replace it with an XMLListCollection.

//assuming that 'xml' is the name of the variable holding the data
[Bindable]
public var visitors:XMLListCollection = new XMLListCollection(xml.result.visitor);

You can use visitors in the place of expenseAC in their example.