我试图从数据库创建 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)