'PHP - XML Object inside XML Object in Soap Response

<diffgr:diffgram
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
    xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet
        xmlns="">
        <MultiplePrices diffgr:id="MultiplePrices1" msdata:rowOrder="0">
            <stationID>06</stationID>
            <station>Aeroporto Ponta Delgada</station>
            <weekDayOpen>00:00</weekDayOpen>
            <weekDayClose>00:00</weekDayClose>
            <supplier_Code>0</supplier_Code>
            <groupID>B</groupID>
            <group_Name>GRUPO B</group_Name>
            <extrasIncluded>CDW</extrasIncluded>
            <extrasRequired/>
            <extrasAccepted/>
            <extrasAvailable>ADD,BSEAT,BOST,CSEAT,PAI,SCDW,THW</extrasAvailable>
            <package/>
            <dayValueWithDiscount>56.03</dayValueWithDiscount>
            <prepaidRate>false</prepaidRate>
            <allExtras>
                <xs:schema
                    xmlns=""
                    xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
                    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="AllExtras" msdata:UseCurrentLocale="true">
                        <xs:complexType>
                            <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="AllExtras">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="groupID" type="xs:string" minOccurs="0"/>
                                            <xs:element name="extraID" type="xs:string" minOccurs="0"/>
                                            <xs:element name="extra" type="xs:string" minOccurs="0"/>
                                            <xs:element name="description" type="xs:string" minOccurs="0"/>
                                            <xs:element name="value" type="xs:decimal" minOccurs="0"/>
                                            <xs:element name="taxRate" type="xs:decimal" minOccurs="0"/>
                                            <xs:element name="valueWithoutRounding" type="xs:decimal" minOccurs="0"/>
                                            <xs:element name="extra_Included" type="xs:boolean" minOccurs="0"/>
                                            <xs:element name="extra_Required" type="xs:boolean" minOccurs="0"/>
                                            <xs:element name="extra_Accepted" type="xs:boolean" minOccurs="0"/>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                </xs:schema>
                <diffgr:diffgram
                    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                    <DocumentElement>
                        <AllExtras diffgr:id="AllExtras1" msdata:rowOrder="0">
                            <groupID>B</groupID>
                            <extraID>ADD</extraID>
                            <extra>ADD</extra>
                            <description>Condutor Adicional</description>
                            <value>4.31</value>
                            <taxRate>16.00</taxRate>
                            <valueWithoutRounding>4.31034</valueWithoutRounding>
                            <extra_Included>false</extra_Included>
                            <extra_Required>false</extra_Required>
                            <extra_Accepted>false</extra_Accepted>
                            <accept_quantity>true</accept_quantity>
                            <insurance>false</insurance>
                            <excess>0</excess>
                            <extraByDay>true</extraByDay>
                        </AllExtras>
                        <AllExtras diffgr:id="AllExtras2" msdata:rowOrder="1">
                            <groupID>B</groupID>
                            <extraID>BSEAT</extraID>
                            <extra/>
                            <description>Cadeira de bebé</description>
                            <value>3.64</value>
                            <taxRate>16.00</taxRate>
                            <valueWithoutRounding>3.63923</valueWithoutRounding>
                            <extra_Included>false</extra_Included>
                            <extra_Required>false</extra_Required>
                            <extra_Accepted>false</extra_Accepted>
                            <accept_quantity>true</accept_quantity>
                            <insurance>false</insurance>
                            <excess>0</excess>
                            <extraByDay>true</extraByDay>
                        </AllExtras>
                    </DocumentElement>
                </diffgr:diffgram>
            </allExtras>
        </MultiplePrices>
    </NewDataSet>
</diffgr:diffgram>
<?php
$xml = $api->MultiplePricesResult->getMultiplePrices->any;
$xml = simplexml_load_string($xml);
$groupID = $xml->NewDataSet->MultiplePrices->groupID;
$group_name = $xml->NewDataSet->MultiplePrices->group_Name;
$group_total_value = $xml->NewDataSet->MultiplePrices->previewValue;
$group_daily_value = $xml->NewDataSet->MultiplePrices->totalDayValueWithTax;
$group_image_url = $xml->NewDataSet->MultiplePrices->imageURL;
$pickUprentalStation = $xml->NewDataSet->MultiplePrices->station;
$nrDays = $xml->NewDataSet->MultiplePrices->nrDays;

Having the above Soap response below, I cannot access or loop through the all Extras element, it always show as empty, any idea how can i access or loop through that element ? When printing $xml->NewDataSet->MultiplePrices->package or even allExtras I got nothing. Can someone help and let me know how to access that part ?

This is the result of the XML

SimpleXMLElement Object
(
    [NewDataSet] => SimpleXMLElement Object
        (
            [MultiplePrices] => SimpleXMLElement Object
                (
                    [stationID] => 06
                    [station] => Aeroporto Ponta Delgada
                    [weekDayOpen] => 00:00
                    [weekDayClose] => 00:00
                    [supplier_Code] => 0
                    [groupID] => B
                    [group_Name] => GRUPO B
                    [SIPP] => EDMR
                    [imageURL] => https://i.postimg.cc/PNsKy0Y0/FIAT-PANDA-CROSS-FRENTE.png
                    [rateCode] => 10
                    [dynamicRate] => false
                    [nrDays] => 13
                    [dayValue] => 56.03
                    [totalDayValueWithTax] => 65.00
                    [kmsValue] => 0.00
                    [kmsIncluded] => true
                    [kmsFreePerDay] => 0
                    [previewValue] => 845.00
                    [valueWithotTax] => 728.45
                    [taxRate] => 16.00
                    [otherTaxValue] => 0.00
                    [taxValue] => 116.55
                    [extrasIncluded] => CDW
                    [extrasRequired] => SimpleXMLElement Object
                        (
                        )

                    [extrasAccepted] => SimpleXMLElement Object
                        (
                        )

                    [extrasAvailable] => ADD,BSEAT,BOST,CSEAT,PAI,SCDW,THW
                    [package] => SimpleXMLElement Object
                        (
                        )

                    [dayValueWithDiscount] => 56.03
                    [previewValueWithDiscount] => 845.00
                    [valueWithDiscountWithoutTax] => 728.45
                    [percentualDiscount] => 0.00
                    [prepaidRate] => false
                    [allExtras] => SimpleXMLElement Object
                        (
                        )

                )

        )

)

But if i try to access $xml->NewDataSet->MultiplePrices->package it's empty: SimpleXMLElement Object ( ) and my question is how to access or loop through the information in allExtras.



Solution 1:[1]

Well I found out i can easily access all elements in an easier way including the ondes in deeper levels with xpath, so this was what i was searching for.

foreach ($xml->xpath('//AllExtras') as $item_extra)

and can even filter further like.

foreach ($xml->xpath('//AllExtras[groupID = "B"]') as $item_extra)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 César Couto