【问题标题】:PHP while loop to generate a KML filePHP while循环生成KML文件
【发布时间】:2012-07-01 19:24:53
【问题描述】:

给定一个房地产数据的 MySQL 表,我想生成一个具有以下输出的 KML 文件:

<?xml version="1.0" encoding="UTF-8"?>
 <kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark>
      <name>Property Address Pulled from Address Field</name>
      <description>
      Some descriptive data pulled from table inserted here.
      </description>
      <Point>
        <coordinates>Latitude FROM Lat,Long FROM Lng</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

这是我到目前为止的代码。如您所见,我无法编写一个循环来构造我的 KML,如上所示。非常感谢任何帮助!

<?php 
require("phpsqlgeocode_dbinfo.php");

// Start XML file, create parent node
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true; //This was added from the PHP Doc. Nice output.

// Creates the root KML element and appends it to the root document.
$node = $dom->createElementNS('http://www.opengis.net/kml/2.2', 'kml');
$parNode = $dom->appendChild($node);

// Creates a KML Document element and append it to the KML element.
$dnode = $dom->createElement('Document');
$docNode = $parNode->appendChild($dnode);

// 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());
}

// Search the rows in the markers table
$query = sprintf("SELECT * FROM markers");
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

header("Content-type: application/vnd.google-earth.kml+xml");

// Iterate through the rows, adding KML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  $dnode = $dom->createElement("Placemark");
  $newnode = $docNode->appendChild($dnode);
  $newnode = $newnode->createElement("Name");
  $newnode = $newnode->createElement("Description");  
  $newnode = $newnode->createElement("Coordinates");
  }
echo $dom->saveXML() . "\n";
?>

【问题讨论】:

  • 好的,这会产生什么?

标签: php mysql xml kml


【解决方案1】:

createElement()DOMDocument 类(根文档$dom)的方法,而不是DOMElement 的方法,据我所知(并基于我对the documentation 的阅读。

您已经创建了 3 个新元素,但您还没有将它们中的任何一个附加为 $dnode 的子元素。对每个使用$dom-&gt;createElement() 并将其附加到正确的$dnode (Placemark)

// Iterate through the rows, adding KML nodes for each
while ($row = mysql_fetch_assoc($result)){
    $dnode = $dom->createElement("Placemark");
    $newnode = $docNode->appendChild($dnode);

    // Append each $newnode after creating from $dom
    // Set its nodeValue to a column from your fetch call
    // before appending it to the parent node
    // Substitute your correct column names from mysql_fetch_assoc()
    $newnode = $dom->createElement("Name");
    $newnode->nodeValue = $row['Name'];
    $dnode->appendChild($newnode);

    $newnode = $dom->createElement("Description");  
    $newnode->nodeValue = $row['Description'];
    $dnode->appendChild($newnode);

    //Coordinates are a child node of the 'Point' node       
    $pointnode = $dom->creteElement("Point");
    $dnode-appendChild($pointnode);

    $coordsnode = $dom->createElement("Coordinates");
    $coordsnode->nodeValue = $row['Coordinates'];
    $pointnode->appendChild($coordsnode);
}
echo $dom->saveXML() . "\n";

要强制使用 .kml 文件名,请使用 Content-Disposition 标头:

header("Content-type: application/vnd.google-earth.kml+xml");
header("Content-disposition: inline; filename=$somefilename.kml");

【讨论】:

  • 谢谢迈克尔,你的解释很有意义,代码现在似乎可以工作了。您能解释一下如何将 MySQL 表中的数据插入每个节点吗?比如Name节点(插入地址),Coordinates节点(插入Lat/Long Coords)等。谢谢!
  • 最后一个问题,代码目前返回一个.PHP文件。如何更改代码以返回 .KML 文件?谢谢!
  • @AME 添加一个 content-disposition 标头,就像我在上面一样。
  • 在参考 KML 文档后,我发现坐标节点必须是“点”节点的子节点,才能正确构建 KML 文件。
  • @AME 所以我在上面编辑它创建一个 Point 节点并使 Coordinates 成为它的子节点。
猜你喜欢
  • 1970-01-01
  • 2012-02-24
  • 2023-02-18
  • 2018-07-18
  • 1970-01-01
  • 2023-04-06
  • 2016-11-27
  • 1970-01-01
  • 2013-07-12
相关资源
最近更新 更多