我尝试从数据库创建 XML,但在 WampServer 中运行 PHP 代码时遇到错误:
XML Parsing Error: junk after document element
Line Number 2, Column 1:<font size='1'><table class='xdebug-error'
dir='ltr' border='1' cellspacing='0' cellpadding='1'>
我的代码来自 code.google:
{<?php
require("phpsqlajax_dbinfo.php");
// Start XML file, create parent node
$doc = new DOMDocument('1.0', 'iso-8859-1');
$node = $doc->createElement("place");
$parnode = $doc->appendChild($node);
// 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 place table
$query = "SELECT * FROM place WHERE 1";
$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
$node = $doc->createElement("place");
$newnode = $parnode->appendChild($node);
$newnode->set_attribute("place_id", $row['place_id']);
$newnode->set_attribute("p_name", $row['p_name']);
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
$newnode->set_attribute("addr", $row['addr']);
$newnode->set_attribute("tel", $row['tel']);
$newnode->set_attribute("category", $row['category']);
$newnode->set_attribute("description", $row['description']);
}
$xmlfile = $doc->dump_mem();
echo $xmlfile;
?>
这是表格:
place_id(int),
p_name(varchar),
lat(float),
lng(float),
addr(varchar),
tel(int),
category(varchar),
description(varchar)
最佳答案
您的代码产生错误。正如消息中提到的xdebug-error
,这表明您在某处放置了错误,而不是有效的代码。
尝试将 XML 保存到某个文件或注释 header("Content-type: text/xml");
以查看发生了什么。
关于php - XML解析错误: junk after document element__wampserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303035/