SimpleXML : 네임 스페이스가 포함 된 XML로 작업합니다
-
19-09-2019 - |
문제
Google-Picasa API에서 GEO 정보를 얻으려고 노력하고 있습니다. 이것은 원래 XML입니다.
<georss:where>
<gml:Point>
<gml:pos>35.669998 139.770004</gml:pos>
</gml:Point>
</georss:where>
나는 이미 지금까지왔다 :
$ns_geo=$item->children($namespace['georss']);
$geo=$ns_geo->children($namespace['gml']);
var_dump($geo)
출력됩니다
object(SimpleXMLElement)#34 (1) {
["Point"]=> object(SimpleXMLElement)#30 (1) {
["pos"]=> string(18) "52.373801 4.890935"
}
}
하지만
echo (string)$geo->position or (string)$geo->position->pos;
나에게 아무것도 줄 것입니다. 내가 잘못하고 있다는 것이 분명합니까?
해결책
XPath와 함께 일할 수 있습니다 registerXPathNamespace()
:
$xml->registerXPathNamespace("georss", "http://www.georss.org/georss");
$xml->registerXPathNamespace("gml", "http://www.opengis.net/gml");
$pos = $xml->xpath("/georss:where/gml:Point/gml:pos");
문서, 강조 광산에서 :
registerxpathnamespace […] 접두사/ns 컨텍스트를 만듭니다. 다음 xpath 쿼리의 경우.
예를 들어 SimpleXML에서 네임 스페이스를 처리하는 더 많은 방법은 다음과 같습니다.
PHP의 Stuart Herbert- RSS 피드를 구문 분석하기 위해 SimpleXML 사용
다른 팁
echo $geo->pos[0];
이것이 XPath를 사용하지 않고 수행 한 방법입니다.
$georss = $photo->children('http://www.georss.org/georss');
$coords;
if($georss->count()>0) {
$gml = $georss->children('http://www.opengis.net/gml');
if($gml->count()>0) {
if(isset($gml->Point->pos)) {
$coords = $gml->Point->pos;
}
}
}
제휴하지 않습니다 StackOverflow