2011-01-28 64 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;