私はphpを初めて使い、xml apiを扱うプロジェクトに取り組んでいます。私はプロジェクトを作成するためにlaravel 5.2フレームワークを使用しています。laravel xml apiからデータを取得します。
私が持っているこのような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>
、今私はそれから部屋データを取得したいと$応答は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"}
親切に助けて....
あなたはより多くのsuccint例にあなたの質問を削減できますか?すばらしい質問をするには、[最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)を作成することが重要です。同様の問題を抱えている他のユーザーにとっては、より簡単に答えることができます。 – ssice