4 Replies Latest reply on Apr 18, 2007 5:55 AM by podclock

    XML attributes

    podclock
      Hi, I'm trying to get a grip on getting attributes from loaded XML. When I run this trace to get the attribute "runtime" from my XML file, it comes up as undefined. My XML file looks like this and is loading—I can trace the childNodes:

      <?xml version="1.0" encoding="UTF-8"?>
      <videoList>
      <item myurl="input/FLV/lumiere2.flv" runtime="335" />
      <item myurl="input/FLV/lumiere3.flv" runtime="34" />
      </videoList>


      =========



        • 1. Re: XML attributes
          Greg Dove Level 4
          this.firstChild is the videoList (the root Node)

          this.firstChild.childNodes is the array of childNodes of videoList

          so,
          this.firstChild.childNodes[0].attributes.runtime

          should give you a result, as will

          this.firstChild.childNodes[1].attributes.runtime

          Takes a while to get your head around XML Objects but once you do it (almost) gets easier.
          • 2. Re: XML attributes
            podclock Level 1
            Thanks, I wasn't understanding the childNodes and attributes as arrays.

            So, my real problem might have more to do with the function I'm writing. If I trace (vidRuntime[0]); inside of the onLoad function below I get the correct answer, but if I put it outside it is undefined. I know this has to be basic, but how do I call it outside of the function.

            ========

            var inputXML:XML = new XML();
            inputXML.ignoreWhite = true;

            var vidURL:Array = new Array ();
            var vidRuntime:Array = new Array ();

            inputXML.onLoad = function(bSuccess:Boolean):Void {
            if (bSuccess) {
            var videos:Array = this.firstChild.childNodes;
            for (i=0; i<videos.length; i++) {
            vidURL.push(videos .attributes.myurl);
            vidRuntime.push(videos
            .attributes.runtime);
            }
            trace (vidRuntime[0]);
            }
            };

            inputXML.load ("input/XML/vidlist.xml")
            • 3. Re: XML attributes
              Greg Dove Level 4
              NB: Its best to use the 'attach Code' button to post code (your i loop iterator is interpreted as an italic tag in the forum).

              You will only see the vidRuntime array populated after the inputXML.onLoad event Handler method has run. What you would normally do where you have trace(vidRuntime[0] is call whatever code you want to do the next thing. So you might call a function called
              displayVideoOptions() ;
              if you wanted to give someone the choice of which video to play.

              This is simply because it takes time to load the XML in and parse it, but other actionscript code (e.g. on the frame) can continue to execute. It just can't use the XML information - or your array which would still be empty - because the XML hasn't loaded' yet.

              Hope that makes sense.
              • 4. Re: XML attributes
                podclock Level 1
                Thanks!