PHP while loop para generar un archivo KML

Given a MySQL table of real estate data, I would like to generate a KML file with the following output:

<?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>

This is the code I have so far. As you can see, I am having trouble writting a loop that will structure my KML as shown above. Any help is highly appreciated!

<?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";
?>

preguntado el 01 de julio de 12 a las 20:07

OK and what does this produce? -

1 Respuestas

createElement() es un método de la DOMDocument class (the root document $dom) and not of DOMElement, to my knowledge (and based on my reading of la documentación.

You have created 3 new elements, but you have not appended any of them as children of $dnode. Utilizar $dom->createElement() for each and append it to the correct $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";

To force a .kml filename, use a Content-Disposition encabezamiento:

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

Respondido 01 Jul 12, 21:07

Thanks Michael, your explanation makes sense and the code seems to work now. Can you explain how I can insert data from a MySQL table into each node? For example, Name node (insert address), Coordinates node (Insert Lat/Long Coords), etc. Thank you! - AME

Last question, the code currently returns a .PHP file. How can I alter the code to return a .KML file? Thanks! - AME

@AME add a content-disposition header as I have above. - Michael Berkowski

after referencing the KML documentation, I see that the coordinates node must be child of the 'Point' node in order for the KML file to properly structured. - AME

@AME So I edited it above create a Point node and make Coordinates a child of it. - Michael Berkowski

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.