使用名称空间从XML响应获取PHP数组
我从地理服务(PDOK)收到一份XML报告。$response->raw_正文包含以下XML结构:使用名称空间从XML响应获取PHP数组,php,arrays,xml,decode,geo,Php,Arrays,Xml,Decode,Geo,我从地理服务(PDOK)收到一份XML报告。$response->raw_正文包含以下XML结构: 121299.73296672151 487003.8972524117 罗金 阿姆斯特丹 阿姆斯特丹 北荷兰省 使用“->expectsJson()”而不是“->expectsXml()。然后这样做: $array=json\u decode($response->raw\u body) 或者如果您使用PHP7.x:经过一些挖掘,我找到了解决问题的方法。这实际上是由名称空间决定的,当在名称空
121299.73296672151 487003.8972524117
罗金
阿姆斯特丹
阿姆斯特丹
北荷兰省
使用“->expectsJson()”而不是“->expectsXml()。然后这样做:
$array=json\u decode($response->raw\u body)代码>
或者如果您使用PHP7.x:经过一些挖掘,我找到了解决问题的方法。这实际上是由名称空间决定的,当在名称空间注册后使用xpath时,您可以找到所需的元素
$xml = new \SimpleXMLElement($response);
$xml->registerXPathNamespace('xls', 'http://www.opengis.net/xls');
$xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
foreach($xml->xpath('//xls:GeocodeResponseList') as $header)
{
$geocoordinates = $header->xpath('//gml:pos');
$street = (string) ($header->xpath('//xls:Street')[0]);
$place = (string) ($header->xpath('//xls:Place')[2]);
}
echo $geocoordinates[0]; // get the coordinates needs to split on space
echo "<br />";
echo $geocoordinates[0]['dimension']; // get the dimension attribute
echo "<br />";
echo $street;
echo "<br />";
echo $place;
$xml=new\simplexmlement($response);
$xml->registerXPathNamespace('xls','http://www.opengis.net/xls');
$xml->registerXPathNamespace('gml','http://www.opengis.net/gml');
foreach($xml->xpath('//xls:GeocodeResponseList')作为$header)
{
$geocoordinates=$header->xpath('//gml:pos');
$street=(字符串)($header->xpath('//xls:street')[0]);
$place=(字符串)($header->xpath('//xls:place')[2]);
}
echo$geocoordinates[0];//获取需要在空间上拆分的坐标
回声“
”;
echo$GeoCoordinations[0]['dimension'];//获取维度属性
回声“
”;
回声街;;
回声“
”;
回音$place;
让我们看看您是如何使用SimpleXML$xml=new\simplexmlement($response->raw\u body)的;打印(xml);您如何获得$response->raw_body
我不是牙医,请不要让我在这里拔牙打开PHP错误报告。无法强制输出Json消息:“无法将响应解析为Json”$xml=simplexml\u load\u file($file);foreach($xml->xpath('//xls:GeocodeResponseList')作为$header)
也可以使用,无需注册名称空间。。。