我正在尝试使用XML文件,并使用HTML在我的网站上写出信息。这对于一个学校项目,老师特意告诉我们使用XML和HTML。我的HTML已经包含代码,图片,文本等,这非常有效。所以我试图将XML代码放在标签中,并将其写出来。 XML文件只是一个表格,其中包含有关我最喜欢的篮球运动员的一些信息。
我已经在互联网上搜索了几天,而且我无法找到有关此主题的任何信息。我访问了W3Schools网站,我尝试使用那里提供的信息。
问题在于,当我使用他们在那里所做的事情时,尝试扩展它,并使其与我自己的标签一起使用,它根本不起作用!例如,当我在XML文件中有两个标签,并尝试将其写出来时,它可以工作。然后我尝试添加一个标签,整个事情就关闭了。唯一出现的是一个小黑方块。
这是我的XML代码:
<?xml version="1.0" encoding="UTF-8"?>
<basketballplayers>
<player>
<name>Steve Nash</name>
<college>Santa Clara</college>
<team>La Lakers</team>
</player>
<player>
<name>Ricky Rubio</name>
<college>Did not attend</college>
<team>Minnesota Timberwolves</team>
</player>
<player>
<name>Michael Jordan</name>
<college>North Carolina</college>
<team>Chicago Bulls</team>
</player>
</basketballplayers>
这是我的HTML代码,只有XML内容:
<script>
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "proving.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
document.write("<table border='1'>");
var x = xmlDoc.getElementsByTagName("player");
for (i = 0; i < x.length; i++) {
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("college")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
document.write(x[i].getElementsByTagName("team")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
</div>
答案 0 :(得分:0)
使用jquery js库,
尝试
$.get("proving.xml", null, null, "xml")
.done(function (xml, textStatus, jqxhr) {
$("body").append("<table id=result border=1></table>");
$(xml).find("player *").each(function (i, el) {
$("#result").append("<tr><td>" + el.textContent + "</td></tr>")
})
})