2011-07-07 15 views
0

私は数時間の間、これで私の髪を裂いてきました。誰か提案があったかどうか確認するためにここに投稿すると思いました。XML/PHPから配列への問題

は基本的に私はこのようになりますSOAP /カール呼び出しを介して、いくつかのXMLの日付をrecevingています:

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <LocationAvailabilityResponse xmlns=""> 
      <getAvailabilityReturn> 
       <errors /> 
        <requestID>389851</requestID> 
         <hotels> 
          <hotels> 
           <hotel> 
            <apt>false</apt> 
            <distance>0</distance> 
            <fromPrice>18.5</fromPrice> 
            <hotelName>Britannia Hotel Stockport</hotelName> 
            <id>5165</id> 
            <images> 
             <images> 
              <hasThumbnail>true</hasThumbnail> 
              <height>187</height> 
              <thumbnailHeight>50</thumbnailHeight> 
              <thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_swimming_pool_1_swi_5165.JPG</thumbnailURL> 
              <thumbnailWidth>68</thumbnailWidth> 
              <title>Britannia Hotel Stockport</title> 
              <url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_swimming_pool_1_swi_5165.JPG</url> 
              <width>257</width> 
             </images> 
             <images> 
              <hasThumbnail>false</hasThumbnail> 
              <height>187</height> 
              <thumbnailHeight>0</thumbnailHeight> 
              <thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_swimming_pool_2_swi_5165.JPG</thumbnailURL> 
              <thumbnailWidth>0</thumbnailWidth> 
              <title>Swimming Pool</title> 
              <url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_swimming_pool_2_swi_5165.JPG</url> 
              <width>257</width> 
             </images> 
             <images> 
              <hasThumbnail>false</hasThumbnail> 
              <height>187</height> 
              <thumbnailHeight>0</thumbnailHeight> 
              <thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_hotel_entrance_1_ent_5165.JPG</thumbnailURL> 
              <thumbnailWidth>0</thumbnailWidth> 
              <title>Hotel Entrance</title> 
              <url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_hotel_entrance_1_ent_5165.JPG</url> 
              <width>257</width> 
             </images> 
             <images> 
              <hasThumbnail>false</hasThumbnail> 
              <height>187</height> 
              <thumbnailHeight>0</thumbnailHeight> 
              <thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_hotel_gym_1_gym_5165.JPG</thumbnailURL> 
              <thumbnailWidth>0</thumbnailWidth> 
              <title>Hotel Gym</title> 
              <url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_hotel_gym_1_gym_5165.JPG</url> 
              <width>257</width> 
             </images> 
             <images> 
              <hasThumbnail>false</hasThumbnail> 
              <height>187</height> 
              <thumbnailHeight>0</thumbnailHeight> 
              <thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_hotel_lounge_1_lou_5165.JPG</thumbnailURL> 
              <thumbnailWidth>0</thumbnailWidth> 
              <title>Hotel Lounge</title> 
              <url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_hotel_lounge_1_lou_5165.JPG</url> 
              <width>257</width> 
             </images> 
             <images> 
              <hasThumbnail>false</hasThumbnail> 
              <height>187</height> 
              <thumbnailHeight>0</thumbnailHeight> 
              <thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_four_poster_bedroom_1_pst_5165.JPG</thumbnailURL> 
              <thumbnailWidth>0</thumbnailWidth> 
              <title>Four Poster Bedroom</title> 
              <url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_four_poster_bedroom_1_pst_5165.JPG</url> 
              <width>257</width> 
             </images> 
             </images> 
            <latitude>53.398941</latitude> 
            <location>Stockport</location> 
            <longitude>-2.13463</longitude> 
            <starRating>3</starRating> 
            </hotel> 
           <roomUnits> 
            <roomUnits> 
             <allocation>1</allocation> 
             <boardCode>RO</boardCode> 
             <boardDescription>Room only</boardDescription> 
             <maxOccupancy>2</maxOccupancy> 
             <minOccupancy>1</minOccupancy> 
             <price>18.5</price> 
             <stdOccupancy>2</stdOccupancy> 
             <unitDescription>Double For 1-2</unitDescription> 
             <unitID>162</unitID> 
            </roomUnits> 
            <roomUnits> 
             <allocation>1</allocation> 
             <boardCode>RO</boardCode> 
             <boardDescription>Room only</boardDescription> 
             <maxOccupancy>2</maxOccupancy> 
             <minOccupancy>1</minOccupancy> 
             <price>18.5</price> 
             <stdOccupancy>2</stdOccupancy> 
             <unitDescription>Twin For 1-2</unitDescription> 
             <unitID>161</unitID> 
            </roomUnits> 
            <roomUnits> 
             <allocation>1</allocation> 
             <boardCode>RO</boardCode> 
             <boardDescription>Room only</boardDescription> 
             <maxOccupancy>2</maxOccupancy> 
             <minOccupancy>2</minOccupancy> 
             <price>23.5</price> 
             <stdOccupancy>2</stdOccupancy> 
             <unitDescription>Executive Double Room</unitDescription> 
             <unitID>65</unitID> 
            </roomUnits> 
            <roomUnits> 
             <allocation>1</allocation> 
             <boardCode>RO</boardCode> 
             <boardDescription>Room only</boardDescription> 
             <maxOccupancy>2</maxOccupancy> 
             <minOccupancy>2</minOccupancy> 
             <price>23.5</price> 
             <stdOccupancy>2</stdOccupancy> 
             <unitDescription>Executive Twin Room</unitDescription> 
             <unitID>64</unitID> 
            </roomUnits> 
           </roomUnits> 
         </hotels> 

私はホテルがつながる各ホテルを反復処理し、多次元配列にそれぞれの結果をオンにしようとしています。私が好きなように動作していない私が使用しているコードの下にある:私は私が直面してる問題を推測している

$doc = new DOMDocument(); 


if ($doc->loadXML($result)) { 
$items = $doc->getElementsByTagName('hotels'); 
$hotelnames = array(); 

foreach($items as $item) { 
    $hotelname = array(); 
    $hotelimages = array(); 

    if($item->childNodes->length) { 
     foreach($item->childNodes as $i) { 

      $hotelname[$i->nodeName] = $i->nodeValue; 

      if($i->childNodes->length){ 

       foreach($i->childNodes as $z) { 

        if($z->childNodes->length){ 

         foreach($z->childNodes as $x) { 

          $hotelimage[$x->nodeName] = $x->nodeValue; 

         } 

        } 



       } 

      } 

      $hotelimages[] = $hotelimage; 
     } 
    } 

    $hotelnames[] = $hotelname; 
} 

} 

は主に子供と親ノードが命名されていることに起因していますホテルやイメージでも同じです。

正しい方向のヘルプやノーを大歓迎します。

答えて

0

必要な値をロードするためにxpath(たとえばSimpleXMLの実装http://php.net/manual/en/simplexmlelement.xpath.php)を使用することをお勧めします。

XML全体を配列に解析する必要がある場合は、PEAR XML_Serializerパッケージ(http://pear.php.net/package/XML_Serializer)を使用してXMLのシリアル化を解除できます。

0

代わりのDOMで直接作業私はそれがXMLデータで動作するように、あなたが行動することができ、それは非常に簡単です、あなたが実際にDOMにアクセスする必要がありますしない限り、あなたは、SimpleXML

を使用してこれらのタスクを実行することをお勧めしますそれは通常の配列とほとんど同じです。その機能の

<?php 
    $url = 'http://www.flickr.com/services/feeds/photos_public.gne'; 

    foreach(simplexml_load_file($url)->entry as $entry) { 
    echo $entry->content; 
    } 
?> 

かなりの数のライン:)

幸運!

+0

おかげで、 だから理論的には、これは動作するはずですが、何らかの理由でそれがない: foreachのは、($結果(simplexml_load_string) - $エントリとして>ホテル){ するecho $エントリー・>ホテル。 } – mikestrand

+0

あなたのホテルのタグはルートにありません。最初にアクセスする必要がある複数の他のノードに同封されています。これは上記と同じ方法で行うことができますが、 '$ xml-> node1-> node1Child1-> data'を複数回実行するか、' children'タグを抽出するために 'children()'関数を使うことができます。また、xpathにxpath検索を実行させる 'xpath()'関数もあります。あなたがxpathに精通しているなら、これはもっとも高級な解決策になるでしょう。 – inquam

0

私はXMLを調べていますが、一部のXMLタグが正しくないことに気付きました。 Ex: <images><images></images><images></images></images>はroomunitと同じです。

私はこれが<images><image></image><image></image></images>のようになるはずだと思います。これはPHPのXMLタグを繰り返し処理するのに役立ちます。あなたの応答のための