views:

20

answers:

2

I am working with an XML list at the moment, which looks like this:

<feature type="startLocation" value="1" x="15" y="3"/>
<feature type="startLocation" value="1" x="15" y="4"/>
<feature type="startLocation" value="1" x="15" y="5"/>
<feature type="startLocation" value="1" x="15" y="6"/>
<feature type="startLocation" value="1" x="15" y="7"/>

This is the ActionScirpt code which parses this XMLList (named this.dropLocationsXML):

public function dropUnit()
{
    var numX:int;
    var numY:int;
    var feature:XMLList;
    trace(this.dropLocationsXML);
    for(var i:int = 0; i < this.dropLocationsXML.length(); i++)
    {
        feature = this.dropLocationsXML.child(i);
        numX = -16 + (feature.@x)*35;
        numY = 4 + (feature.@y)*35;
    }
}

Now when I try to access this XMLList using the child() function, I end up with an empty XMLList. The same thing is true for when I try to use this.dropLocationsXML[1], and the valueOf() method.
As you can see I only need to extract the x and y attributes from each of the feature tags. The trace() statement gives the XML output shown above.
Does anyone know how to access the root nodes of the XML lists, so that I can use the x and y attributes?

+1  A: 
for each (var x : XML in dropLocationsXML.feature) {
    trace("xml: "+x.@type);

}
maxmc
ah i see. i assumed your xml to be like this: <droplocations><feature .../><feature .. /></droplocations>
maxmc
That almost did the trick: for each (var x : XML in dropLocationsXML) { trace("xml: "+x.@x); }
Bartvbl
aha :)Thanks for the help!
Bartvbl
A: 

You can use XPath, too (untested, but should be close enough):

import mx.xpath.XPathAPI;

var xpath:String   = '/*/feature';
var features:Array = XPathAPI.selectNodeList(this.dropLocationsXML, xpath);

for (var i:Number = 0; i < features.length; i++) {
  trace(features[i].@x);
  trace(features[i].@y);

}

Here is Adobe's documentation of XPathAPI.

Tomalak
That is actionscript 2, the question was on actionscript 3. E4X eliminates the need for XPath in actionscript 3.
George Profenza