【问题标题】:PHP export as XML error on line 2 at column 1: Extra content at the end of the documentPHP 在第 1 列第 2 行导出为 XML 错误:文档末尾的额外内容
【发布时间】:2017-08-27 16:27:56
【问题描述】:

当我在浏览器中看到页面源代码时,缺少“id”字段
代码如下和浏览器中页面源的结果

// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);



// Select all the rows in the markers table
$query = "SELECT * FROM markers 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->create_element("marker");
  $newnode = $parnode->append_child($node);

  $newnode->set_attribute("id", $row['id']);
  $newnode->set_attribute("name", $row['name']);
  $newnode->set_attribute("address", $row['address']);
  $newnode->set_attribute("lat", $row['lat']);
  $newnode->set_attribute("lng", $row['lng']);
  $newnode->set_attribute("type", $row['type']);
}

$xmlfile = $doc->dump_mem();
echo $xmlfile;

?>

以下是来自 Chrome 的页面源视图的输出

<markers><marker name="Love.Fish" lat="-33.861034" lng="151.171936" /><marker name="Young Henrys" lat="-33.898113" lng="151.174469" /><marker name="Hunter Gatherer" lat="-33.840282" lng="151.207474" /><marker name="The Potting Shed" lat="-33.910751" lng="151.194168" /><marker name="Nomad" lat="-33.879917" lng="151.210449" /><marker name="Three Blue Ducks" lat="-33.906357" lng="151.263763" /></markers>

一些样本数据

【问题讨论】:

  • 请显示几行您的 markers 表。是否有空的 id
  • intable 标记有 id 列,它是 1 2 3 谢谢我编辑问题添加图像,我捕获表格标记的屏幕图像谢谢
  • 这看起来像 PHP 4 DOM。你真的在某处使用它吗?
  • 似乎不仅缺少 id 属性,而且还缺少地址和类型……?您是否首先验证了 $row 中的相应值实际上已设置?如果您实际上将这些属性设置为“空”或 false,那么当整个内容被写为 XML 时,可能会被忽略...
  • 首先,使用不同的 MySQL API,例如 mysqli_ 或 PDO,因为 mysql_deprecated in PHP 5.5 and entirely removed in PHP 7。其次,删除 @ 符号,因为这会抑制任何错误。第三,尝试回显 $row 值并查看输出。

标签: php xml


【解决方案1】:
    $mysqli = new mysqli("localhost", "caico", "posxxxxt8", "ca888888co");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}



// Set the active MySQL database



// Select all the rows in the markers table
header("Content-type: text/xml");
$query = "SELECT * FROM markers WHERE 1";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        // Add to XML document node
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("id",$row['id']);
  $newnode->setAttribute("name",$row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("type", $row['type']);
    }

    /* free result set */
    $result->free();
}

【讨论】:

  • 改成mysqli后就可以了,谢谢大家的帮助
猜你喜欢
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
相关资源
最近更新 更多