I am working on a simple movie database to practise my JavaScript and have encountered difficulty extracting information from the xml file. Let me explain my scenario. Sorry if it gets a bit long-winded!
The interface I have created has three columns which each serve the following functions:
Column 1 -> The user selects a film of their choice.
Column 2 -> Once the user has selected a film in Column 1 then more information about the film appears in this column. This includes title, director and cast. The user has the option of then selecting a cast member to find out information about them.
Column 3 -> Once the user has selected a cast member in Column 2 then their name and a picture of them appears in this column (using the tag). This information in this column also includes the film title, film artwork (applied tag).
But the difficulty I have encountered is as follows - I have a rough idea of how to update column 2 in real time to reflect the changes in Column 1.
The method I am using for acquiring the relevant information in Column 2 is creating an array then using the indexOf to retrieve a the details of a specific film. I know this method is wrong and it would be better to pull the relevant information from the xml file.
How do I use the idx from the Column 1 selection to pull out the relevant information to put in Column 2 and 3?
Here is what I've done so far:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function XMLload() {
jQuery.post(url, function(data) {getxml(data)}, 'xml');
}
function dataFromTag(node, t) {
var d = node.getElementsByTagName(t);
if (d.length == 0) return ('');
return (d[0].firstChild.nodeValue);
}
jQuery(document).ready(XMLload);
var url = 'movie.xml';
var xmlMovies;
var aryMovieList = [];
var xmlActors;
var aryActors = [];
var iframeOpen = '<html><head><\/head><body>'
var iframeClose = '<\/select><\/form><\/body><\/html>'
function getxml(xmldoc) {
xmlMovies = xmldoc.getElementsByTagName('movie');
var hstr = iframeOpen;
hstr += '<select size="' + xmlMovies.length + '" onchange="parent.actors(this.selectedIndex);">';
for (var MovieID = 0; MovieID < xmlMovies.length; MovieID++) {
aryMovieList[aryMovieList.length] = dataFromTag(xmlMovies[MovieID], "title");
xmlActors = xmlMovies[MovieID].getElementsByTagName("actor");
for (var ActorID = 0; ActorID < xmlActors.length; ActorID++) {
aryActors[aryActors.length] = dataFromTag(xmlMovies[MovieID], "director") + "/" + dataFromTag(xmlMovies[MovieID], "title") + "/" + dataFromTag(xmlActors[ActorID], "name");
}
hstr += '<option>' + aryMovieList[MovieID] + '<\/option>';
}
hstr += iframeClose;
// test for aryMovieList
// alert(aryMovieList);
// test for aryActors
// alert(aryActors);
with(document.getElementById('movies').contentDocument) {
open();
write(hstr);
close();
}
}
function actors(idx) {
var hstr = iframeOpen;
var selectActor = [];
hstr += 'title: ' + dataFromTag(xmlMovies[idx], 'title');
hstr += '<br>';
hstr += 'director: ' + dataFromTag(xmlMovies[idx], 'director');
hstr += '<br>';
for (var i = 0; i < aryActors.length; i++) {
var aryActorList = aryActors[i].indexOf(dataFromTag(xmlMovies[idx], 'director') + '/' + dataFromTag(xmlMovies[idx], 'title'));
if (aryActorList >= 0) {
selectActor[selectActor.length] = i;
}
}
// alert(selectActor);
hstr += '<select size="' + selectActor.length + '" onchange="parent.info(this.selectedIndex);">';
for (var i = 0; i < aryActors.length; i++) {
var aryActorList = aryActors[i].indexOf(dataFromTag(xmlMovies[idx], 'director') + '/' + dataFromTag(xmlMovies[idx], 'title'));
if (aryActorList >= 0) {
hstr += '<option>' + aryActors[i].substring(aryActors[i].lastIndexOf("/") + 1) + '<\/option>';
}
}
hstr += iframeClose;
with(document.getElementById('actors').contentDocument) {
open();
write(hstr);
close();
}
}
function info(idx) {
var hstr = iframeOpen;
hstr += '';
hstr += iframeClose;
with(document.getElementById('info').contentDocument) {
open();
write(hstr);
close();
}
}
</script>
movie.xml
<movies>
<movie>
<title>Match Point</title>
<director>Woody Allen</director>
<image>Match-Point.jpg</image>
<actor>
<name>Scarlett Johansson</name>
<image>Scarlett-Johansson.jpg</image>
</actor>
<actor>
<name>Brian Cox</name>
<image>Brian-Cox.jpg</image>
</actor>
<actor>
<name>Matthew Goode</name>
<image>Matthew-Goode.jpg</image>
</actor>
<actor>
<name>Penelope Wilton</name>
<image>Penelope-Wilton.jpg</image>
</actor>
</movie>
<movie>
<title>Inception</title>
<director>Christopher Nolan</director>
<artwork>Inception.jpg</artwork>
<actor>
<name>Leonardo DiCaprio</name>
<image>Leonardo-DiCaprio.jpg</image>
</actor>
<actor>
<name>Ken Watanabe</name>
<image>Ken-Watanabe.jpg</image>
</actor>
<actor>
<name>Joseph Gordon-Levitt</name>
<image>Joseph-Gordon-Levitt.jpg</image>
</actor>
<actor>
<name>Marion Cotillard</name>
<image>Marion-Cotillard.jpg</image>
</actor>
<actor>
<name>Ellen Page</name>
<image>Ellen-Page.jpg</image>
</actor>
<actor>
<name>Tom Hardy</name>
<image>Tom-Hardy.jpg</image>
</actor>
</movie>
<movie>
<title>Blade II</title>
<director>Guillermo del Toro</director>
<artwork>Blade-II.jpg</artwork>
<actor>
<name>Wesley Snipes</name>
<image>Wesley-Snipes.jpg</image>
</actor>
<actor>
<name>Kris Kristofferson</name>
<image>Kris-Kristofferson.jpg</image>
</actor>
<actor>
<name>Ron Perlman</name>
<image>Ron-Perlman.jpg</image>
</actor>
<actor>
<name>Leonor Varela</name>
<image>Leonor-Varela.jpg</image>
</actor>
<actor>
<name>Norman Reedus</name>
<image>Norman-Reedus.jpg</image>
</actor>
</movie>
<movie>
<title>Pulp Fiction</title>
<director>Quentin Tarantino</director>
<artwork>Pulp-Fiction.jpg</artwork>
<actor>
<name>John Travolta</name>
<image>John-Travolta.jpg</image>
</actor>
<actor>
<name>Samuel L Jackson</name>
<image>Samuel-L-Jackson.jpg</image>
</actor>
<actor>
<name>Uma Thurman</name>
<image>Uma-Thurman.jpg</image>
</actor>
<actor>
<name>Harvey Keitel</name>
<image>Harvey-Keitel.jpg</image>
</actor>
</movie>
<movie>
<title>Avatar</title>
<director>James Cameron</director>
<artwork>Avatar.jpg</artwork>
<actor>
<name>Sam Worthington</name>
<image>Sam-Worthington.jpg</image>
</actor>
<actor>
<name>Zoe Saldana</name>
<image>Zoe-Saldana.jpg</image>
</actor>
<actor>
<name>Stephen Lang</name>
<image>Stephen-Lang.jpg</image>
</actor>
<actor>
<name>Michelle Rodriguez</name>
<image>Michelle-Rodriguez.jpg</image>
</actor>
</movie>
</movies>
Thanks for taking the patience to bear with my question!