We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Medium Priority
267 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
Comment
Watch Question

Commented:
Are you saying this does not work?

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

Author

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.

Commented:
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?

Author

Commented:
Yes, there are multiple <media> nodes, that's the problem.
Commented:
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];
                  }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
correction:
var entries =  medias.childNodes[j];

Author

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 ;)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.