如何从xml响应中获取元素

时间:2015-06-11 06:27:20

标签: php xml dom

我已将以下xml发送到测试服务器

$data=
    '<RQ>
    <Administration>
    <Username>one</Username>
    <Password>one</Password>
    </Administration>
    <Requests>
    <VehicleRQ lang="33" aff="0" reserv_num="" coo="">
    <pickUpLocation>3788</pickUpLocation> 
    <dropOffLocation>3788</dropOffLocation> 
    <PickTime>2015-6-25 12:00</PickTime> 
    <DropTime>2015-6-26 1:00</DropTime> 
    <DrvAge>33</DrvAge> 
    <AddDrvAge /> 
    <PromCode /> 
    <PriceList>5</PriceList> 
    <DrvCountry>gb</DrvCountry> 
    </VehicleRQ>
    </Requests>
    </RQ>';


$ch = curl_init(); 
      curl_setopt ($ch, CURLOPT_URL, 'http://testsdsd.com/aslvaice.php'); 
      curl_setopt ($ch, CURLOPT_POST, 1); 
      curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); 
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
      $out = curl_exec( $ch); 
      Header ("Content-type: text/xml"); 
      print_r($out); 

我收到的回复如下:

响应:

<RS><Administration><Errors/></Administration><Responses><VehicleRS commType="" comm=""><VehicleInfo><Car onRequest="0" id="9349420" group="SUV" subgroup="Standard" class="IFAR" internalClass="SFAR" transmissionId="AUTOMATIC" transmission="Automatic" airco="YES" doors="5" seats="5" airbag="" fuelId="N_A" fuel="N/A" comments="Excess USD 0" image="Toyota-Rav4.jpg" smallSuitcases="4" bigSuitcases="" freeSell="0" searchId="bVDLTsMwEPwXn6PKr8R1byA49NYD4m5sx6xa7GiTVkKIf2cdA0XAcR67sztvbI4O_fM-sF1ve2Ol7Jh3WLFV2mrJOxZg9uWcl0oy1rEJ_PE8PcBLJCy06qWUW85JCVimMo4_pcEOq-QLEpWe6rzzR5diXSfWtAOCp4FBbNSW5Ia02Qh1DY_h8C_PdnThJSKMr58GUe8Aom4SIUVeFwIsULI73V0F_vXILbrcGjBGK_P9xR-ebm4l8PpOXtD5Zc08wfybf4w4U2KLIQ_kdJ8T5NhKfP8A" availChecked="0" excess="0.00" excessCurrency="USD" driverMinimumAge="21" driverMaximumAge="75" gps="0">Toyota Rav4</Car><PriceCust realdeposit="6.5" balancePaymentDate="" commission="0" currency="GBP" localCurrency="USD" drvFee="0.00" addDrvFee="0.00" dropOffFee="0.00" isDropOffFeeLocal="1" isOutOfHoursFeeLocal="1" isYoungDriverFeeLocal="1" outOfHourFee="0.00" discount="0.00" promDiscount="0" mult="0.6619842315356" discountedPrice="47.13" duration="1">47.13</PriceCust><Supplier logo="IsUBeyST.jpg" isMajorSupplier="0" locId="77437" dropLocId="77437" locationId="3788" dropOffLocationId="3788" locName="LAX" isDeposit="1" productType="1" hasOnlinePrices="1" hasXmlBooking="1" supplierId="11" pickUpLatitude="33.9494000" pickUpLongitude="-118.3850000">Sixt</Supplier><Packages><Package id="1" default="1"><Name>Default</Name><Price searchId="bVDLTsMwEPwXn6PKr8R1byA49NYD4m5sx6xa7GiTVkKIf2cdA0XAcR67sztvbI4O_fM-sF1ve2Ol7Jh3WLFV2mrJOxZg9uWcl0oy1rEJ_PE8PcBLJCy06qWUW85JCVimMo4_pcEOq-QLEpWe6rzzR5diXSfWtAOCp4FBbNSW5Ia02Qh1DY_h8C_PdnThJSKMr58GUe8Aom4SIUVeFwIsULI73V0F_vXILbrcGjBGK_P9xR-ebm4l8PpOXtD5Zc08wfybf4w4U2KLIQ_kdJ8T5NhKfP8A" currency="GBP" discountedPrice="47.13 ">47.13 </Price><Items/></Package></Packages><FuelPolicy code=""><Name></Name><Description></Description></FuelPolicy><VehicleAt id=""/></VehicleInfo></VehicleRS></Responses></RS>

根据此回复,我无法从汽车标签中获取该元素。如何通过此响应从汽车标签获取“搜索ID”等属性? 感谢。

1 个答案:

答案 0 :(得分:0)

从下面的结果数组中,您可以获得CAR标记响应:

$simple = '<RS><Administration><Errors/></Administration><Responses><VehicleRS commType="" comm=""><VehicleInfo><Car onRequest="0" id="9349420" group="SUV" subgroup="Standard" class="IFAR" internalClass="SFAR" transmissionId="AUTOMATIC" transmission="Automatic" airco="YES" doors="5" seats="5" airbag="" fuelId="N_A" fuel="N/A" comments="Excess USD 0" image="Toyota-Rav4.jpg" smallSuitcases="4" bigSuitcases="" freeSell="0" searchId="bVDLTsMwEPwXn6PKr8R1byA49NYD4m5sx6xa7GiTVkKIf2cdA0XAcR67sztvbI4O_fM-sF1ve2Ol7Jh3WLFV2mrJOxZg9uWcl0oy1rEJ_PE8PcBLJCy06qWUW85JCVimMo4_pcEOq-QLEpWe6rzzR5diXSfWtAOCp4FBbNSW5Ia02Qh1DY_h8C_PdnThJSKMr58GUe8Aom4SIUVeFwIsULI73V0F_vXILbrcGjBGK_P9xR-ebm4l8PpOXtD5Zc08wfybf4w4U2KLIQ_kdJ8T5NhKfP8A" availChecked="0" excess="0.00" excessCurrency="USD" driverMinimumAge="21" driverMaximumAge="75" gps="0">Toyota Rav4</Car><PriceCust realdeposit="6.5" balancePaymentDate="" commission="0" currency="GBP" localCurrency="USD" drvFee="0.00" addDrvFee="0.00" dropOffFee="0.00" isDropOffFeeLocal="1" isOutOfHoursFeeLocal="1" isYoungDriverFeeLocal="1" outOfHourFee="0.00" discount="0.00" promDiscount="0" mult="0.6619842315356" discountedPrice="47.13" duration="1">47.13</PriceCust><Supplier logo="IsUBeyST.jpg" isMajorSupplier="0" locId="77437" dropLocId="77437" locationId="3788" dropOffLocationId="3788" locName="LAX" isDeposit="1" productType="1" hasOnlinePrices="1" hasXmlBooking="1" supplierId="11" pickUpLatitude="33.9494000" pickUpLongitude="-118.3850000">Sixt</Supplier><Packages><Package id="1" default="1"><Name>Default</Name><Price searchId="bVDLTsMwEPwXn6PKr8R1byA49NYD4m5sx6xa7GiTVkKIf2cdA0XAcR67sztvbI4O_fM-sF1ve2Ol7Jh3WLFV2mrJOxZg9uWcl0oy1rEJ_PE8PcBLJCy06qWUW85JCVimMo4_pcEOq-QLEpWe6rzzR5diXSfWtAOCp4FBbNSW5Ia02Qh1DY_h8C_PdnThJSKMr58GUe8Aom4SIUVeFwIsULI73V0F_vXILbrcGjBGK_P9xR-ebm4l8PpOXtD5Zc08wfybf4w4U2KLIQ_kdJ8T5NhKfP8A" currency="GBP" discountedPrice="47.13 ">47.13 </Price><Items/></Package></Packages><FuelPolicy code=""><Name></Name><Description></Description></FuelPolicy><VehicleAt id=""/></VehicleInfo></VehicleRS></Responses></RS>';
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals);
xml_parser_free($p);
echo '<pre>';
var_dump($vals);