2012-07-01 62 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