【问题标题】:hostip.info - Parse API response with SimpeXMLhostip.info - 使用 SimpeXML 解析 API 响应
【发布时间】:2011-01-28 12:27:12
【问题描述】:

我正在尝试使用 SimpleXML 解析 http://api.hostip.info/?ip=12.215.42.19 的 xml 响应,但我似乎无法正常工作。

回应

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

有人可以帮我访问例如 Hostip>ipLocation>pointProperty>Point>coordinates

提前致谢!

【问题讨论】:

标签: php xml api simplexml


【解决方案1】:

这里有两种方法(可以通过搜索在 SO 的其他地方找到!)。

XPath(一个非常基础的演示)

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

“简单”XML(没那么简单)

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

【讨论】:

    【解决方案2】:

    您可以访问数组等属性 http://www.electrictoolbox.com/php-simplexml-element-attributes/ 喜欢(我不确定你的例子)

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

    【讨论】:

    • OP 不打算获取属性,并且链接的文章没有解决如何获取命名空间元素
    猜你喜欢
    • 1970-01-01
    • 2019-02-08
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多