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("cha nge", 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.lengt h; 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].attrib utes.name] ;
}
And the XML structure is
<xml>
<listitem name="The Fish" url="rtmp://media.westwood .edu/video source/">
<stream name="fish" start="0" len="-1" />
</listitem>
<listitem name="The Trike" url="rtmp://media.westwood .edu/video source/">
<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/video source/">
<stream name="fish" start="0" len="-1" />
</listitem>
<listitem name="The Trike" url="rtmp://media.westwood .edu/video source/">
<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
The ActionScript in the player is as follows:
list.addEventListener("cha
//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.lengt
var entry = entries.childNodes[i];
if (entry.nodeName == "listitem") {
//builds array of video clip names
playlists[entry.attributes
} else if (entry.nodeName == "menu") {
//builds array of available videos
for (var j = 0; j<entry.childNodes.length;
nav[j] = playlists[entry.childNodes
}
And the XML structure is
<xml>
<listitem name="The Fish" url="rtmp://media.westwood
<stream name="fish" start="0" len="-1" />
</listitem>
<listitem name="The Trike" url="rtmp://media.westwood
<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
<stream name="fish" start="0" len="-1" />
</listitem>
<listitem name="The Trike" url="rtmp://media.westwood
<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
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.
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.lengt h; 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?
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.lengt
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?
ASKER
Yes, there are multiple <media> nodes, that's the problem.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
correction:
var entries = medias.childNodes[j];
var entries = medias.childNodes[j];
ASKER
Very awesome, I had to change the j and tweak some code later on, but it works perfect now, thanks a million! (or 500 ;)
var entries = this.childNodes[0].childNo