我目前正在关注此事:
https://developers.google.com/maps/articles/phpsqlajax_v3#domfunctions
这引出了我的代码:
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Select all the rows in the markers table
$query = "SELECT latitude, longitude FROM people";
$result = $db->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 = $result->fetch_assoc()){
$lat = $row["latitude"];
$lng = $row["longitude"];
echo $lat;
echo $lng;
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("lat", $row['latitude']);
$newnode->setAttribute("lng", $row['longitude']);
}
$xmlfile = $dom->saveXML();
echo $xmlfile;
?>
然而,这给了我以下错误。
XML解析错误:格式不正确位置:myfile行号1, 第35栏:53.554825-6.7911150.0000000.000000
如果我删除header()行,那么我在浏览器中获得经纬度。需要进行哪些修改才能使xml良好地运行?
答案 0 :(得分:0)
<?php
$query = "SELECT latitude, longitude FROM people";
$result = $db->query($query);
$fields = $result->fetch_fields();
if( $result ) {
$dom = new DOMDocument('1.0','utf-8');
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
while( $row = $result->fetch_assoc() ){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
/* Add attribute for any/all fields in recordset dynamically */
foreach( $fields as $field ) $newnode->setAttribute( $field->name, $row[ $field->name ] );
}
header("Content-type: text/xml");
exit( $dom->saveXML() );
}
exit('error');
?>