2017-07-13 15 views
0

GEOサービス(PDOK)からXML repsonceを受信しました。 $ responce-> raw_bodyは、このXML構造が含まれています名前空間でXMLレスポンスからPHP配列を取得

<xls:GeocodeResponse xmlns:xls="http://www.opengis.net/xls" xmlns:gml="http://www.opengis.net/gml"> 
 
    <xls:GeocodeResponseList numberOfGeocodedAddresses="1"> 
 
     <xls:GeocodedAddress> 
 
     <gml:Point srsName="EPSG:28992"> 
 
      <gml:pos dimension="2">121299.73296672151 487003.8972524117</gml:pos> 
 
     </gml:Point> 
 
     <xls:Address countryCode="NL"> 
 
      <xls:StreetAddress> 
 
       <xls:Street>Rokin</xls:Street> 
 
      </xls:StreetAddress> 
 
      <xls:Place type="MunicipalitySubdivision">Amsterdam</xls:Place> 
 
      <xls:Place type="Municipality">Amsterdam</xls:Place> 
 
      <xls:Place type="CountrySubdivision">Noord-Holland</xls:Place> 
 
     </xls:Address> 
 
     </xls:GeocodedAddress> 
 
    </xls:GeocodeResponseList> 
 
</xls:GeocodeResponse>

私はここで要素にアクセスするにはどうすればよいです。たとえば、PHP配列を要素121299.73296672151 487003.8972524117 にアクセスして座標を取得したいとします。

また、他の要素。私はSimpleXMLパーサーを使用しましたが、私はいつもnullを受け取ります。 名前空間と関係があると思います。しかし、私はこれを解決する方法を知りません。


     $url = "http://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=xxxxx%20xx"; 
     $response = \Httpful\Request::get($url) 
     ->expectsXml() 
      ->send(); 

     $xml = new \SimpleXMLElement($response->raw_body); 

     print_r($xml); 

出力:

SimpleXMLElementオブジェクト()

すべてのヘルプがにappriciatedさ

responceから来ています!

+0

SimpleXMLの使い方をご紹介します。 – RiggsFolly

+0

$ xml = new \ SimpleXMLElement($ response-> raw_body); \t \t \t print_r($ xml); – Aren

+0

そして、あなたは '$ response-> raw_body'をどうやって取得しますか – RiggsFolly

答えて

1

を次にこれを行います。その名前空間は確かに名前空間であり、名前空間の登録後にxpathを使うと、必要な要素を見つけることができます。

$xml = new \SimpleXMLElement($response); 
    $xml->registerXPathNamespace('xls', 'http://www.opengis.net/xls'); 
    $xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml'); 

    foreach($xml->xpath('//xls:GeocodeResponseList') as $header) 
    { 
     $geocoordinates = $header->xpath('//gml:pos'); 
     $street = (string) ($header->xpath('//xls:Street')[0]); 
     $place = (string) ($header->xpath('//xls:Place')[2]); 

    } 

    echo $geocoordinates[0];    // get the coordinates needs to split on space 
    echo "<br />"; 
    echo $geocoordinates[0]['dimension']; // get the dimension attribute 
    echo "<br />"; 
    echo $street; 
    echo "<br />"; 
    echo $place; 
+0

'$ xml = simplexml_load_file($ file); foreach($ xml-> xpath( '// xls:GeocodeResponseList')$ヘッダ) 'も動作し、名前空間を登録する必要はありません... –

-1

" - > expectsXml()"ではなく " - > expectsJson()"を使用します。

$array = json_decode($response->raw_body);

か、PHP 7.xの使用の場合:私は私の問題の解決策を見つけたいくつかの掘削後https://github.com/eddypouw/geodata-postal-api

+0

出力をJsonに強制することはできません メッセージ: '応答をJSONとして解析できません' – Aren

関連する問題