如何使用JS从XML中提取数据

时间:2011-11-27 18:06:51

标签: javascript xml

我正在开发一个简单的电影数据库来练习我的JavaScript,并且在从xml文件中提取信息时遇到了困难。让我解释一下我的情景。对不起,如果它有点啰嗦!

我创建的界面有三列,每列都提供以下功能:

第1栏 - >用户选择他们选择的电影。

第2栏 - >一旦用户在第1列中选择了一部电影,就会在此栏中显示有关该电影的更多信息。这包括头衔,导演和演员。用户可以选择选择演员以查找有关他们的信息。

第3栏 - >一旦用户在第2列中选择了演员,那么他们的名字和图片就会显示在此栏中(使用标签)。此栏目中的信息还包括电影片名,电影作品(应用标签)。

但我遇到的困难如下 - 我大致了解如何实时更新第2列以反映第1列的变化。

我用于获取第2列中相关信息的方法是创建一个数组,然后使用indexOf检索特定电影的详细信息。我知道这种方法是错误的,最好从xml文件中提取相关信息。

如何使用第1列选择中的idx提取相关信息以放入第2列和第3列?

这是我到目前为止所做的:

 <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>

感谢您耐心忍受我的问题!

1 个答案:

答案 0 :(得分:1)

获胜的jQuery! http://api.jquery.com/jQuery.parseXML/

  

然后可以将此文档传递给jQuery以创建典型的jQuery   可以遍历和操纵的对象。

如果您熟悉使用jQuery,那么将解析后的数据作为典型的jQuery对象进行遍历应该没有任何问题。