我正在尝试用PHP解析OTA服务的XML响应。我试过下面的代码,但没有运气。
$doc = new DOMDocument();
$doc->loadXML($response);
$XMLresults = $doc->getElementsByTagName("OTA_VehAvailRateRS");
我想从xml响应中获取不同的标记值。有人可以帮我解决这个问题吗?谢谢。例如,我想解析以下xml响应:
<?xml version="1.0"?>
<OTA_VehAvailRateRS xmlns="http://www.opentravel.org/OTA/2003/05" TimeStamp="2018-02-28T01:12:27" Target="Test" Version="4.500" SequenceNmbr="1">
<Success/>
<VehAvailRSCore>
<VehRentalCore PickUpDateTime="2018-03-13T08:00:00" ReturnDateTime="2018-03-30T08:00:00">
<PickUpLocation LocationCode="TEST"/>
<ReturnLocation LocationCode="TEST"/>
</VehRentalCore>
<VehVendorAvails>
<VehVendorAvail>
<VehAvails> <VehAvail>
<VehAvailCore Status="Available">
<Vehicle Code="TEST15" VendorCarType="TEST15" Description="TEST"/>
<RentalRate>
<RateDistance Unlimited="True" DistUnitName="Mile" VehiclePeriodUnitName="RentalPeriod"/>
<VehicleCharges>
<VehicleCharge Description="TEST" Amount="1299.35" CurrencyCode="" GuaranteedInd="True" Purpose="1"> <Calculation UnitCharge="33.32" UnitName="Hour"/>
<Calculation UnitCharge="1560.00" UnitName="Month"/>
<Calculation UnitCharge="499.75" UnitName="Week"/>
<Calculation UnitCharge="99.95" UnitName="Day"/>
<TaxAmounts> </TaxAmounts>
</VehicleCharge>
</VehicleCharges>
</RentalRate>
<Fees> </Fees>
<TotalCharge CurrencyCode="" RateTotalAmount="1299.35" EstimatedTotalAmount="1299.35"/>
<PricedEquips> </PricedEquips> </VehAvailCore> <VehAvailInfo> <PricedCoverages> </PricedCoverages> </VehAvailInfo> </VehAvail> </VehAvails> </VehVendorAvail> </VehVendorAvails> </VehAvailRSCore> </OTA_VehAvailRateRS>
答案 0 :(得分:0)
我找到了解决方案。我使用simplexml_load_string()函数进行解析响应。例如:
$xml = simplexml_load_string($finalresponse);
var_dump($xml); //getting parsed response here
为获取不同xml节点的值,我使用了以下代码:
$nodevalue = $xml->VehAvailRSCore->VehRentalCore->attributes()->PickUpDateTime; //this code gives me "2018-03-13T08:00:00" as result