如何使用php从XML文件中读取数据

时间:2016-12-08 10:36:42

标签: php xml api loops xmlreader

我想使用php代码从下面的xml文件中读取数据。

下面是XML文件,我想从中读取数据。

<availabilityRS xmlns="http://www.hotelbeds.com/schemas/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.hotelbeds.com/schemas/messages">
<auditData processTime="4331" timestamp="2016-12-08 11:05:49.211" requestHost="182.48.20.151" serverId="sa37AUX3ROLBLIS.env" environment="[int]" release="8629cf6ef0a2deed178b86daa0e13c79b5c99237" internal="0|D4A049C5629A4D3B81ED8C1609B92A30|UK|06|1|39||||||||||||25||1~1~2~0|||||vgw3t5723ryk95m4q58m3g87||"/>
<hotels checkIn="2016-12-15" checkOut="2016-12-16" total="1">
<hotel code="1070" name="Armadams" categoryCode="4EST" categoryName="4 STARS" destinationCode="PMI" destinationName="Majorca" zoneCode="10" zoneName="Palma" latitude="39.568861" longitude="2.630294" minRate="69.34" maxRate="224.00" currency="EUR">
<rooms>
<room code="DBT.ST" name="Double or Twin STANDARD">
<rates>
<rate rateKey="20161215|20161216|W|1|1070|DBT.ST|CG- FIT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="69.34" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="69.34" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|DBT.ST|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="70.32" sellingRate="80.00" hotelSellingRate="80.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="8.00" commissionVAT="1.68" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="80.00" hotelAmount="80.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.ST|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="69.79" sellingRate="80.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="69.79" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.ST|CG- FIT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="96.18" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="96.18" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|DBT.ST|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="96.69" sellingRate="110.00" hotelSellingRate="110.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="11.00" commissionVAT="2.31" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="110.00" hotelAmount="110.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.ST|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="95.97" sellingRate="110.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="95.97" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.ST|CG- FIT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="140.92" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="140.92" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|DBT.ST|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="140.64" sellingRate="160.00" hotelSellingRate="160.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="16.00" commissionVAT="3.36" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="160.00" hotelAmount="160.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.ST|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="139.57" sellingRate="160.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="139.57" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
</rates>
</room>
<room code="DBT.SU" name="Double or Twin SUPERIOR">
<rates>
<rate rateKey="20161215|20161216|W|1|1070|DBT.SU|CG- FIT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="91.70" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="91.70" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|DBT.SU|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="93.17" sellingRate="106.00" hotelSellingRate="106.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="10.60" commissionVAT="2.23" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="106.00" hotelAmount="106.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.SU|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="92.47" sellingRate="106.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="92.47" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.SU|CG- FIT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="118.54" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="118.54" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.SU|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="118.65" sellingRate="136.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="118.65" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|DBT.SU|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="119.54" sellingRate="136.00" hotelSellingRate="136.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="13.60" commissionVAT="2.86" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="136.00" hotelAmount="136.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.SU|CG- FIT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="163.28" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="163.28" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|DBT.SU|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="163.49" sellingRate="186.00" hotelSellingRate="186.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="18.60" commissionVAT="3.91" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="186.00" hotelAmount="186.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|DBT.SU|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="162.25" sellingRate="186.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="162.25" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
</rates>
</room>
<room code="TPL.ST" name="TRIPLE STANDARD">
<rates>
<rate rateKey="20161215|20161216|H|1|1070|TPL.ST|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="94.93" sellingRate="108.00" hotelSellingRate="108.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="10.80" commissionVAT="2.27" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="108.00" hotelAmount="108.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|TPL.ST|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="94.22" sellingRate="108.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="94.22" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|TPL.ST|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="121.30" sellingRate="138.00" hotelSellingRate="138.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="13.80" commissionVAT="2.90" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="138.00" hotelAmount="138.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|TPL.ST|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="120.40" sellingRate="138.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="120.40" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|TPL.ST|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="165.25" sellingRate="188.00" hotelSellingRate="188.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="18.80" commissionVAT="3.95" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="188.00" hotelAmount="188.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|TPL.ST|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="164.00" sellingRate="188.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="164.00" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
</rates>
</room>
<room code="JSU.ST" name="JUNIOR SUITE STANDARD">
<rates>
<rate rateKey="20161215|20161216|W|1|1070|JSU.ST|CG- FIT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="123.02" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="123.02" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|JSU.ST|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="126.58" sellingRate="144.00" hotelSellingRate="144.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="14.40" commissionVAT="3.02" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="144.00" hotelAmount="144.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|JSU.ST|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="125.62" sellingRate="144.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="125.62" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|JSU.ST|CG- FIT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="149.86" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="149.86" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|JSU.ST|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="151.80" sellingRate="174.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="151.80" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|JSU.ST|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="152.95" sellingRate="174.00" hotelSellingRate="174.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="17.40" commissionVAT="3.65" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="174.00" hotelAmount="174.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|JSU.ST|CG- FIT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="194.60" allotment="99" rateCommentsId="1|94855|3" paymentType="AT_WEB" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="194.60" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|JSU.ST|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="196.90" sellingRate="224.00" hotelSellingRate="224.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="22.40" commissionVAT="4.70" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="224.00" hotelAmount="224.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|JSU.ST|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="195.40" sellingRate="224.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="195.40" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
</rates>
</room>
<room code="QUA.ST" name="QUADRUPLE STANDARD">
<rates>
<rate rateKey="20161215|20161216|H|1|1070|QUA.ST|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="115.15" sellingRate="131.00" hotelSellingRate="131.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="13.10" commissionVAT="2.75" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="131.00" hotelAmount="131.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|QUA.ST|CG- MERCHANT|RO||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="114.28" sellingRate="131.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="RO" boardName="ROOM ONLY" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="114.28" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|QUA.ST|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="141.52" sellingRate="161.00" hotelSellingRate="161.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="16.10" commissionVAT="3.38" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="161.00" hotelAmount="161.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|QUA.ST|CG- MERCHANT|BB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="140.46" sellingRate="161.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="BB" boardName="BED AND BREAKFAST" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="140.46" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|W|1|1070|QUA.ST|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="184.06" sellingRate="211.00" hotelMandatory="true" allotment="99" rateCommentsId="1|90440|96" paymentType="AT_WEB" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="184.06" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
<rate rateKey="20161215|20161216|H|1|1070|QUA.ST|CG- MERCHANT|HB||1~2~0||N@D4A049C5629A4D3B81ED8C1609B92A30" rateClass="NOR" rateType="BOOKABLE" net="185.47" sellingRate="211.00" hotelSellingRate="211.00" hotelCurrency="EUR" hotelMandatory="true" allotment="99" commission="21.10" commissionVAT="4.43" commissionPCT="10.00" rateCommentsId="1|90440|96" paymentType="AT_HOTEL" packaging="false" boardCode="HB" boardName="HALF BOARD" rooms="1" adults="2" children="0" childrenAges="">
<cancellationPolicies>
<cancellationPolicy amount="211.00" hotelAmount="211.00" hotelCurrency="EUR" from="2016-12-12T23:59:00+01:00"/>
</cancellationPolicies>
</rate>
</rates>
</room>
</rooms>
<creditCards>
<creditCard code="VI" name="VISA-SSL" paymentType="AT_HOTEL"/>
<creditCard code="AE" name="AMEX-SSL" paymentType="AT_HOTEL"/>
<creditCard code="MC" name="ECMC-SSL" paymentType="AT_HOTEL"/>
<creditCard code="DC" name="DINERS-SSL" paymentType="AT_HOTEL"/>
</creditCards>
</hotel>
</hotels>
</availabilityRS>

我试过这个代码从xml中检索数据。

下面我附上了用于从xml

检索数据的代码
$xml  = simplexml_load_string("myxml.xml");
foreach($xml as $hotel) { 
   echo "Hotel Name: ".$hotel->hotel['name']."<br>";
   echo "Hotel Category: ".$hotel->hotel['categoryName']."<br>";
   echo "Hotel Destination: ".$hotel->hotel['destinationName']."<br>";
   echo "Room Name: ".$hotel->hotel->rooms->room['name']."<br>";
   //echo "<br>"; 
}

但结果是

Hotel Name: 
Hotel Category: 
Hotel Destination: 
Room Name: 
Hotel Name: Armadams
Hotel Category: 4 STARS
Hotel Destination: Majorca
Room Name: Double or Twin STANDARD

所有房间数据都没有显示,有没有其他方法可以从xml读取数据?

3 个答案:

答案 0 :(得分:0)

代码中的

$xml可能是指根元素,即<availabilityRS>。您需要更改循环以转到<hotels><hotel>s.及其房间。如果您正在从文件中读取XML数据,则还需要将simplexml_load_string更改为simplexml_load_file

$xml = simplexml_load_file(__DIR__.'/myxml.xml');

foreach ($xml->hotels[0] as $hotel) {
    echo $hotel['name']."<br>";
    echo $hotel['categoryName']."<br>";
    echo $hotel['destinationName']."<br>";
    echo "rooms:<br>";
    foreach ($hotel->rooms[0] as $room) {
        echo $room['name']."<br>";
    }
}

答案 1 :(得分:0)

以下示例代码可以提供帮助:

<?php
$availabilityrs = simplexml_load_file('temp.xml');
$hotel = $availabilityrs->hotels->hotel;
$room_nodes = $hotel->rooms->room;
foreach ($room_nodes as $room) {
    echo "Hotel Name: ".$hotel['name']."\n";
    echo "Hotel Category: ".$hotel['categoryName']."\n";
    echo "Hotel Destination: ".$hotel['destinationName']."\n";
    echo "Room Name: ".$room['name']."\n";
}

请参阅this manul

  

当元素的多个实例作为单个父元素的子元素存在时,将应用常规迭代技术。

如果hotel多,您也应该迭代它。

答案 2 :(得分:0)

遍历XML元素,面向对象的方式对于某些人来说可能是混乱的。将它们转换为数组元素会更好,然后更容易迭代它们。下面是将XML对象转换为数组并遍历它们的代码:

$xml = simplexml_load_file('myxml.xml');
$xml = json_decode(json_encode($xml), true);
$hotel = $xml['hotels']['hotel'];

foreach($hotel['rooms'] as $rooms) {
    foreach($rooms as $room) {
        echo "<p>Hotel Name: {$hotel['@attributes']['name']}</p>";
        echo "<p>Hotel Category: {$hotel['@attributes']['categoryName']}</p>";
        echo "<p>Hotel Destination: {$hotel['@attributes']['destinationName']}</p>";
        echo "<p>Room Name: {$room['@attributes']['name']}</p>";
        echo "<br><br>";
    }
}

希望它有所帮助!