【问题标题】:XML/PHP to Array IssuesXML/PHP 到数组的问题
【发布时间】:2011-07-07 09:50:15
【问题描述】:

我已经为此烦恼了几个小时,想把它贴在这里看看是否有人有任何建议。

基本上,我通过 SOAP/Curl 调用接收一些 XML 日期,如下所示:

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

}

我猜我面临的问题主要是由于子节点和父节点对于酒店和图像的命名相同。

我们将不胜感激任何对正确方向的帮助或点头。

【问题讨论】:

    标签: php xml arrays multidimensional-array


    【解决方案1】:

    我建议您使用xpath(例如在SimpleXML 实现中http://php.net/manual/en/simplexmlelement.xpath.php)来加载您需要的值。

    或者如果您需要将整个 XML 解析为数组,您可以随时使用 PEAR XML_Serializer 包 (http://pear.php.net/package/XML_Serializer) 来反序列化您的 XML。

    【讨论】:

      【解决方案2】:

      我建议您不要直接使用 DOM,除非您确实需要访问 DOM,否则请使用 SimpleXML 执行这些任务

      它使处理 XML 数据变得非常容易,您几乎可以像处理普通数组一样对其进行操作。

      例子

      <?php 
        $url = 'http://www.flickr.com/services/feeds/photos_public.gne';   
      
        foreach(simplexml_load_file($url)->entry as $entry) {
          echo $entry->content; 
        }
      ?>
      

      该功能的几行代码:)

      祝你好运!

      【讨论】:

      • 感谢您的回复,所以理论上这应该有效,但由于某种原因它不起作用: foreach(simplexml_load_string($result)->hotels as $entry) { echo $entry->hotels; }
      • 您的酒店标签不在根目录下。您将它包含在必须首先访问的多个其他节点中。您可以按照与上述相同的方式执行此操作,但需要多个步骤$xml-&gt;node1-&gt;node1Child1-&gt;data,或者您可以使用children() 函数来提取hotels 标记。还有xpath() 函数可以让你对你的xml 执行xpath 搜索。如果您熟悉 xpath,这很可能是最优雅的解决方案。
      【解决方案3】:

      我正在查看 XML,我注意到一些 XML 标记不正确 Ex: &lt;images&gt;&lt;images&gt;&lt;/images&gt;&lt;images&gt;&lt;/images&gt;&lt;/images&gt; roomunit 也是如此。

      我认为它应该像&lt;images&gt;&lt;image&gt;&lt;/image&gt;&lt;image&gt;&lt;/image&gt;&lt;/images&gt; 这将有助于在 php 中迭代 XML 标记。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2016-10-08
        • 2012-08-22
        • 2011-03-26
        • 1970-01-01
        相关资源
        最近更新 更多