0

I'm trying to parse the xml response of http://api.hostip.info/?ip=12.215.42.19 with SimpleXML but I can't seem to get it work.

Response

<?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>

Can someone help me to access for instance Hostip>ipLocation>pointProperty>Point>coordinates

Thanks in advance!

n00b
  • 16,088
  • 21
  • 56
  • 72
  • 2
    possible duplicate of [SimpleXML: Working with XML containing namespaces](http://stackoverflow.com/questions/2014835/simplexml-working-with-xml-containing-namespaces) – Gordon Jan 28 '11 at 12:44
  • great I think that'll help :) – n00b Jan 28 '11 at 12:54

2 Answers2

2

Here are two ways (which are available elsewhere on SO by searching!).

XPath (a very basic on to demonstrate)

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

"Simple" XML (not so simple)

echo $xml
    ->children('gml', TRUE)->featureMember
    ->children('', TRUE)->Hostip->ipLocation
    ->children('gml', TRUE)->pointProperty->Point->coordinates;
salathe
  • 51,324
  • 12
  • 104
  • 132
-1

You can access attributes like array http://www.electrictoolbox.com/php-simplexml-element-attributes/ like (im not sure with your example)

Hostip->ipLocation->gml['pointproperty']
osm
  • 4,186
  • 3
  • 23
  • 24
  • OP isnt looking to fetch an attribute and linked article doesnt address how to fetch namespaced elements – Gordon Jan 28 '11 at 12:45