我一直在试图弄清楚如何打开一个 xml 文件并将其解析为一个数组。但我似乎无法让它正常工作。这是 XML 文件:
<RMSAvailRateChartRS Version="1.0.0.0">
<SoldMessage>call</SoldMessage>
<RoomTypes>
<RoomType>
<RoomTypeId>10</RoomTypeId>
<SubPropertyId>1</SubPropertyId>
<Name>K</Name>
<MaxOccupancy>2</MaxOccupancy>
<Availability Id="1" Date="2013-11-04" Available="false" NoOfRoomsAvailable="0" />
<BookingRangeAvailable>false</BookingRangeAvailable>
<ChargeTypes>
<ChargeType>
<ChargeTypeId>8</ChargeTypeId>
<Name>BAR</Name>
<Description>Best Rate Available</Description>
<Charge Id="1" Date="2013-11-04" Price="100.00" MinStay="1" MaxStay="25" ValidCharge="true" IncludeChildInBase="false" IncludeInfantInBase="false" Flames="false" CanArriveToday="True" PersonBase="2" ChildBase="0" InfantBase="0" />
</ChargeType>
</ChargeTypes>
</RoomType>
</RoomTypes>
</RMSAvailRateChartRS>
这是我的 php 代码(将其作为数组传递):
public static function getXML($id) {
$file = JPATH_SITE.'/tmp/request-'.$id.'.xml';
if (file_exists($file)) :
$xml = simplexml_load_file($file,'SimpleXMLElement',LIBXML_NOCDATA);
return $xml;
else :
exit('Failed to open '.$file.'.');
endif;
}
哪个有效,并给了我这个数组:
SimpleXMLElement Object
(
[@attributes] => Array
(
[Version] => 1.0.0.0
)
[SoldMessage] => call
[RoomTypes] => SimpleXMLElement Object
(
[RoomType] => SimpleXMLElement Object
(
[RoomTypeId] => 10
[SubPropertyId] => 1
[Name] => K
[MaxOccupancy] => 2
[Availability] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Id] => 1
[Date] => 2013-11-04
[Available] => false
[NoOfRoomsAvailable] => 0
)
)
[BookingRangeAvailable] => false
[ChargeTypes] => SimpleXMLElement Object
(
[ChargeType] => SimpleXMLElement Object
(
[ChargeTypeId] => 8
[Name] => BAR
[Description] => Best Rate Available
[Charge] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Id] => 1
[Date] => 2013-11-04
[Price] => 100.00
[MinStay] => 1
[MaxStay] => 25
[ValidCharge] => true
[IncludeChildInBase] => false
[IncludeInfantInBase] => false
[Flames] => false
[CanArriveToday] => True
[PersonBase] => 2
[ChildBase] => 0
[InfantBase] => 0
)
)
)
)
)
)
)
但是当我尝试遍历数组并回显信息时,它失败了。这是我尝试使用的代码:
foreach($xmlArr->RMSAvailRateChartRS[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
};
但我得到的错误是:致命错误:在第 33 行的 default.php 中的非对象上调用成员函数属性()
有人可以帮我回显 XML 文件的详细信息吗?