0

I wish to extract an attribute from an XMLList and assign it to a string. Here is the snippet of the XML

<node>
   <sport  GAME="Squash" TIME1="2" TIME2="3" TIME3="8"/>
   <sport  GAME="Table Tennis" TIME1="4" TIME2="6" TIME3="7"/>
</node>

I have two variables. One variable will contain the game type, either Squash or Table Tennis. The other will contain one of the following strings “TIME1”, “TIME2” or “TIME3”. The variables are called game and time. I have tried many variations on the code below to get the needed attribute but with no joy. So any help would be much appreciated.

var result:String = node.sport.(@GAME == game).(attribute(time));
1
  • Thanks guys, i was so near yet so far. The way you suggested worked perfectly. Again many thanks Commented Oct 22, 2011 at 10:52

2 Answers 2

1

You are mostly there, here is code that is tested and works.

var myXML:XML = <node>
                <sport GAME="Squash" TIME1="2" TIME2="3" TIME3="8"/>
                <sport GAME="Table Tennis" TIME1="4" TIME2="6" TIME3="7"/>
             </node>;

        public function init():void {
            var game:String = "Table Tennis";
            var time:String = "TIME2";
            var result:String = myXML.sport.(@GAME==game).attribute(time).toString();

            trace("Result: "+result);
        }
Sign up to request clarification or add additional context in comments.

Comments

0

Not entirely sure what you're trying to accomplish, but I think you're trying to find the value of the specified time attribute?

Don't use parenthesis around the attribute(time);

package
{
    import flash.display.Sprite;

    public class XmlAttribute extends Sprite
    {
        private var xml:XML =   <node>
                                   <sport  GAME="Squash" TIME1="2" TIME2="3" TIME3="8"/>
                                   <sport  GAME="Table Tennis" TIME1="4" TIME2="6" TIME3="7"/>
                                </node>;

        public function XmlAttribute()
        {
            super();

            var game:String = "Squash";
            var time:String = "TIME1";

            var result:String = xml.sport.(@GAME == game).attribute(time);
            trace(result);
        }

    }
}

Output is: 2

Game "Squash" using "TIME1" is: 2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.