Interfacing with XML and ActionScript 3.0

K

KDawg44

Hello,

I have used JavaScript in the past but have not tried anything like
this. If anyone could point me to a good spot to learn how to do
this, I would greatly appreciate it.

What I am trying to do, is describe in an XML file some videos. This
file looks something like this

<videos>
<video>
<fileName>video1.flv</fileName>
<title>title1</title>
<description>This is video1</description>
</video>
<video>
<fileName>video2.flv</fileName>
<title>title2</title>
<description>This is video2</description>
</video>
..... etc.

What i would like to do is load this from the XML file, list out the
titles, and when the titles are clicked the filename is passed into my
swf file to play the different clip.

Thanks very much for any suggestions.
 
K

KDawg44

I would parse the filenames on the server with MSXML and generate the JS
that creates the links. This could also be done with PHP, PERL, etc. (or
AJAX on the client side.) In the JS, pass the filenames to Flash in the
links' onclick handlers using SetVariable or ExternalInterface (one of these
is better supported across Flash versions than the other, but I forget
which.) The links should be hidden initially and presented only if a
suitable Flash version is detected. If using AJAX and a suitable Flash
version is not found, you should skip the XML download entirely.

Thanks very much for the suggestions. My problem is that I am doing
this through a Content Management System and do not have the ability
to use a server side scripting language (because that would make it
waaaaay to easy and we can't have that can we?).

I can access the XML file and parse it. I would like to parse it into
variables and then access those variables through function calls. The
problem is that, on loading the page, the function calls to write part
of the page are being called before the XML parsing is loading the
variables what's written is coming up as undefined...

Any suggestions?

Thanks.
 
D

David Mark

Thanks very much for the suggestions. My problem is that I am doing
this through a Content Management System and do not have the ability
to use a server side scripting language (because that would make it
waaaaay to easy and we can't have that can we?).

I can access the XML file and parse it. I would like to parse it into
variables and then access those variables through function calls. The
problem is that, on loading the page, the function calls to write part
of the page are being called before the XML parsing is loading the
variables what's written is coming up as undefined...

Any suggestions?

Yes. Post your code.
 
K

KDawg44

Yes. Post your code.

<html>
<head>
<script language="javascript" type="text/javascript">
var fileName = new Array();
var title = new Array();

function importXML()
{
if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createList;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) extract()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("videos.xml");
}

function extract()
{
var x = xmlDoc.getElementsByTagName('video');
for (i=0;i<x.length;i++)
{
fileName =
document.createTextNode(x.childNodes[1].firstChild.nodeValue);
title =
document.createTextNode(x.childNodes[3].firstChild.nodeValue);
}
}

function getfileNames()
{
return fileName;
}

function getTitles()
{
return title;
}


</script>
</head>
<body bgcolor = "#FFFFFF" onLoad="javascript:importXML();">

<table border = 0 width = 221>

<tr><td align = center><a href =
"javascript:launchVideoPlayer();"><font face = verdana size = 2 color
= "#FFFFFF">Launch Video Player</a></td></tr>
<tr><td>
<script type="text/javascript">
var titles = new Array;
titles = getTitles();
document.write(titles[0]);
</script>
</td></tr>
<tr><td>&nbsp;</td></tr>
</table>

</body>
</html>


Thanks for any suggestions. Obviously this code is not complete for
my ultimate goal but I am just trying to parse the XML and print it
out at this point. (preferably without any of that DOM appendChild
and createElement stuff).

Thanks.
 
D

David Mark

Yes. Post your code.

<html>
<head>
<script language="javascript" type="text/javascript">
var fileName = new Array();
var title = new Array();

function importXML()
{
if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createList;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) extract()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("videos.xml");
}

function extract()
{
var x = xmlDoc.getElementsByTagName('video');
for (i=0;i<x.length;i++)
{
fileName =
document.createTextNode(x.childNodes[1].firstChild.nodeValue);
title =
document.createTextNode(x.childNodes[3].firstChild.nodeValue);
}
}

function getfileNames()
{
return fileName;
}

function getTitles()
{
return title;
}

</script>
</head>
<body bgcolor = "#FFFFFF" onLoad="javascript:importXML();">

<table border = 0 width = 221>

<tr><td align = center><a href =
"javascript:launchVideoPlayer();"><font face = verdana size = 2 color
= "#FFFFFF">Launch Video Player</a></td></tr>
<tr><td>
<script type="text/javascript">
var titles = new Array;
titles = getTitles();
document.write(titles[0]);


How can you write something during load that isn't retrieved until
after the page loads?
</script>
</td></tr>
<tr><td>&nbsp;</td></tr>
</table>

</body>
</html>

Thanks for any suggestions. Obviously this code is not complete for
my ultimate goal but I am just trying to parse the XML and print it
out at this point. (preferably without any of that DOM appendChild
and createElement stuff).

You will have to use DOM manipulation to do this.
 
B

Bart Van der Donck

KDawg44 said:
I have used JavaScript in the past but have not tried
anything like this. If anyone could point me to a good spot
to learn how to do this, I would greatly appreciate it.

What I am trying to do, is describe in an XML file some
videos. This file looks something like this

[ snip XML code ]
What i would like to do is load this from the XML file, list
out the titles, and when the titles are clicked the filename
is passed into my swf file to play the different clip.


-------------------------------------------------------------------
XML file 'videos.xml':
-------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<videos>
<video>
<file>video1.flv</file>
<title>title1</title>
<desc>This is video1</desc>
</video>
<video>
<file>video2.flv</file>
<title>title2</title>
<desc>This is video2</desc>
</video>
<video>
<file>video3.flv</file>
<title>title3</title>
<desc>This is video3</desc>
</video>
</videos>

-------------------------------------------------------------------
HTML file (in same directory):
-------------------------------------------------------------------

<html>
<head>
<script type="text/javascript">
var xmlDoc;
var output = '';

function loadXML()
{
// load xml file - code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load('videos.xml');
getmessage();
}

// load xml file - code for Mozilla, Firefox, Opera, etc.
else if (document.implementation
&& document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load('videos.xml');
xmlDoc.onload = getmessage;
}

else
{
alert('Your browser cannot handle this script');
}
}

function getmessage()
{
// walk the 'file'-tags and make code with link to player.swf:
// <a href="player.swf?file=FILE">TITLE</a><br>DESC<br><hr>
for (var i=0; i<xmlDoc.getElementsByTagName("file").length; ++i)
{
output += '<a href="player.swf?file='
+ xmlDoc.getElementsByTagName('file').childNodes[0].nodeValue
+ '">'
+ xmlDoc.getElementsByTagName('title').childNodes[0].nodeValue
+ '</a><br>\n'
+ xmlDoc.getElementsByTagName('desc').childNodes[0].nodeValue
+ '<br><hr>\n';
}
document.getElementById('vids').innerHTML = output;
}
</script>
</head>
<body onload="loadXML()">
<span id="vids"></span>
</body>
</html>
 
K

KDawg44

KDawg44 said:
I have used JavaScript in the past but have not tried
anything like this. If anyone could point me to a good spot
to learn how to do this, I would greatly appreciate it.
What I am trying to do, is describe in an XML file some
videos. This file looks something like this

[ snip XML code ]
What i would like to do is load this from the XML file, list
out the titles, and when the titles are clicked the filename
is passed into my swf file to play the different clip.

-------------------------------------------------------------------
XML file 'videos.xml':
-------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<videos>
<video>
<file>video1.flv</file>
<title>title1</title>
<desc>This is video1</desc>
</video>
<video>
<file>video2.flv</file>
<title>title2</title>
<desc>This is video2</desc>
</video>
<video>
<file>video3.flv</file>
<title>title3</title>
<desc>This is video3</desc>
</video>
</videos>

-------------------------------------------------------------------
HTML file (in same directory):
-------------------------------------------------------------------

<html>
<head>
<script type="text/javascript">
var xmlDoc;
var output = '';

function loadXML()
{
// load xml file - code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load('videos.xml');
getmessage();
}

// load xml file - code for Mozilla, Firefox, Opera, etc.
else if (document.implementation
&& document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load('videos.xml');
xmlDoc.onload = getmessage;
}

else
{
alert('Your browser cannot handle this script');
}

}

function getmessage()
{
// walk the 'file'-tags and make code with link to player.swf:
// <a href="player.swf?file=FILE">TITLE</a><br>DESC<br><hr>
for (var i=0; i<xmlDoc.getElementsByTagName("file").length; ++i)
{
output += '<a href="player.swf?file='
+ xmlDoc.getElementsByTagName('file').childNodes[0].nodeValue
+ '">'
+ xmlDoc.getElementsByTagName('title').childNodes[0].nodeValue
+ '</a><br>\n'
+ xmlDoc.getElementsByTagName('desc').childNodes[0].nodeValue
+ '<br><hr>\n';
}
document.getElementById('vids').innerHTML = output;}

</script>
</head>
<body onload="loadXML()">
<span id="vids"></span>
</body>
</html>


Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top