duan97689 2012-09-08 07:22
浏览 58
已采纳

文档末尾的XML Extra内容(使用PHP SimpleXML)

I want to create a app for search properties with zip code of corresponding areas and locate them on google map.I am testing Google's tutorial to use Maps with PHP/MySql but stuck in XML. Its giving me this error:- This page contains the following errors:

error on line 2 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error.

This is the Code:

    <?php
$username="root";
$password="";
$database="test";

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->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 markers table
$query = "SELECT * FROM markers";
$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 = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("distance", $row['distance']);
}

$smd=$dom->saveXML();
echo $smd;

?>
  • 写回答

1条回答 默认 最新

  • douzha5990 2012-11-27 13:27
    关注

    mostly this error results when no XML Root element is seen or not unique!

    in your case: I guess your XML creation failed with an die() and your external script parsing this PHP output will not recognize it as a valid XML?!

    Also the comment above by StasGrin might be a good point: Remove the blanks in front of in the end! Both will render blanks, but this should also result in an PHP Error / Warning:

    Warning: Cannot modify header information - headers already sent by (output started at …) in …
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?