douwei7976 2018-03-05 08:35
浏览 54
已采纳

无法转换OTA API xml响应?

I'm trying to parse OTA service's XML response in PHP. I have tried below code but no luck.

$doc = new DOMDocument();

$doc->loadXML($response);

$XMLresults = $doc->getElementsByTagName("OTA_VehAvailRateRS");

I want to get different tags value from xml response. Can someone help me out from this? Thanks. For example i want to parse following xml response:

<?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>
  • 写回答

1条回答 默认 最新

  • duanhai1455 2018-03-07 09:31
    关注

    I have found solution. I have used simplexml_load_string() function for parse response. For ex:

    $xml = simplexml_load_string($finalresponse);
    var_dump($xml); //getting parsed response here
    

    For getting value of different xml nodes i have used below code:

    $nodevalue = $xml->VehAvailRSCore->VehRentalCore->attributes()->PickUpDateTime; //this code gives me "2018-03-13T08:00:00" as result
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看