日期:2014-05-17  浏览次数:20510 次

responseXML无法取得结果
代码如下:
<?php 
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");

$q = $_GET["q"];


// include_once("../public/dbManager.php");
// include_once("../public/stringConvert.php");;


$con = mysql_connect('localhost', 'root', 'root');
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("ajax_demo", $con);

$sql = "SELECT * FROM user WHERE id = " . $q . "";

$result = mysql_query($sql);

echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<person>';
while ($row = mysql_fetch_array($result)) {
  echo "<firstname>" . $row['FirstName'] . "</firstname>";
  echo "<lastname>" . $row['LastName'] . "</lastname>";
  echo "<age>" . $row['Age'] . "</age>";
  echo "<hometown>" . $row['Hometown'] . "</hometown>";
  echo "<job>" . $row['Job'] . "</job>";
}
echo "</person>";

mysql_close($con);
?>

上述代码只要引入了其他文件,注释部分
// include_once("../public/dbManager.php");
// include_once("../public/stringConvert.php");
所示。
responseXML 便无法获取结果,并且执行到xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
处程序便不再执行。
如果去掉include语句,程序运行正常。
上述引入的文件没有任何页面输出。
本人刚接触php ,请高手解答此为何故, 谢谢。


------解决方案--------------------
先看看XML是否正确返回的再说,调试功夫不能没有吧。
------解决方案--------------------
PHP数组与xml相互转换封装函数:xmlparse.php
PHP code

#将数组转为xml
include(dirname(__FILE__)."/xmlparse.php");
$listtags = explode(" ","name");
$arr = array(1,"name"=>"SOM","admin",100,array("name"=>"TOM","Lily"));
$contXML = dump_xml_config($arr, "xml", "utf-8");
//放入文件
//file_put_contents_safe("/path/test.xml",$contXML,"w");
echo $contXML;
#源码
#<?xml version="1.0" encoding="utf-8"?>
#<xml>
#    <0>1</0>
#    <name>SOM</name>
#    <1>admin</1>
#    <2>100</2>
#    <3>
#        <name>TOM</name>
#        <0>Lily</0>
#    </3>
#</xml>

------解决方案--------------------
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
将这两句注释掉。将包含文件打开。打开错误提示功能,看是否报错。