Link to home
Start Free TrialLog in
Avatar of AcydTrip
AcydTrip

asked on

Help traversing Nodes in XML in Actionscript - probably simple but urgent

Trying to adapt an example from Adobe, the Dynamic Playlist.  I'm trying to adapt it to interact with another app which uses an XML file sructured a bit differently and I'm not having much luck traversing Nodes.

The ActionScript in the player is as follows:

list.addEventListener("change", listListener);
//Function that loads the XML file, parses it, and builds the list of available video clips
var xmllist = new XML();
//setup a variable to hold the XML
xmllist.ignoreWhite = true;
xmllist.load("webcams.xml");
//load the XML file
//The following gets called when the XML has been loaded
xmllist.onLoad = function(status) {
      if (!status) {
            trace(status);
      }
      var entries = this.childNodes[0];
      var playlists = {};
      var nav = [];
      var text1:TextField;
      for (var i = 0; i<entries.childNodes.length; i++) {
            var entry = entries.childNodes[i];
            if (entry.nodeName == "listitem") {
                  //builds array of video clip names
                  playlists[entry.attributes.name] = entry;
            } else if (entry.nodeName == "menu") {
                  //builds array of available videos
                  for (var j = 0; j<entry.childNodes.length; j++) {
                        nav[j] = playlists[entry.childNodes[j].attributes.name];
                  }

And the XML structure is

<xml>
      <listitem name="The Fish" url="rtmp://media.westwood.edu/videosource/">
            <stream name="fish" start="0" len="-1" />
      </listitem>
      
      <listitem name="The Trike" url="rtmp://media.westwood.edu/videosource/">
            <stream name="trike_final" start="0" len="-1" />
      </listitem>
      
      <menu>
      <listitem name="The Fish" />
      <listitem name="The Trike" />
      
      </menu>

Now, the problem is, the app I'm trying to integrate with has one additional level and resembles

<xml>
           <media>
      <listitem name="The Fish" url="rtmp://media.westwood.edu/videosource/">
            <stream name="fish" start="0" len="-1" />
      </listitem>
      
      <listitem name="The Trike" url="rtmp://media.westwood.edu/videosource/">
            <stream name="trike_final" start="0" len="-1" />
      </listitem>
      
      <menu>
      <listitem name="The Fish" />
      <listitem name="The Trike" />
      
      </menu>

(Note the <media> element) Now, there are other elements within <media> or I'd just change the structure.  

What I'm after is the way to change the AS code, simply adding an additional childNodes isn't cutting it as it would in another language
Avatar of jkmyoung
jkmyoung

Are you saying this does not work?

var entries = this.childNodes[0].childNodes[0];
Avatar of AcydTrip

ASKER

Ok, so I'm an idiot ;)  

Well, sort of, it works, but which do I need to loop the first or second (where do I put the [i] )? I'm only seeing the first <media> contents.
In your original xml, 'entries' references the <xml> element
In the next xml, with the updated entries, it references the <media> node

Your earlier code should still work without modification.
for (var i = 0; i<entries.childNodes.length; i++) {
  var entry = entries.childNodes[i];

If you wanted to deal with the other nodes, you could simply add a else if clause at the end.

Just to check, there aren't multiple media nodes correct?
Yes, there are multiple <media> nodes, that's the problem.
ASKER CERTIFIED SOLUTION
Avatar of jkmyoung
jkmyoung

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
correction:
var entries =  medias.childNodes[j];
Very awesome, I had to change the j and tweak some code later on, but it works perfect now, thanks a million! (or 500 ;)