用 PHP 解析 KML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8342337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:29:37  来源:igfitidea点击:

Parse KML file with PHP

phpparsinggoogle-mapssimplexmlkml

提问by jbrtrnd

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

有没有办法用 simple_xml_load_file("*.kml") 解析谷歌地图 *.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.

我需要在我的数据库中保存在我的 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>

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

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

采纳答案by Mano Marks

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.

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

回答by Sutechksh

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 代码:

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

回答by ajreal

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

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

$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

                )

        )

)