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 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败