2011-01-28 82 views
0

我試圖解析http://api.hostip.info/?ip=12.215.42.19SimpleXML的xml響應,但我似乎無法得到它的工作。hostip.info - 使用SimpeXML解析API響應

響應

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

有人可以幫助我訪問例如主機IP> ipLocation> pointProperty>點提前>座標

謝謝!

+2

可能重複:使用含有XML命名空間的工作](http://stackoverflow.com/questions/2014835/simplexml-working-with-xml-containing-namespaces) – Gordon 2011-01-28 12:44:13

+0

偉大的,我認爲這將有助於:) – n00b 2011-01-28 12:54:15

回答

2

這裏有兩種方法(這可在別處SO通過搜索!)。

的XPath(一上表現出非常基本的)

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

「簡單」 XML(沒那麼簡單)的[SimpleXML的

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