我有一些XML文件包含酒店數據。我需要解析這些文件中的酒店數據並保存到PHP數組中。必填字段來自Hotel節點和城市的brandId,brandName,名稱,地址等,來自Location節點的地址。我使用的是SimpleXML PHP擴展。但它不適合我。使用XML數組解析XML數據
是否有任何其他良好的PHP腳本來完成這項任務。或者我如何更新我的當前腳本。我在下面提供了我的示例XML。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SearchHotelsResponse xmlns="http://example.com/webservices/hotelhds">
<SearchHotelsResult>
<Info version="7.123" culture="en-US" serverTime="2017-03-01T05:20:08.9411324-05:00" xmlns="http://schemas.abc.com/webservices/hotelV5rggs"/>
<HotelList xmlns="http://schemas.abc.com/webservices/hotelrshhslkmnj">
<Hotel brandId="424" brandName="Independent Managed Hotels" category="Deluxe" PropertySubType="" NumOfRoom="899" minAverPublishPrice="345.16" PropertyType="Hotel" name="Lotte New York Palace - Demo" bestValue="false" hotelId="1040577" provider="localTgsProvider" thumb="http://image1.urlforimages.com/Images/1040577/100x100/1559480295.jpg" currency="USD" minAverPrice="345.16" starsLevel="4.5" desc="Demo - Location
This modern luxury hotel is one of the top addresses in the country. It enjoys a superb midtown location on Madison Avenue, opposite St Patrick's Cathedral and directly at the heart of the city. Countless shops and entertainment venues, b">
<Location countryCode="US" stateCode="NY" city="New York" searchingState="New York" searchingCity="New York" location="Manhattan - Midtown" address="Demo - 455 Madison Avenue New York New York 10022" longitude="-73.9753354497376" latitude="40.758002144334"/>
<RoomTypes>
<RoomType NumOfBathrooms="0" roomTypeCategory="Superior" roomTypeCategoryId="20" nights="7" name="Superior" productId="1040577;2754674;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="2754674" roomId="80" isAvailable="true">
<AvailabilityBreakdown>
<Availability offset="0" status="true"/>
<Availability offset="1" status="true"/>
<Availability offset="2" status="true"/>
<Availability offset="3" status="true"/>
<Availability offset="4" status="true"/>
<Availability offset="5" status="true"/>
<Availability offset="6" status="true"/>
</AvailabilityBreakdown>
<Occupancies>
<Occupancy taxPublish="335.05" avrNightPublishPrice="345.16" occupId="1040577;2754674;302661;3;2;0" occupPrice="2416.1" tax="335.05" maxGuests="2" maxChild="0" avrNightPrice="345.16" occupPublishPrice="2416.1" isPublish="false" bedding="2,1">
<PriceBreakdown>
<Price offset="0" value="408.38" valuePublish="408.38"/>
<Price offset="1" value="408.38" valuePublish="408.38"/>
<Price offset="2" value="408.38" valuePublish="408.38"/>
<Price offset="3" value="408.38" valuePublish="408.38"/>
<Price offset="4" value="260.86" valuePublish="260.86"/>
<Price offset="5" value="260.86" valuePublish="260.86"/>
<Price offset="6" value="260.86" valuePublish="260.86"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements/>
<BoardBases/>
</Occupancy>
</Occupancies>
</RoomType>
<RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Catheral View" productId="1040577;1165849;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="1165849" roomId="4768962" isAvailable="true">
<AvailabilityBreakdown>
<Availability offset="0" status="true"/>
<Availability offset="1" status="true"/>
<Availability offset="2" status="true"/>
<Availability offset="3" status="true"/>
<Availability offset="4" status="true"/>
<Availability offset="5" status="true"/>
<Availability offset="6" status="true"/>
</AvailabilityBreakdown>
<Occupancies>
<Occupancy taxPublish="378.77" avrNightPublishPrice="393.71" occupId="1040577;1165849;302661;3;2;0" occupPrice="2755.94" tax="378.77" maxGuests="2" maxChild="0" avrNightPrice="393.71" occupPublishPrice="2755.94" isPublish="false" bedding="2,1">
<PriceBreakdown>
<Price offset="0" value="463.7" valuePublish="463.7"/>
<Price offset="1" value="463.7" valuePublish="463.7"/>
<Price offset="2" value="463.7" valuePublish="463.7"/>
<Price offset="3" value="463.7" valuePublish="463.7"/>
<Price offset="4" value="300.38" valuePublish="300.38"/>
<Price offset="5" value="300.38" valuePublish="300.38"/>
<Price offset="6" value="300.38" valuePublish="300.38"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements/>
<BoardBases/>
</Occupancy>
</Occupancies>
</RoomType>
<RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Tower King" productId="1040577;4014667;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="4014667" roomId="2203455" isAvailable="true">
<AvailabilityBreakdown>
<Availability offset="0" status="true"/>
<Availability offset="1" status="true"/>
<Availability offset="2" status="true"/>
<Availability offset="3" status="true"/>
<Availability offset="4" status="true"/>
<Availability offset="5" status="true"/>
<Availability offset="6" status="true"/>
</AvailabilityBreakdown>
<Occupancies>
<Occupancy taxPublish="553.49" avrNightPublishPrice="587.89" occupId="1040577;4014667;302661;3;2;0" occupPrice="4115.21" tax="553.49" maxGuests="2" maxChild="0" avrNightPrice="587.89" occupPublishPrice="4115.21" isPublish="false" bedding="2,1">
<PriceBreakdown>
<Price offset="0" value="684.98" valuePublish="684.98"/>
<Price offset="1" value="684.98" valuePublish="684.98"/>
<Price offset="2" value="684.98" valuePublish="684.98"/>
<Price offset="3" value="684.98" valuePublish="684.98"/>
<Price offset="4" value="458.43" valuePublish="458.43"/>
<Price offset="5" value="458.43" valuePublish="458.43"/>
<Price offset="6" value="458.43" valuePublish="458.43"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements/>
<BoardBases/>
</Occupancy>
</Occupancies>
</RoomType>
<RoomType NumOfBathrooms="0" roomTypeCategory="Suite" roomTypeCategoryId="19" nights="7" name="Tower Executive Suite" productId="1040577;13147974;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="13147974" roomId="4830397" isAvailable="true">
<AvailabilityBreakdown>
<Availability offset="0" status="true"/>
<Availability offset="1" status="true"/>
<Availability offset="2" status="true"/>
<Availability offset="3" status="true"/>
<Availability offset="4" status="true"/>
<Availability offset="5" status="true"/>
<Availability offset="6" status="true"/>
</AvailabilityBreakdown>
<Occupancies>
<Occupancy taxPublish="651.49" avrNightPublishPrice="696.84" occupId="1040577;13147974;302661;3;2;0" occupPrice="4877.88" tax="651.49" maxGuests="2" maxChild="0" avrNightPrice="696.84" occupPublishPrice="4877.88" isPublish="false" bedding="2,1">
<PriceBreakdown>
<Price offset="0" value="696.84" valuePublish="696.84"/>
<Price offset="1" value="696.84" valuePublish="696.84"/>
<Price offset="2" value="696.84" valuePublish="696.84"/>
<Price offset="3" value="696.84" valuePublish="696.84"/>
<Price offset="4" value="696.84" valuePublish="696.84"/>
<Price offset="5" value="696.84" valuePublish="696.84"/>
<Price offset="6" value="696.84" valuePublish="696.84"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements/>
<BoardBases/>
</Occupancy>
</Occupancies>
</RoomType>
<RoomType NumOfBathrooms="0" roomTypeCategory="Suite" roomTypeCategoryId="19" nights="7" name="Tower Corner Suite" productId="1040577;13147975;302661" startDate="2017-03-20T00:00:00" hotelRoomTypeId="13147975" roomId="4830398" isAvailable="true">
<AvailabilityBreakdown>
<Availability offset="0" status="true"/>
<Availability offset="1" status="true"/>
<Availability offset="2" status="true"/>
<Availability offset="3" status="true"/>
<Availability offset="4" status="true"/>
<Availability offset="5" status="true"/>
<Availability offset="6" status="true"/>
</AvailabilityBreakdown>
<Occupancies>
<Occupancy taxPublish="1004.49" avrNightPublishPrice="1089.16" occupId="1040577;13147975;302661;3;2;0" occupPrice="7624.11" tax="1004.49" maxGuests="2" maxChild="0" avrNightPrice="1089.16" occupPublishPrice="7624.11" isPublish="false" bedding="2,1">
<PriceBreakdown>
<Price offset="0" value="1111.74" valuePublish="1111.74"/>
<Price offset="1" value="1111.74" valuePublish="1111.74"/>
<Price offset="2" value="1111.74" valuePublish="1111.74"/>
<Price offset="3" value="1111.74" valuePublish="1111.74"/>
<Price offset="4" value="1059.05" valuePublish="1059.05"/>
<Price offset="5" value="1059.05" valuePublish="1059.05"/>
<Price offset="6" value="1059.05" valuePublish="1059.05"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements/>
<BoardBases/>
</Occupancy>
</Occupancies>
</RoomType>
</RoomTypes>
</Hotel>
<Hotel brandId="288" brandName="Holiday Inn" category="First Class" PropertySubType="" NumOfRoom="200" minAverPublishPrice="202.77" PropertyType="Hotel" name="Holiday Inn NYC Times Square - Demo" bestValue="true" hotelId="1437966" provider="localTgsProvider" thumb="http://image1.urlforimages.com/Images/1437966/100x100/8012H66O018.JPG" currency="USD" minAverPrice="202.77" starsLevel="3.5" desc="Demo - ">
<Location countryCode="US" stateCode="NY" city="New York" searchingState="New York" searchingCity="New York" location="Manhattan - Times Square/Broadway" address="Demo - 585 8th Avenue New York New York 10018" longitude="-73.991712" latitude="40.755229"/>
<RoomTypes>
<RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Standard 2 Beds" productId="1437966;14787188;12927607" startDate="2017-03-20T00:00:00" hotelRoomTypeId="14787188" roomId="1286" isAvailable="true">
<AvailabilityBreakdown>
<Availability offset="0" status="true"/>
<Availability offset="1" status="true"/>
<Availability offset="2" status="true"/>
<Availability offset="3" status="true"/>
<Availability offset="4" status="true"/>
<Availability offset="5" status="true"/>
<Availability offset="6" status="true"/>
</AvailabilityBreakdown>
<Discount xsi:type="ProgressivePromotion" from="2017-03-20T00:00:00" to="2017-03-27T00:00:00" type="Percent" value="10" name="Special Deal"/>
<Occupancies>
<Occupancy taxPublish="208.76" avrNightPublishPrice="202.77" occupId="1437966;14787188;12927607;2;2;0;2" occupPrice="1419.4" tax="208.76" maxGuests="4" maxChild="2" avrNightPrice="202.77" occupPublishPrice="1419.4" isPublish="false" bedding="2,2">
<PriceBreakdown>
<Price offset="0" value="207.86" valuePublish="207.86"/>
<Price offset="1" value="220.58" valuePublish="220.58"/>
<Price offset="2" value="207.86" valuePublish="207.86"/>
<Price offset="3" value="195.14" valuePublish="195.14"/>
<Price offset="4" value="195.14" valuePublish="195.14"/>
<Price offset="5" value="220.58" valuePublish="220.58"/>
<Price offset="6" value="172.24" valuePublish="172.24"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements>
<Supplement xsi:type="PerRoomSupplement" suppId="1200127" suppName="2 tickets for Empire State Building " supptType="1" suppIsMandatory="false" suppChargeType="Addition" price="52.85" publishPrice="52.85"/>
</SelctedSupplements>
<BoardBases>
<Boardbase bbId="1" bbName="Continental Breakfast" bbPrice="356.21" bbPublishPrice="356.21"/>
</BoardBases>
</Occupancy>
</Occupancies>
</RoomType>
</RoomTypes>
</Hotel>
<Hotel brandId="51" brandName="Embassy Suites Hotels" category="First Class" PropertySubType="" NumOfRoom="189" minAverPublishPrice="198.19" PropertyType="Hotel" name="Embassy Suites Elizabeth - Newark Airport - Demo" bestValue="false" hotelId="1343771" provider="localTgsProvider" thumb="http://image1.urlforimages.com/Images/1343771/100x100/1629572161.jpg" currency="USD" minAverPrice="198.19" starsLevel="3.5" desc="Demo - Our straightforward suites include separate living rooms with pull-out sofas, microwaves, minifridges and coffeemakers, plus 2 flat-screen TVs, and WiFi (fee).

A free made-to-order breakfast is offered each day">
<Location countryCode="US" stateCode="NJ" city="Elizabeth" searchingState="New Jersey" searchingCity="Elizabeth" location="Newark Liberty International Airport" address="Demo - 95 International Blvd Elizabeth New Jersey 07201" longitude="-74.1771680963125" latitude="40.6621379134249"/>
<RoomTypes>
<RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Standard" productId="1343771;13479523;11755505" startDate="2017-03-20T00:00:00" hotelRoomTypeId="13479523" roomId="3" isAvailable="true">
<AvailabilityBreakdown>
<Availability offset="0" status="true"/>
<Availability offset="1" status="true"/>
<Availability offset="2" status="true"/>
<Availability offset="3" status="true"/>
<Availability offset="4" status="true"/>
<Availability offset="5" status="true"/>
<Availability offset="6" status="true"/>
</AvailabilityBreakdown>
<Occupancies>
<Occupancy taxPublish="236.02" avrNightPublishPrice="198.19" occupId="1343771;13479523;11755505;2;2;0;1" occupPrice="1387.31" tax="236.02" maxGuests="4" maxChild="2" avrNightPrice="198.19" occupPublishPrice="1387.31" isPublish="false" bedding="2,1">
<PriceBreakdown>
<Price offset="0" value="206.09" valuePublish="206.09"/>
<Price offset="1" value="206.09" valuePublish="206.09"/>
<Price offset="2" value="206.09" valuePublish="206.09"/>
<Price offset="3" value="206.09" valuePublish="206.09"/>
<Price offset="4" value="178.43" valuePublish="178.43"/>
<Price offset="5" value="178.43" valuePublish="178.43"/>
<Price offset="6" value="206.09" valuePublish="206.09"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements/>
<BoardBases>
<Boardbase bbId="4" bbName="Breakfast Buffet" bbPrice="0" bbPublishPrice="0"/>
</BoardBases>
</Occupancy>
</Occupancies>
</RoomType>
<RoomType NumOfBathrooms="0" roomTypeCategory="Standard" roomTypeCategoryId="17" nights="7" name="Standard Double" productId="1343771;15050057;11755505" startDate="2017-03-20T00:00:00" hotelRoomTypeId="15050057" roomId="932" isAvailable="true">
<AvailabilityBreakdown>
<Availability offset="0" status="true"/>
<Availability offset="1" status="true"/>
<Availability offset="2" status="true"/>
<Availability offset="3" status="true"/>
<Availability offset="4" status="true"/>
<Availability offset="5" status="true"/>
<Availability offset="6" status="true"/>
</AvailabilityBreakdown>
<Occupancies>
<Occupancy taxPublish="236.02" avrNightPublishPrice="198.19" occupId="1343771;15050057;11755505;2;2;0;1" occupPrice="1387.31" tax="236.02" maxGuests="4" maxChild="0" avrNightPrice="198.19" occupPublishPrice="1387.31" isPublish="false" bedding="2,1">
<PriceBreakdown>
<Price offset="0" value="206.09" valuePublish="206.09"/>
<Price offset="1" value="206.09" valuePublish="206.09"/>
<Price offset="2" value="206.09" valuePublish="206.09"/>
<Price offset="3" value="206.09" valuePublish="206.09"/>
<Price offset="4" value="178.43" valuePublish="178.43"/>
<Price offset="5" value="178.43" valuePublish="178.43"/>
<Price offset="6" value="206.09" valuePublish="206.09"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements/>
<BoardBases>
<Boardbase bbId="4" bbName="Breakfast Buffet" bbPrice="0" bbPublishPrice="0"/>
</BoardBases>
</Occupancy>
<Occupancy taxPublish="236.02" avrNightPublishPrice="198.19" occupId="1343771;15050057;11755505;2;2;0;2" occupPrice="1387.31" tax="236.02" maxGuests="4" maxChild="0" avrNightPrice="198.19" occupPublishPrice="1387.31" isPublish="false" bedding="2,2">
<PriceBreakdown>
<Price offset="0" value="206.09" valuePublish="206.09"/>
<Price offset="1" value="206.09" valuePublish="206.09"/>
<Price offset="2" value="206.09" valuePublish="206.09"/>
<Price offset="3" value="206.09" valuePublish="206.09"/>
<Price offset="4" value="178.43" valuePublish="178.43"/>
<Price offset="5" value="178.43" valuePublish="178.43"/>
<Price offset="6" value="206.09" valuePublish="206.09"/>
</PriceBreakdown>
<Rooms>
<Room seqNum="1">
<AdultNum>2</AdultNum>
<ChildNum>0</ChildNum>
</Room>
</Rooms>
<SelctedSupplements/>
<BoardBases>
<Boardbase bbId="4" bbName="Breakfast Buffet" bbPrice="0" bbPublishPrice="0"/>
</BoardBases>
</Occupancy>
</Occupancies>
</RoomType>
</RoomTypes>
</Hotel>
</HotelList>
</SearchHotelsResult>
</SearchHotelsResponse>
</s:Body>
我的PHP腳本:
$hotels = simplexml_load_file('newyork.xml') or die("Error: Cannot create object");
echo "<ul id='hotelList'>";
foreach ($hotels->hotel as $hotelinfo)
{
$title = $hotelinfo['name'];
$brandId = $hotelinfo['brandId'];
echo "Test1";
//$artist=$hotelinfo->artist;
//$date=$hotelinfo['dateplayed'];
echo "<li><div class='title'>",$title,"</div><div class='artist'>by ",$brandId,"</div><time>",1111,"</time></li>";
}
echo "</ul>";
這個腳本prodcuing 「錯誤:無法創建對象」。
請幫忙。謝謝。
完美運作!非常感謝Casimir .. – shfkktm
如果我們將XML作爲響應,我們如何在這裏使用simplexml_load_string($ response)而不是simplexml_load_file? – shfkktm
@ user1693763:你不需要改變任何東西,只需用'simplexml_load_string'替換'simplexml_load_string'和相同的參數,除了第一個必須是包含你的xml的字符串。 –