views:

16

answers:

1

Given a very basic xml structure and a generic Object declaration with a property already defined (location), how can I add the nodes of the xml structure (id & name) to the object's properties without statically referencing the names of the nodes?

XML:

<record>
    <id><![CDATA[SS001]]></id>
    <name><![CDATA[SubstationName]]></name>
</record>

ActionScript Object Declaration:

var dataHolder:Object = {location: "Far Away"};

Desired Result:

trace(dataHolder.location);  // returns "Far Away"
trace(dataHolder.id);  // returns "SS001"
trace(dataHolder.name); // returns "SubstationName"

The number and names of the nodes inside the xml structure will change dynamically. This is why I can not simply statically reference them.

Thanks in advance,

Peter Hanneman

A: 

After about an hour and a half of messing around I got it working. Generic solution to the problem looks like this:

for each(var record:XML in sourceXML)
{
    for(var i:int=0;i<record.children().length();i++)
        myObj[record.children()[i].name()] = record.children()[i];
}

Assuming myObj is your Object and sourceXML is your XML structure. Impossibly simple solution but the actual implementation can be a little tricky. Hope this helps someone else.

Peter Hanneman