2011-01-28 2 views
0

J'essaye d'analyser la réponse xml de http://api.hostip.info/?ip=12.215.42.19 avec SimpleXML mais je n'arrive pas à le faire fonctionner.hostip.info - Parse réponse API avec SimpeXML

Réponse

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd"> 
<gml:description>This is the Hostip Lookup Service</gml:description> 
<gml:name>hostip</gml:name> 
<gml:boundedBy> 
    <gml:Null>inapplicable</gml:Null> 
</gml:boundedBy> 
<gml:featureMember> 

    <Hostip> 
    <ip>12.215.42.19</ip> 
    <gml:name>Sugar Grove, IL</gml:name> 
    <countryName>UNITED STATES</countryName> 
    <countryAbbrev>US</countryAbbrev> 
    <!-- Co-ordinates are available as lng,lat --> 
    <ipLocation> 

    <gml:pointProperty> 
    <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326"> 
     <gml:coordinates>-88.4588,41.7696</gml:coordinates> 
    </gml:Point> 
    </gml:pointProperty> 
    </ipLocation> 
    </Hostip> 
</gml:featureMember> 

</HostipLookupResultSet> 

que quelqu'un peut me aider à accéder par exemple hostip> ipLocation> pointProperty> Point> Coordonnées

Merci à l'avance!

+2

en double possible de [SimpleXML: Travailler avec les espaces de noms contenant XML ] (http://stackoverflow.com/questions/2014835/simplexml-working-with-xml-containing-namespaces) – Gordon

+0

grand je pense que cela aidera :) – n00b

Répondre

2

Voici deux façons (qui sont disponibles ailleurs sur le SO en effectuant une recherche!).

XPath (un très basique à démontrer)

$coords = $xml->xpath('//gml:coordinates'); 
echo $coords[0]; 

"Simple" XML (pas si simple)

echo $xml 
    ->children('gml', TRUE)->featureMember 
    ->children('', TRUE)->Hostip->ipLocation 
    ->children('gml', TRUE)->pointProperty->Point->coordinates; 
-1

Vous pouvez accéder à des attributs tels que tableau http://www.electrictoolbox.com/php-simplexml-element-attributes/ comme (je ne suis pas sûr avec votre exemple)

Hostip->ipLocation->gml['pointproperty']
+0

OP ne cherche pas à aller chercher un attribut et l article encré ne traite pas de la façon d'aller chercher des éléments avec des espaces de noms – Gordon

+0

Oh Gordon désolé, j'ai vu mal. – osm