Parse KML file with PHP

2019-07-11 19:22发布

问题:

Is there a way to parse google maps *.kml file with simple_xml_load_file("*.kml") ?

I need to save in my database name and coordinates of each polygons registered in my KML file. On my PHP script, simple_xml_load_file("*.kml") return false, so I can't read it.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <Document>
        <Schema>
        ...
        </Schema>
        <Style id="FEATURES">
        ...
        </Style>
        <Folder>
            <Placemark>
                <name>
                    name
                </name>
                <Polygon>
                    <LinearRing>
                        <coordinates>
                            coordinates
                        </coordinates>
                    </LinearRing>
                </Polygon>
            </Placemark>
            <Placemark>
                ...
            </Placemark>
        </Folder>
    </Document>
</kml>

I need "name" and "coordinates" values for each "Placemark".

回答1:

The first line:

<?xml version="1.0" encoding="UTF-8"?>

Tells PHP that this is a document encoded in UTF-8, but your error says it is not encoded as UTF-8. Is this a doc you created with a text editor? If so, you can usually use your editor to save it out in UTF-8. Or you can probably use PHP to detect the encoding and change that first line.



回答2:

If the XML strucutre is as what you have posted, you can try :-

$xml = simplexml_load_file(...);
$childs = $xml->Document->Folder->children();
foreach ($childs as $child)
{
    // child object is same as -> Placemark

}

Example :-

SimpleXMLElement Object
(
    [name] => 
                    name

    [Polygon] => SimpleXMLElement Object
        (
            [LinearRing] => SimpleXMLElement Object
                (
                    [coordinates] => 
                            coordinates

                )

        )

)


回答3:

The xml structure is exactly that xml you sent:

For example:

<Document>
<Placemark>
      <name>356HH</name>
      <description>
</description>
      <Polygon><outerBoundaryIs><LinearRing><coordinates>some cordinates here</coordinates></LinearRing></innerBoundaryIs></Polygon>
  <Style><LineStyle><color>ff0000ff</color></LineStyle>  <PolyStyle><fill>0</fill></PolyStyle></Style>
  </Placemark>
  <Placemark>
</document>

And it's my php code:

<?php

$completeurl = "2.xml";
$xml = simplexml_load_file($completeurl);

$placemarks = $xml->Document->Placemark;
$con=mysqli_connect("localhost","root","","j3");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

 $query = '';
 $run='';
for ($i = 0; $i < sizeof($placemarks); $i++) {
    $coordinates = $placemarks[$i]->name;
     $cor_d  =  explode(' ', $placemarks[$i]->Polygon->outerBoundaryIs->LinearRing->coordinates);
     $qtmp=array();
     foreach($cor_d as $value){
          $tmp = explode(',',$value);
          $ttmp=$tmp[1];
          $tmp[1]=$tmp[0];
          $tmp[0]=$ttmp; 
          $qtmp[]= '(' . $tmp[0] . ',' .$tmp[1].')';
     }

    $cor_d = json_encode($qtmp);
    $query .='\''.$coordinates.'\', \''.$cor_d.'\'';
    $run .="INSERT INTO jos_rpl_addon_zipinfo (name, boundary) VALUES (".$query." );";

    //echo $run;
    //break;
}
mysqli_query($con,$run);
//echo $run;

mysqli_close($con);
?>