?
Solved

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

Posted on 2007-07-19
7
Medium Priority
?
251 Views
Last Modified: 2013-11-18
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
Comment
Question by:AcydTrip
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:jkmyoung
ID: 19531545
Are you saying this does not work?

var entries = this.childNodes[0].childNodes[0];
0
 

Author Comment

by:AcydTrip
ID: 19533680
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
 
LVL 12

Expert Comment

by:jkmyoung
ID: 19533910
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AcydTrip
ID: 19534175
Yes, there are multiple <media> nodes, that's the problem.
0
 
LVL 12

Accepted Solution

by:
jkmyoung earned 2000 total points
ID: 19534792
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
 
LVL 12

Expert Comment

by:jkmyoung
ID: 19534796
correction:
var entries =  medias.childNodes[j];
0
 

Author Comment

by:AcydTrip
ID: 19535148
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question