开发者

XML/PHP to Array Issues

I've been tearing my hair out with this now for a few hours and thought I'd post it up here to see if anybody had any suggestions.

Basically I am receving some XML date via SOAP/Curl call which looks like this:

<?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>
                                    开发者_JAVA百科        <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>

I'm attempting to iterate through each hotels hotels result and turn each result into a multi dimensional array. The code I'm using which isn't working as I'd like is below:

$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; 
} 

}

I'm guessing the issues I'm facing are mostly caused by the fact that the child and parent nodes are named the same for hotels and for the images.

Any help or a nod in the right direction will be much appreciated.


I suggest you using xpath (for example in SimpleXML implementation http://php.net/manual/en/simplexmlelement.xpath.php) for loading the values you need.

Or if you need whole XML parsed to array, you can always use PEAR XML_Serializer package (http://pear.php.net/package/XML_Serializer) to unserialize your XML.


Instead of working directly with the DOM I would recommend that you, unless you actually do need access to the DOM, perform these tasks using SimpleXML

It makes it very easy to work with XML data and you can act on it almost like a normal array.

Example

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

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

Quite few lines for that functionality :)

Good luck!


I am looking in to the XML and I noticed some XML tags are not correct Ex: <images><images></images><images></images></images> same is the case with roomunit.

I think it should be like <images><image></image><image></image></images> this will help to iterate over XML tag in php.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜