【发布时间】:2016-07-22 14:31:29
【问题描述】:
我是 php 新手,正在从事一个我必须处理 xml api 的项目。我正在使用 laravel 5.2 框架来创建项目。
我有一个这样的 xml:
<?xml version="1.0" encoding="UTF-8"?>
<HotelListing>
<HotelCode Currency="INR">1000000681</HotelCode>
<RoomList>
<Room>
<RoomTypeName>Deluxe Cottage AC</RoomTypeName>
<RoomTypeCode>45000001713</RoomTypeCode>
<IsActive>True</IsActive>
</Room>
<Room>
<RoomTypeName>Den Cottage AC</RoomTypeName>
<RoomTypeCode>45000001714</RoomTypeCode>
<IsActive>True</IsActive>
</Room>
<Room>
<RoomTypeName>Premium Cottage AC</RoomTypeName>
<RoomTypeCode>45000001715</RoomTypeCode>
<IsActive>True</IsActive>
</Room>
<Room>
<RoomTypeName>Couple Package Deluxe Non AC</RoomTypeName>
<RoomTypeCode>45000012484</RoomTypeCode>
<IsActive>False</IsActive>
</Room>
<Room>
<RoomTypeName>Couple Package for Den AC Room</RoomTypeName>
<RoomTypeCode>45000012503</RoomTypeCode>
<IsActive>False</IsActive>
</Room>
<Room>
<RoomTypeName>Couple Package for PREMIUM AC </RoomTypeName>
<RoomTypeCode>45000012507</RoomTypeCode>
<IsActive>False</IsActive>
</Room>
</RoomList>
<RatePlanList>
<RatePlan IsEditable="True">
<RoomTypeName>Deluxe Cottage AC</RoomTypeName>
<RoomTypeCode>45000001713</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000001857</RatePlanCode>
<RatePlanName>APAI</RatePlanName>
<MealPlan>FREE Breakfast, Lunch and Dinner</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
<RatePlan IsEditable="True">
<RoomTypeName>Deluxe Cottage AC</RoomTypeName>
<RoomTypeCode>45000001713</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000012282</RatePlanCode>
<RatePlanName>CPAI</RatePlanName>
<MealPlan>FREE Breakfast</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
<RatePlan IsEditable="True">
<RoomTypeName>Deluxe Cottage AC</RoomTypeName>
<RoomTypeCode>45000001713</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000044053</RatePlanCode>
<RatePlanName>MAPAI</RatePlanName>
<MealPlan>FREE Breakfast and Dinner</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
<RatePlan IsEditable="True">
<RoomTypeName>Den Cottage AC</RoomTypeName>
<RoomTypeCode>45000001714</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000001858</RatePlanCode>
<RatePlanName>APAI</RatePlanName>
<MealPlan>FREE Breakfast, Lunch and Dinner</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
<RatePlan IsEditable="True">
<RoomTypeName>Den Cottage AC</RoomTypeName>
<RoomTypeCode>45000001714</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000012283</RatePlanCode>
<RatePlanName>CPAI</RatePlanName>
<MealPlan>FREE Breakfast</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
<RatePlan IsEditable="True">
<RoomTypeName>Den Cottage AC</RoomTypeName>
<RoomTypeCode>45000001714</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000044054</RatePlanCode>
<RatePlanName>MAPAI</RatePlanName>
<MealPlan>FREE Breakfast and Dinner</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
<RatePlan IsEditable="True">
<RoomTypeName>Premium Cottage AC</RoomTypeName>
<RoomTypeCode>45000001715</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000001859</RatePlanCode>
<RatePlanName>APAI</RatePlanName>
<MealPlan>FREE Breakfast, Lunch and Dinner</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
<RatePlan IsEditable="True">
<RoomTypeName>Premium Cottage AC</RoomTypeName>
<RoomTypeCode>45000001715</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000012284</RatePlanCode>
<RatePlanName>CPAI</RatePlanName>
<MealPlan>FREE Breakfast</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
<RatePlan IsEditable="True">
<RoomTypeName>Premium Cottage AC</RoomTypeName>
<RoomTypeCode>45000001715</RoomTypeCode>
<IsActive>True</IsActive>
<RatePlanCode>990000044055</RatePlanCode>
<RatePlanName>MAPAI </RatePlanName>
<MealPlan>FREE Breakfast and Dinner</MealPlan>
<LinkedRatePlan IsLinked="False"/>
</RatePlan>
</RatePlanList>
</HotelListing>
现在我想从中获取房间数据,我的代码是 $response 是 xml 字符串的地方:
private function hotelDetailsResponse($response)
{
$data = [];
$dom = new \DOMDocument();
$dom->formatOutput = TRUE;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXml($response);
$Rooms = $dom->getElementsByTagName( "Room" );
foreach( $Rooms as $Room ) {
$names = $Room->getElementsByTagName("RoomTypeName");
$data[] = $names->item(0)->nodeValue;
$types = $Room->getElementsByTagName("RoomTypeCode");
$data[] = $types->item(0)->nodeValue;
}
return $data;
}
它给了我这样的数组:
["Deluxe Cottage AC","45000001713","Den Cottage AC","45000001714","Premium Cottage AC","45000001715","Couple Package Deluxe Non AC","45000012484","Couple Package for Den AC Room","45000012503","Couple Package for PREMIUM AC ","45000012507"]
我需要这样的数组:
[{"Name":"Couple Package for PREMIUM AC ","Code":"45000012507"}, {"Name":"Couple Package for PREMIUM AC ","Code":"45000012507"}, {"Name":"Couple Package for PREMIUM AC ","Code":"45000012507"}]
我也尝试像这样在 foreach 中给出值:
$Rooms = $dom->getElementsByTagName( "Room" );
foreach( $Rooms as $Room ) {
$names = $Room->getElementsByTagName("RoomTypeName");
$data['name'] = $names->item(0)->nodeValue;
$types = $Room->getElementsByTagName("RoomTypeCode");
$data['code'] = $types->item(0)->nodeValue;
}
但它只给我最后一个参数,如下所示:
{"name":"Couple Package for PREMIUM AC ","code":"45000012507"}
请帮忙....
【问题讨论】:
-
你能把你的问题简化为一个更简洁的例子吗?很多关于如何提出好问题的方法包括提出minimal, complete and verifiable example。对于有类似问题的其他用户来说会更好,并且更容易回答。