Pokrowsky Pokrowsky - 2 months ago 9
PHP Question

Issue with DOM element GetElementByTagName in PHP

<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! )

Answer

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.
}