简体   繁体   English

无法使用JavaScript将多个条目从xml解析为html

[英]Unable to parse multiple entries from xml to html with javascript

What I'm trying to do is to parse multiple entries from xml to html! 我正在尝试做的是将多个条目从xml解析为html! This is script which i wrote to do that: 这是我为此写的脚本:

<body>  
<script type="text/javascript">
    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }
    else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", "distros.xml", false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;

    var counter=0;
    for(i=0; i<30; i++){
        var level=xmlDoc.getElementsByTagName("for")[i].childNodes[0].nodeValue;
        if(level=="Beginners"){
            counter++;
        }
    }

    for(i=0; i<counter; i++){
        document.write('<div id="page">');
            document.write('<div id="page-top">');
                document.write('<div id="page-bottom">');
                    document.write('<div id="content">');
                        document.write('<div class="post">');
                            document.write('<h2 class="title"><span id="'name'+i"></span></h2>');
// This is the line                                                      ^^^^^^^^
//  with the error                                                       ||||||||
//-------------------------------------------------------------------------------
                            document.write('<div class="entry">');
                                document.write('<p><b>Based on: </b> <span id="'based_on'+i"></span><br />');
                                document.write('<b>Architecture: </b> <span id="'architecture'+i"></span><br />');
                                document.write('<b>Desktop: </b> <span id="'desktop'+i"></span><br />');
                                document.write('<b>Origin: </b> <span id="'origin'+i"></span><br />');
                                document.write('<b>Description: </b> <span id="'description'+i"></span></p>');
                            document.write('</div>');
                        document.write('</div>');
                    document.write('</div>');
                document.write('</div>');
            document.write('</div>');
        document.write('</div>');
    }
</script>

<script type="text/javascript">
    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }
    else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", "distros.xml", false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;

    for(i=0; i<30; i++){

        var level=xmlDoc.getElementsByTagName("for")[i].childNodes[0].nodeValue;

        if(level=="Beginners"){
            document.getElementById("'name'+i").innerHTML=
            xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue;
            document.getElementById("'based_on'+i").innerHTML=
            xmlDoc.getElementsByTagName("based_on")[i].childNodes[0].nodeValue;
            document.getElementById("'architecture'+i").innerHTML=
            xmlDoc.getElementsByTagName("architecture")[i].childNodes[0].nodeValue;
            document.getElementById("'desktop'+i").innerHTML=
            xmlDoc.getElementsByTagName("desktop")[i].childNodes[0].nodeValue;
            document.getElementById("'origin'+i").innerHTML=
            xmlDoc.getElementsByTagName("origin")[i].childNodes[0].nodeValue;
            document.getElementById("'description'+i").innerHTML=
            xmlDoc.getElementsByTagName("description")[i].childNodes[0].nodeValue;
        }
    }
</script>

well... guess what, it's not working! 好吧...猜怎么着,它不起作用! This is the small part of xml file I'm trying to parse: 这是我要解析的xml文件的一小部分:

<linux>

<distributions>

    <distribution>
        <name>Mint</name>
        <for>Beginners</for>
        <based_on>Debian, Ubuntu</based_on>
        <architecture>i386, x86_64</architecture>
        <desktop>Fluxbox, GNOME, KDE, LXDE, Xfce</desktop>
        <origin>Ireland</origin>
        <description>Linux Mint is an Ubuntu-based distribution whose goal is to provide a more complete out-of-the-box experience by including browser plugins, media codecs, support for DVD playback, Java and other components. It also adds a custom desktop and menus, several unique configuration tools, and a web-based package installation interface. Linux Mint is compatible with Ubuntu software repositories.</description>
    </distribution>

    <distribution>
        <name>Ubuntu</name>
        <for>Beginners</for>
        <based_on>Debian</based_on>
        <architecture>i686, x86_64</architecture>
        <desktop>KDE, LXDE, Unity, Xfce</desktop>
        <origin>Isle of Man</origin>
        <description>Ubuntu is a complete desktop Linux operating system, freely available with both community and professional support. The Ubuntu community is built on the ideas enshrined in the Ubuntu Manifesto: that software should be available free of charge, that software tools should be usable by people in their local language and despite any disabilities, and that people should have the freedom to customise and alter their software in whatever way they see fit. "Ubuntu" is an ancient African word, meaning "humanity to others". The Ubuntu distribution brings the spirit of Ubuntu to the software world.</description>
    </distribution>

    <distribution>
        <name>Fedora</name>
        <for>Experienced</for>
        <based_on>Independent</based_on>
        <architecture>i686, x86_64</architecture>
        <desktop>GNOME, KDE, LXDE, Openbox, Xfce</desktop>
        <origin>USA</origin>
        <description>The Fedora Project is an openly-developed project designed by Red Hat, open for general participation, led by a meritocracy, following a set of project objectives. The goal of The Fedora Project is to work with the Linux community to build a complete, general purpose operating system exclusively from open source software. Development will be done in a public forum. The project will produce time-based releases of Fedora about 2-3 times a year, with a public release schedule. The Red Hat engineering team will continue to participate in building Fedora and will invite and encourage more outside participation than in past releases. By using this more open process, we hope to provide an operating system more in line with the ideals of free software and more appealing to the open source community.</description>
    </distribution>

    <distribution>
        <name>Debian</name>
        <for>Advanced</for>
        <based_on>Independent</based_on>
        <architecture>amd64, armel, hppa, ia64, i386, mips, mipsel, powerpc, s390, sparc64, x86_64</architecture>
        <desktop>AfterStep, Blackbox, Fluxbox, GNOME, IceWM, KDE, LXDE, Openbox, WMaker, Xfce</desktop>
        <origin>Global</origin>
        <description>The Debian Project is an association of individuals who have made common cause to create a free operating system. This operating system is called Debian GNU/Linux, or simply Debian for short. Debian systems currently use the Linux kernel. Linux is a completely free piece of software started by Linus Torvalds and supported by thousands of programmers worldwide. Of course, the thing that people want is application software: programs to help them get what they want to do done, from editing documents to running a business to playing games to writing more software. Debian comes with over 20,000 packages (precompiled software that is bundled up in a nice format for easy installation on your machine) - all of it free. It's a bit like a tower. At the base is the kernel. On top of that are all the basic tools. Next is all the software that you run on the computer. At the top of the tower is Debian -- carefully organizing and fitting everything so it all works together.</description>
    </distribution>
</distributions>

Take a look at this: 看看这个:

var brojac=0:
var lvl=xmlDoc.getElementsByTagName("for")[i].childNodes[0].nodeValue;
//---------------------------------------- ^
for(i=0; i<30; i++){
    if(lvl=="Beginners"){
        brojac++;
    }
}

for(j=0; j<brojac; j++){

The marked i is unknown at this place, so the whole line will fail.(usually the whole script should stop from executing there, you should receive an exception) 标有“ i”的位置在此位置未知,因此整行将失败。(通常,整个脚本应从此处停止执行,您应收到一个异常)

The loop that should increment brojac will do nothing( lvl will never be "Beginners") and the last of the lines will never run, because brojac will always be 0 应该增加brojac的循环不会执行任何操作( lvl永远不会是“初学者”),并且最后一行也永远不会运行,因为brojac始终为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM