Question

Je suis en train de se rendre à l'information géographique de l'API google-picasa. Ceci est le XML original:

<georss:where>
  <gml:Point>
    <gml:pos>35.669998 139.770004</gml:pos>
  </gml:Point>
</georss:where>

Je suis déjà venu jusqu'ici, avec:

$ns_geo=$item->children($namespace['georss']);
$geo=$ns_geo->children($namespace['gml']);

var_dump($geo) Affichera

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; 

me donnera rien. Y at-il quelque chose d'évident que je fais mal?

Était-ce utile?

La solution

Vous pouvez travailler avec XPath et 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");

De la documentation, Souligné par l'auteur:

  

registerXPathNamespace [...] Crée un contexte préfixe / ns pour la prochaine requête XPath .

D'autres façons de gérer se trouvent dans les espaces de noms SimpleXML ici, par exemple:
Stuart Herbert En PHP - En utilisant SimpleXML pour Parse RSS Feeds

Autres conseils

echo $geo->pos[0];

Voici comment je l'ai fait sans utiliser 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;
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top