我不确定为什么这不是XML信息不会打印到屏幕上

时间:2019-06-26 15:23:59

标签: javascript html xml

我已经编写了这段代码,该代码应该将xml文件中的信息打印到每个教师的列表中。我最终希望将所有这些都放到桌子上,但是需要知道如何首先将它们打印到屏幕上。

function init() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            console.log(this.responseXML);
            var faculty = this.responseXML.getElementsByTagName("faculty");
            var strOut = "<ul>";
            for (i = 0; i < faculty.length; i++) {
                var name = faculty[i].getElementsByTagName("name")[0].innerHTML;
                var title = faculty[i].getElementsByTagName("title")[0].innerHTML;
                var office = faculty[i].getElementsByTagName("office")[0].innerHTML;
                var phone = faculty[i].getElementsByTagName("phone")[0].innerHTML;
                var email = faculty[i].getElementsByTagName("email")[0].innerHTML;

                strOut += "<li><a href = " + name + title + "</a></li>";
            }
            strOut += "<ul>";
            document.getElementById("output").innerHTML = strOut;
        }
    };
    xhttp.open("GET", "faculty.xml", true);
    xhttp.send();
}
window.onload = init;

这是XML文件:

<facultyInfo>
    <faculty>
        <name>Prof A</name>
        <title>Professor and Program Coordinator</title>
        <office>CI 555</office>
        <phone>(999-999-9999</phone>
        <email>ProfA@school.edu</email>
    </faculty>
    <faculty>
        <name>Prof B</name>
        <title>Visiting Professor</title>
        <office>CI 333</office>
        <phone>999-999-9999</phone>
        <email>ProfB@school.edu</email>
    </faculty>
</facultyInfo>

1 个答案:

答案 0 :(得分:0)

此行:

strOut += "<li><a href = " + name + title + "</a></li>";

...格式不正确,可能不是您想要的。在href属性的引号之间,在“> <a>末尾处缺少”>“之间,并且在<a></a>之间未放置任何文本,这会导致链接标记位于链接目标位置(href )已设置,但未设置要显示给用户的实际文本。我看不到您的XML中的任何链接(也许是将来的链接),所以现在您可能想要这样的东西:

strOut += '<li><a href="">' + name + ', ' + title + '</a></li>';

以下是您的XML输入的快速演示:

<div id="output"></div>

<script>
var xmlString = '<facultyInfo> <faculty> <name>Prof A</name> <title>Professor and Program Coordinator</title> <office>CI 555</office> <phone>(999-999-9999</phone> <email>ProfA@school.edu</email> </faculty> <faculty> <name>Prof B</name> <title>Visiting Professor</title> <office>CI 333</office> <phone>999-999-9999</phone> <email>ProfB@school.edu</email> </faculty> </facultyInfo>';
var xmlDoc = (new DOMParser()).parseFromString(xmlString,'text/xml');

// var faculty = this.responseXML.getElementsByTagName("faculty");
var faculty = xmlDoc.getElementsByTagName("faculty");
var strOut = "<ul>";

for (i = 0; i < faculty.length; i++){
    var name = faculty[i].getElementsByTagName("name")[0].innerHTML;
    var title = faculty[i].getElementsByTagName("title")[0].innerHTML;
    var office = faculty[i].getElementsByTagName("office")[0].innerHTML;
    var phone = faculty[i].getElementsByTagName("phone")[0].innerHTML;
    var email = faculty[i].getElementsByTagName("email")[0].innerHTML;
    strOut += '<li><a href="">' + name + ', ' + title + '</a></li>';
}
strOut += "<ul>";
document.getElementById("output").innerHTML = strOut;
</script>