使用PHP解析KML文件

时间:2011-12-01 13:45:40

标签: php parsing google-maps simplexml kml

有没有办法用simple_xml_load_file(“*。kml”)解析google maps * .kml文件?

我需要保存在我的KML文件中注册的每个多边形的数据库名称和坐标。 在我的PHP脚本上,simple_xml_load_file(“*。kml”)返回false,所以我无法读取它。

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

我需要每个“地标”的“名称”和“坐标”值。

3 个答案:

答案 0 :(得分:2)

如果XML结构与您发布的结构相同,您可以尝试: -

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

}

示例: -

SimpleXMLElement Object
(
    [name] => 
                    name

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

                )

        )

)

答案 1 :(得分:1)

第一行:

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

告诉PHP这是一个用UTF-8编码的文档,但是你的错误说它没有编码为UTF-8。这是您使用文本编辑器创建的文档吗?如果是这样,您通常可以使用编辑器将其保存为UTF-8。或者您可以使用PHP来检测编码并更改第一行。

答案 2 :(得分:1)

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>

这是我的PHP代码:

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