2012-07-01 105 views
2

鉴于房地产数据的MySQL表,我想生成KML有以下输出文件:PHP while循环产生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"; 
?> 
+0

好的,这是什么产生的? – craig1231

回答

3

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

您已经创建了3个新的元素,但你有没有附加任何人作为$dnode孩子使用$dom->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"); 
+0

谢谢迈克尔,你的解释是有道理的,代码似乎现在工作。你能解释一下如何将MySQL表中的数据插入到每个节点中?例如,名称节点(插入地址),坐标节点(插入纬度/长度坐标)等。谢谢! – AME

+0

@AME请参阅上面添加的'nodeValue'作业.... –

+0

最后一个问题,代码当前返回一个.PHP文件。如何修改代码以返回.KML文件?谢谢! – AME