2

I'm new at actionscript 2.0, and I'd like to know how to read a xml attribute's value while iterating the xml.

So far, I can get the xml elements, but I can't get this issue to work.

Thanks in advance,

Brian

2 Answers 2

4

well, any element node has the property attributes ... this is simply an anonymous objects stuffed with the info ...

just do someXML.attributes.someAttribute and you will get the desired value ...

likewise, you may check out this little library i made for AS2, to bring parts of e4x to the AS2 and simplify XML processing ... it is not at all production level though!

greetz

back2dos

Sign up to request clarification or add additional context in comments.

Comments

0

Hum, need some code of yours to solve this mystery ;p Otherwise you will get some boring links to the sdk documentation. But ill give it a shot, it's AS3 code and I don't know if xml had some major rework from AS2->AS3. Must say its a very nice experience to work with xml in AS3 though.

        //current level number
        public var mCurrentLevelNumber:Number = 0;
        //read from file variables:
        private var mLoader:URLLoader = new URLLoader();
        private var mXML:XML;

        //add a listener 
        mLoader.addEventListener(Event.COMPLETE, OnLoadXML, false, 0, true);
        mLoader.load(new URLRequest("../assets/content.xml"));

....
        /* 
        OnLoadXML
        Parses the data from the file, loads one level
        @e:Event    
        */  
        private function OnLoadXML(e:Event):void{   
            var loadLevel:Array = new Array();
            try{
                //convert the text into an XML
                mXML = new XML(e.target.data);
                //trace("reading from .xml is done, values: ", mXML);
                trace(" Name of the level: ", mXML.level[mCurrentLevelNumber].title.text() );
                for (var j:int=0; j<mXML.level[mCurrentLevelNumber].tiles.tilerow.length(); j++) {
                        trace("Row",j,", tiles:", mXML.level[mCurrentLevelNumber].tiles.tilerow[j].text() );
                }
                //......
                //}  
            }catch (e:TypeError){
                trace("Could not parse the XML");
                trace(e.message);
            }
        }


And the xml file structure;
<!-- pretty level arrays... -->
<levels>
    <level>
        <title>Level 1</title>
        <tiles>
            <tilerow>1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 1 1 1 3 1 13</tilerow>
            <tilerow>1 0 0 0 0 0 0 0 0 0 0 0 10 12 0 9 0 0 0 0 0 0 0 0 1</tilerow>
            <tilerow>1 0 0 0 0 1 0 0 0 0 0 0 10 11 11 11 12 0 6 1 0 0 0 0 1</tilerow>
            <tilerow>1 9 1 0 0 1 0 0 0 0 0 0 10 12 0 9 0 0 6 0 0 0 0 0 1</tilerow>
        </tiles>
    </level>
</levels>       

1 Comment

Thanks for your answer. I'll post below how I'm getting the xml elements' values. What I don't know is how to get the attributes of each element. For example: <Levels><Level id="1">Stage 1</Level></Levels> I already know how to get "Stage 1" but also I'd like to get the value for the id attribute. So well, I'm getting each xml item like this(inside a for): var myXml:XML = new XML(); var bars:Array = new Array(); if(myXml.childNodes[0].childNodes[chartIndex].nodeName=="level") { bars.push[myXml.childNodes[0].childNodes[chartIndex].childNodes[0].childNodes[0].nodeValue] } Thanks, Brian

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.