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!
我正在研究一个简单的电影数据库来练习我的JavaScript,并且在从xml文件中提取信息时遇到了困难。让我解释一下我的情景。对不起,如果它有点啰嗦!
The interface I have created has three columns which each serve the following functions:
我创建的接口有三列,每列都提供以下功能:
Column 1 -> The user selects a film of their choice.
第1列 - >用户选择他们选择的电影。
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.
第2栏 - >一旦用户在第1列中选择了一部电影,就会在此栏中显示有关该电影的更多信息。这包括头衔,导演和演员。用户可以选择选择演员以查找有关他们的信息。
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).
第3列 - >一旦用户在第2列中选择了演员,那么他们的名字和图片就会出现在这一栏中(使用标签)。此栏目中的此信息还包括电影片名,电影作品(应用标签)。
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.
但我遇到的困难如下 - 我大致了解如何实时更新第2列以反映第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.
我用于获取第2列中相关信息的方法是创建一个数组,然后使用indexOf检索特定电影的细节。我知道这种方法是错误的,最好从xml文件中提取相关信息。
How do I use the idx from the Column 1 selection to pull out the relevant information to put in Column 2 and 3?
如何使用第1列选择中的idx来提取相关信息以放入第2列和第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
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!
感谢您耐心忍受我的问题!
1 个解决方案
#1
1
jQuery for the win! http://api.jquery.com/jQuery.parseXML/
jQuery赢了! http://api.jquery.com/jQuery.parseXML/
This document can then be passed to jQuery to create a typical jQuery object that can be traversed and manipulated.
然后可以将此文档传递给jQuery以创建可以遍历和操作的典型jQuery对象。
If you are familiar with using jQuery, then there shouldn't be any problem to traverse the parsed data as a typical jQuery object.
如果您熟悉使用jQuery,那么将解析的数据作为典型的jQuery对象遍历应该没有任何问题。
#1
1
jQuery for the win! http://api.jquery.com/jQuery.parseXML/
jQuery赢了! http://api.jquery.com/jQuery.parseXML/
This document can then be passed to jQuery to create a typical jQuery object that can be traversed and manipulated.
然后可以将此文档传递给jQuery以创建可以遍历和操作的典型jQuery对象。
If you are familiar with using jQuery, then there shouldn't be any problem to traverse the parsed data as a typical jQuery object.
如果您熟悉使用jQuery,那么将解析的数据作为典型的jQuery对象遍历应该没有任何问题。