Javascript和XML - Uncaught TypeError: Cannot read property
Javascript and XML - Uncaught TypeError: Cannot read property 'childNodes' of undefined
我正在尝试创建一个web应用程序,从XML文件中提取并在表中显示信息。我的XML文件是从PHP脚本生成的。我遇到的问题是数据没有显示在表中,当我检查谷歌chrome开发控制台时,它说我有错误:
Uncaught TypeError:无法读取未定义on的属性'childNodes'线37 .
这是我试图使用
的代码<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse:collapse;
}
th, td {
padding: 5px;
}
</style>
</head>
<body onload="init()">
<script>
function init(){
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","http://hankyranky.com/dev/includes/reviewxml.gen.php?bathroomid=1",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table><tr><th>Artist</th><th>Title</th></tr>");
var x=xmlDoc.getElementsByTagName("review");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("bathroomid")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("rating")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
}
</script>
</body>
</html>
下面是XML文件的输出:
<ratings>
<rating>
<id>1</id>
<bathroomid>1</bathroomid>
<review>5</review>
</rating>
<rating>
<id>2</id>
<bathroomid>1</bathroomid>
<review>4</review>
</rating>
</ratings>
如果有人需要它,这里是XML生成器的代码
<?php
require("db.config.php");
// Start XML file, create parent node
$xml = new SimpleXMLElement('<ratings/>');
$bathroomid = ($_GET['bathroomid']);
// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can''t use db : ' . mysql_error());
}
// Select all the rows in the bathrooms table
$query = "SELECT * FROM ratings WHERE bathroomid = '".$bathroomid."'";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$rating = $xml->addChild('rating');
$rating->addChild('id', $row['id']);
$rating->addChild('bathroomid', $row['bathroomid']);
$rating->addChild('review', $row['rating']);
}
echo $xml->saveXML();
?>
谢谢大家,非常感谢大家的帮助!
像bathroomid
和review
这样的标签是rating
元素的子元素,而不是review
元素,所以x[i].getElementsByTagName("bathroomid")
不会返回任何元素。
它们都是rating
元素的子元素,所以像
var x=xmlDoc.getElementsByTagName("rating");
看起来您在Javascript中混淆了rating
和review
元素。下面的代码块:
var x=xmlDoc.getElementsByTagName("review");
for (i=0;i<x.length;i++) {
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("bathroomid")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("rating")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
应:var x=xmlDoc.getElementsByTagName("rating");
for (i=0;i<x.length;i++) {
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("bathroomid")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("review")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
相关文章:
- 未捕获TypeError: Cannot read property 'geocode'未定义的'
- SailsJs中的错误.TypeError: Cannot read property '的定义
- 未捕获的TypeError: Cannot read property 'top'的定义
- 未捕获的TypeError: Cannot read property '未定义的数据表
- 未捕获的TypeError: Cannot read property 'timing'的定义
- 未捕获的TypeError: Cannot read property 'checked'零
- 未捕获的TypeError: Cannot read property 'top'在引导中单击导航栏时未
- “未捕获的TypeError: Cannot read”属性是什么?JS误差均值
- Extjs5 treepanel [Uncaught TypeError: Cannot read property &
- TypeError: Cannot read property 'submit'添加到购物车'
- 添加自定义jquery.验证方法导致Uncaught TypeError: Cannot read property &
- Vue 2和Vue- resource [Cannot read property 'get'未定义的(
- asp:textbox Uncaught TypeError: Cannot read property 'va
- TypeError: Cannot read property 'search' of undefine
- 读取json时出错未捕获类型错误:Cannot read property 'output'的定义
- 未捕获的TypeError: Cannot read property 'current'在选择任何选项
- 未捕获的TypeError: Cannot read property '3'的定义
- jquery-ui.js:12443未捕获的TypeError: Cannot read property 'a
- jquery Uncaught TypeError: Cannot read property 'left
- 未捕获类型错误:Cannot read property 'print'的定义