PHP SimpleXMLElement - 在元素中访问值

我正在使用 SimpleXMLElement 类在 php 中读取 KML 文件。树中的最后一个元素看起来像下面的例子(php 对象的 var_dump 说 $element):


object(SimpleXMLElement)#2 (2) {

  ["@attributes"]=>

  array(1) {

    ["name"]=>

    string(10) "featurecla"

  }

  [0]=>

  string(15) "Admin-0 country"

}

如何在 php 中访问“Admin-0 country”值?


我已经尝试了 $element->children() 和 $element->attributes() 并且只能访问“featurecla”。


Qyouu
浏览 104回答 1
1回答

慕桂英4014372

我找到了解决方案事实证明我没有正确检查 KML 文件,只是查看 php var_dump 输出以了解数据结构。KML 文件数据如下所示:<Folder><name>ne_50m_admin_0_countries</name>&nbsp; <Placemark>&nbsp; &nbsp; <name>Zimbabwe</name>&nbsp; &nbsp; <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>&nbsp; &nbsp; <ExtendedData><SchemaData schemaUrl="#ne_50m_admin_0_countries">&nbsp; &nbsp; &nbsp; &nbsp; <SimpleData name="featurecla">Admin-0 country</SimpleData>在 PHP 代码中,我使用下面的代码访问 SimpleData:foreach($xmlContent->Document->Folder->children() as $Placemark){&nbsp; &nbsp; &nbsp; &nbsp; print_r("<h1>".(string)$Placemark->name."</h1>");&nbsp; &nbsp; &nbsp; &nbsp; foreach ($Placemark->ExtendedData->SchemaData->SimpleData as $element){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var_dump($element); //output shown above&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var_dump($element->children());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var_dump($element->attributes());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }查看原始 kml 文件后,我能够访问所需的信息:foreach($xmlContent->Document->Folder->children() as $Placemark){&nbsp; &nbsp; &nbsp; &nbsp; print_r("<h1>".(string)$Placemark->name."</h1>");&nbsp; &nbsp; &nbsp; &nbsp; foreach ($Placemark->ExtendedData->SchemaData->SimpleData as $element){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $element->attributes()."=>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $element."<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //output featurecla=>Admin-0 country&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP