Javascript和XML - 未捕获TypeError:无法读取未定义的属性'childNodes'

时间:2015-08-10 05:03:40

标签: javascript php mysql xml

我正在尝试创建一个从XML文件中提取并在表中显示信息的Web应用程序。我拥有的XML文件是从PHP脚本生成的。我遇到的问题是数据没有显示在表格中,当我检查谷歌Chrome开发控制台时,它说我有错误:

  

未捕获的TypeError:无法读取未定义的属性'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();

?>

谢谢大家,真的很感激帮助!

2 个答案:

答案 0 :(得分:3)

"(?<=from |FROM )[\s\S]*?(?= where| WHERE|;)" bathroomid等标记是review元素的子元素,而不是rating元素的子元素,因此review不会返回任何元素。< / p>

相反,每个都是x[i].getElementsByTagName("bathroomid")元素的子元素,因此迭代该元素,如

rating

答案 1 :(得分:0)

您似乎混淆了Javascript中的ratingreview元素。以下代码块:

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>");
}