[英]PHP SimpleXMLElement - accessing values with in an element
I am reading a KML file in php using SimpleXMLElement class.我正在使用 SimpleXMLElement class 在 php 中读取 KML 文件。 The last elements in the tree look like the example below (var_dump of php object say $element):
树中的最后一个元素类似于下面的示例(php object 的 var_dump 说 $element):
object(SimpleXMLElement)#2 (2) {
["@attributes"]=>
array(1) {
["name"]=>
string(10) "featurecla"
}
[0]=>
string(15) "Admin-0 country"
}
How do I access "Admin-0 country" value in php?如何访问 php 中的“Admin-0 country”值?
I have tried both $element->children() and $element->attributes() and only be able to access "featurecla" only.我已经尝试过 $element->children() 和 $element->attributes() 并且只能访问“featurecla”。
I have found the solution我找到了解决方案
Turns out I was not examining the KML file properly, and just looking at php var_dump outputs to understand the data structure.原来我没有正确检查 KML 文件,只是查看 php var_dump 输出以了解数据结构。 The KML file data looked like below:
KML 文件数据如下所示:
<Folder><name>ne_50m_admin_0_countries</name>
<Placemark>
<name>Zimbabwe</name>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#ne_50m_admin_0_countries">
<SimpleData name="featurecla">Admin-0 country</SimpleData>
In the PHP code, I was using the code below to access the SimpleData:在 PHP 代码中,我使用以下代码访问 SimpleData:
foreach($xmlContent->Document->Folder->children() as $Placemark){
print_r("<h1>".(string)$Placemark->name."</h1>");
foreach ($Placemark->ExtendedData->SchemaData->SimpleData as $element){
var_dump($element); //output shown above
var_dump($element->children());
var_dump($element->attributes());
}
}
After looking at the raw kml file, I was able to access the required information:查看原始 kml 文件后,我能够访问所需的信息:
foreach($xmlContent->Document->Folder->children() as $Placemark){
print_r("<h1>".(string)$Placemark->name."</h1>");
foreach ($Placemark->ExtendedData->SchemaData->SimpleData as $element){
echo $element->attributes()."=>";
echo $element."<br>";
//output featurecla=>Admin-0 country
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.