从多维数组中检索xml节点值


To retrieve xml node values from multidimensional array

我正在以数组格式存储一个xml响应。现在我需要从数组中获取节点属性值,如HotelLocation、hotelCode等。下面是我的数组的格式:

Array
(
[0] => Array
        (
            [media] => <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"><SOAP:Body><hotel:HotelMediaLinksRsp TraceId="trace" TransactionId="69B008C27F00000109CA96A4918F80C6" ResponseTime="77" xmlns:common_v25_0="http://www.travelport.com/schema/common_v25_0" xmlns:hotel="http://www.travelport.com/schema/hotel_v25_0"><hotel:HotelPropertyWithMediaItems><hotel:HotelProperty HotelChain="CP" HotelCode="36588" HotelLocation="DXB" Name="CROWNE PLAZA FESTIVAL CITY" VendorLocationKey="1tTgCxLKRUuwf3+sFMCwFg==" HotelTransportation="Limo" ReserveRequirement="Other" ParticipationLevel="Enhanced Best Available Rate participant" Availability="Available" FeaturedProperty="true" NetTransCommissionInd="C"><hotel:PropertyAddress><hotel:Address>DUBAI-FESTIVAL CITY</hotel:Address></hotel:PropertyAddress></hotel:HotelProperty></hotel:HotelPropertyWithMediaItems></hotel:HotelMediaLinksRsp></SOAP:Body></SOAP:Envelope>
)
)

(Php)

首先,您需要访问数组的这一部分。

如果它被存储为$MyResponse,您将使用$MyResponse[0]访问[0]

然后我会把XML转换成一个数组,这样更容易访问。

$xml = new SimpleXMLElement($MyResponse[0]);

然后,您可以像访问数组一样访问各个元素。例如

echo $xml->a->b->b['name'];