• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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
0
AcydTrip
Asked:
AcydTrip
  • 4
  • 3
1 Solution
 
jkmyoungCommented:
Are you saying this does not work?

var entries = this.childNodes[0].childNodes[0];
0
 
AcydTripAuthor Commented:
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.
0
 
jkmyoungCommented:
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?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
AcydTripAuthor Commented:
Yes, there are multiple <media> nodes, that's the problem.
0
 
jkmyoungCommented:
An extra for loop should do it then:
var medias = this.childNodes[0];
var playlists = {};
var nav = [];
var text1:TextField;
for (var j = 0; j<medias.childNodes.length; j++){
  var entries =  medias[j];
  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];
                  }
0
 
jkmyoungCommented:
correction:
var entries =  medias.childNodes[j];
0
 
AcydTripAuthor Commented:
Very awesome, I had to change the j and tweak some code later on, but it works perfect now, thanks a million! (or 500 ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now