I am using PHP and I have the following XML response from my web-service, it is an XML object and I can convert it to a string too.
How can I get every service from the XML object above it as an array? I tried to use the function below but I got an empty array, any help please?
The XML is as below:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<rateresp:RateResponse xmlns:rateresp="http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgResponse">
<Provider code="DHL">
<Notification code="0">
<Message/>
</Notification>
<Service type="Q">
<TotalNet>
<Currency>EUR</Currency>
<Amount>197.45</Amount>
</TotalNet>
<Charges>
<Currency>EUR</Currency>
<Charge>
<ChargeType>MEDICAL EXPRESS</ChargeType>
<ChargeAmount>159.19</ChargeAmount>
</Charge>
<Charge>
<ChargeCode>FF</ChargeCode>
<ChargeType>FUEL SURCHARGE</ChargeType>
<ChargeAmount>28.26</ChargeAmount>
</Charge>
<Charge>
<ChargeCode>II</ChargeCode>
<ChargeType>SHIPMENT INSURANCE</ChargeType>
<ChargeAmount>10.00</ChargeAmount>
</Charge>
</Charges>
<DeliveryTime>2019-12-02T12:00:00</DeliveryTime>
<CutoffTime>2019-11-26T18:00:00</CutoffTime>
<NextBusinessDayInd>N</NextBusinessDayInd>
</Service>
<Service type="8">
<TotalNet>
<Currency>EUR</Currency>
<Amount>72.00</Amount>
</TotalNet>
<Charges>
<Currency>EUR</Currency>
<Charge>
<ChargeType>EXPRESS EASY</ChargeType>
<ChargeAmount>72.00</ChargeAmount>
</Charge>
</Charges>
<DeliveryTime>2019-12-02T23:59:00</DeliveryTime>
<CutoffTime>2019-11-26T18:00:00</CutoffTime>
<NextBusinessDayInd>N</NextBusinessDayInd>
</Service>
</Provider>
</rateresp:RateResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
PHP function:
function xml2array($xmlObject, $out = array()) {
foreach ((array) $xmlObject as $index => $node) {
$out[$index] = ( is_object($node) ) ? xml2array($node) : $node;
}
return $out;
}