dongyi1777 2016-09-16 15:11
浏览 147
已采纳

PHP中的DOM元素GetElementsByTagName问题

<FlightSegment DepartureDateTime="2016-09-20T06:05:00" ArrivalDateTime="2016-09-20T08:05:00" FlightNumber="716" ResBookDesigCode="N">
    <FlightDuration>2016-09-14T02:00:00</FlightDuration>
    <DepartureAirport LocationCode="IST" Terminal="I" />
    <ArrivalAirport LocationCode="KBP" />
    <OperatingAirline Code="PS" />
    <Equipment AirEquipType="73H" />
    <MarketingAirline Code="PS" />
    <BookingClassAvails>
        <BookingClassAvail ResBookDesigCode="N" ResBookDesigQuantity="9" RPH="ADT" AvailablePTC="ADT" ResBookDesigCabinCode="M" FareBasis="NL1LTP4" />
    </BookingClassAvails>
</FlightSegment>
<FlightSegment DepartureDateTime="2016-09-20T09:50:00" ArrivalDateTime="2016-09-20T11:55:00" FlightNumber="101" ResBookDesigCode="N">
    <FlightDuration>2016-09-14T03:05:00</FlightDuration>
    <DepartureAirport LocationCode="KBP" />
    <ArrivalAirport LocationCode="AMS" />
    <OperatingAirline Code="PS" />
    <Equipment AirEquipType="73R" />
    <MarketingAirline Code="PS" />
    <BookingClassAvails>
        <BookingClassAvail ResBookDesigCode="N" ResBookDesigQuantity="9" RPH="ADT" AvailablePTC="ADT" ResBookDesigCabinCode="M" FareBasis="NL1LTP4" />
    </BookingClassAvails>
</FlightSegment>

There are two Flight Segments. We don`t have any identificators in this code, so,

  1. How could I divide this code by blocks to use attributes from first <FlightSegment> block?
  2. Also I need to count <FlightSegments>.

Please HELP! )

  • 写回答

2条回答 默认 最新

  • douxiong5438 2016-09-16 18:41
    关注

    You could use this function to convert an XML node to a (nested) array:

    function domToArray($node) {
        $arr = [];
        // Add all attributes of this node as key/values:
        foreach ($node->attributes as $attribute) {
            $arr[$attribute->nodeName] = $attribute->nodeValue;
        }
        // Iterate through the child nodes
        foreach ($node->childNodes as $child) {
            if ($child->nodeType === XML_TEXT_NODE) {
                // Assign the text content to a "value" key
                if (trim($child->textContent)!== '') {
                    $arr['value'] = $child->textContent;
                }
            } else if ($child->nodeType === XML_ELEMENT_NODE) {
                // Create key/value pairs for child nodes, using recursion.
                // If the children are repeated elements, then build
                // an indexed array, otherwise an associative array.
                if (preg_replace("/y$/", "ie", $child->nodeName) ."s" === $node->nodeName) {
                    $arr[] = domToArray($child);
                } else {
                    $arr[$child->nodeName] = domToArray($child);
                }
            }
        }
        return $arr;
    }
    

    You would first have to create a DOMDocument and read the XML string into it. Note that the piece of XML you provided needs to be wrapped in a single element, which I have called FlightSegments on purpose -- the multiple of FlightSegment of which you have 2:

    $doc = new DOMDocument();
    $doc->loadXML("<FlightSegments>$xml</FlightSegments>");
    

    Once you have that, you can call the function I mentioned above:

    // Convert XML to nested array:
    $flightSegments = domToArray($doc->documentElement);
    

    Here is some of the output you can get from that array:

    // Number of FlightSegments:
    echo "Number of FlightSegment elements: " . count($flightSegments) . "<br>";
    
    echo "DepartureDateTime of first FlightSegment: " . $flightSegments[0]['DepartureDateTime'] . "<br>";
    
    echo "All information:<br>";
    
    print_r ($flightSegments);
    

    See it run on eval.in.

    With the sample XML you provided, the output of the above code would be:

    Number of FlightSegment elements: 2
    DepartureDateTime of first FlightSegment: 2016-09-20T06:05:00
    All information:
    Array
    (
        [0] => Array
            (
                [DepartureDateTime] => 2016-09-20T06:05:00
                [ArrivalDateTime] => 2016-09-20T08:05:00
                [FlightNumber] => 716
                [ResBookDesigCode] => N
                [FlightDuration] => Array
                    (
                        [value] => 2016-09-14T02:00:00
                    )
    
                [DepartureAirport] => Array
                    (
                        [LocationCode] => IST
                        [Terminal] => I
                    )
    
                [ArrivalAirport] => Array
                    (
                        [LocationCode] => KBP
                    )
    
                [OperatingAirline] => Array
                    (
                        [Code] => PS
                    )
    
                [Equipment] => Array
                    (
                        [AirEquipType] => 73H
                    )
    
                [MarketingAirline] => Array
                    (
                        [Code] => PS
                    )
    
                [BookingClassAvails] => Array
                    (
                        [0] => Array
                            (
                                [ResBookDesigCode] => N
                                [ResBookDesigQuantity] => 9
                                [RPH] => ADT
                                [AvailablePTC] => ADT
                                [ResBookDesigCabinCode] => M
                                [FareBasis] => NL1LTP4
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [DepartureDateTime] => 2016-09-20T09:50:00
                [ArrivalDateTime] => 2016-09-20T11:55:00
                [FlightNumber] => 101
                [ResBookDesigCode] => N
                [FlightDuration] => Array
                    (
                        [value] => 2016-09-14T03:05:00
                    )
    
                [DepartureAirport] => Array
                    (
                        [LocationCode] => KBP
                    )
    
                [ArrivalAirport] => Array
                    (
                        [LocationCode] => AMS
                    )
    
                [OperatingAirline] => Array
                    (
                        [Code] => PS
                    )
    
                [Equipment] => Array
                    (
                        [AirEquipType] => 73R
                    )
    
                [MarketingAirline] => Array
                    (
                        [Code] => PS
                    )
    
                [BookingClassAvails] => Array
                    (
                        [0] => Array
                            (
                                [ResBookDesigCode] => N
                                [ResBookDesigQuantity] => 9
                                [RPH] => ADT
                                [AvailablePTC] => ADT
                                [ResBookDesigCabinCode] => M
                                [FareBasis] => NL1LTP4
                            )
    
                    )
    
            )
    
    )
    

    Edit after you provided complete XML

    I am not sure what your problem is any more, because in comments you shared code where you walk through your XML successfully.

    Just to note that you can use the above function to turn the complete XML into an array (I made one correction to the function to correctly identify the plural of a tag name that ends with "y").

    Here is how you would call it on your XML:

    $soap = domToArray($xmlDoc->documentElement);
    

    The content of $soap would be (I truncated it):

    Array
    (
        [soap:Body] => Array
            (
                [SearchFlightResponse] => Array
                    (
                        [OTA_AirLowFareSearchRS] => Array
                            (
                                [Version] => 0
                                [HasMoreResult] => Array
                                    (
                                        [value] => false
                                    )
    
                                [Success] => Array
                                    (
                                    )
    
                                [PricedItineraries] => Array
                                    (
                                        [0] => Array
                                            (
                                                [Currency] => USD
                                                [ProviderType] => AmadeusProvider
                                                [SequenceNumber] => 0
                                                [AirItinerary] => Array
                                                    (
                                                        [OriginDestinationOptions] => Array
                                                            (
                                                                [0] => Array
                                                                    (
                                                                        [RefNumber] => 0
                                                                        [DirectionId] => 0
                                                                        [ElapsedTime] => 0650
                                                                        [FlightSegment] => Array
                                                                            (
                                                                                [DepartureDateTime] => 2016-09-20T09:50:00
                                                                                [ArrivalDateTime] => 2016-09-20T11:55:00
                                                                                [FlightNumber] => 101
                                                                                [ResBookDesigCode] => N
                                                                                [FlightDuration] => Array
                                                                                    (
                                                                                        [value] => 2016-09-14T03:05:00
                                                                                    )
    
                                                                                [DepartureAirport] => Array
                                                                                    (
                                                                                        [LocationCode] => KBP
                                                                                    )
    
                                                                                [ArrivalAirport] => Array
                                                                                    (
                                                                                        [LocationCode] => AMS
                                                                                    )
    
                                                                                [OperatingAirline] => Array
                                                                                    (
                                                                                        [Code] => PS
                                                                                    )
    
                                                                                [Equipment] => Array
                                                                                    (
                                                                                        [AirEquipType] => 73R
                                                                                    )
    
                                                                                [MarketingAirline] => Array
                                                                                    (
                                                                                        [Code] => PS
                                                                                    )
    
                                                                                [BookingClassAvails] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [ResBookDesigCode] => N
                                                                                                [ResBookDesigQuantity] => 9
                                                                                                [RPH] => ADT
                                                                                                [AvailablePTC] => ADT
                                                                                                [ResBookDesigCabinCode] => M
                                                                                                [FareBasis] => NL1LTP4
                                                                                            )
    
                                                                                    )
    

    Now you can write code like:

    $itineries = $soap['soap:Body']['SearchFlightResponse']['OTA_AirLowFareSearchRS']['PricedItineraries']; 
    foreach ($itineries as $itinery) {
        // ... etc. Always array access.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路