2014-07-01 129 views
-1
SimpleXMLElement Object 
(
[FlightOption] => Array 
    (
    [0] => SimpleXMLElement Object 
     (
     [Flight] => SimpleXMLElement Object 
      (
      [@attributes] => Array 
      (
       [ArrivalDateTime] => 2014-07-05T15:00:00.000 
       [DepartureDateTime] => 2014-07-05T13:00:00.000 
       [EquipmentCode] => 321 
       [FlightNumber] => 677 
      ) 
      [Airline] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [AirlineCode] => AI 
          [IATACode] => AI 
          [ICAOCode] => AIC 
          [Name] => Air India 
         ) 
       ) 
      [ArrivalAirport] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [AirportCode] => DEL 
          [IATACode] => DEL 
          [ICAOCode] => VIDP 
          [Name] => Indira Gandhi International Airport 
         ) 
       ) 
      [Cabin] => Array 
       (
        [0] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (
            [Code] => C 
            [Name] => Coach 
           ) 
          [FareClass] => Array 
           (
            [0] => SimpleXMLElement Object 
             (
              [@attributes] => Array 
               (
                [Code] => Y 
                [Name] => ECONOMY 
                [NumOfAvailableSeats] => 9 
               ) 
             ) 
           ) 
         ) 
        [1] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (
            [Code] => B 
            [Name] => Business 
           ) 
          [FareClass] => Array 
           (
            [0] => SimpleXMLElement Object 
             (
              [@attributes] => Array 
               (
                [Code] => C 
                [Name] => BUSINESS 
                [NumOfAvailableSeats] => 4 
               ) 
             ) 
           ) 
         ) 
       ) 
      ) 
    ) 
) 
) 

我必須解析上面的xml響應。請幫助我。通過php解析xml響應

我正在使用下面的代碼,但它使我錯誤代碼,並沒有獲得變量中的所有值。

foreach ($data->FlightOption as $FlightOption) { 
    printf(
     $FlightOption["Flight"]->ArrivalDateTime 
     /*$FlightOption->FareClass, 
     $FlightOption->Airline*/ 
    ); 
} 

回答

1

假設你的XML是這樣的:

<?xml version="1.0"?> 
<data> 
    <FlightOption> 
     <Flight ArrivalDateTime="2014-07-05T15:00:00.000" DepartureDateTime="2014-07-05T13:00:00.000" EquipmentCode="321" FlightNumber="677"> 
      <Airline AirlineCode="AI" IATACode="AI" ICAOCode="AIC" Name="Air India" /> 
      <ArrivalAirport AirportCode="DEL" IATACode="DEL" ICAOCode="VIDP" Name="Indira Gandhi International Airport" /> 
      <Cabin Code="C" Name="Coach"> 
       <FareClass Code="Y" Name="ECONOMY" NumOfAvailableSeats="9" /> 
      </Cabin> 
      <Cabin Code="B" Name="Business"> 
       <FareClass Code="C" Name="BUSINESS" NumOfAvailableSeats="4" /> 
      </Cabin> 
     </Flight> 
    </FlightOption> 
</data> 

它看起來像你的問題是,目前在各FlightOption元素多FareClass元素。這將需要在第一個循環內嵌套循環。下面的代碼:

<?php 

$data = simplexml_load_file("data.xml"); 

foreach($data->FlightOption as $FlightOption) 
{ 
    $arrivalDateTime = $FlightOption->Flight["ArrivalDateTime"]; 
    $airline = $FlightOption->Flight->Airline["Name"]; 

    foreach($FlightOption->Flight->Cabin as $Cabin) 
    { 
     print($arrivalDateTime); 
     print("/"); 
     print($Cabin->FareClass["Name"]); 
     print("/"); 
     print($airline); 

     print("<br/><br/>"); 
    } 

    print("<br/><br/>"); 
} 

?> 

將產生以下的輸出:

2014-07-05T15:00:00.000/ECONOMY/Air India 

2014-07-05T15:00:00.000/BUSINESS/Air India 

注:我在這裏做一些假設,因爲我沒有足夠的代表處發表評論,要求獲得更多信息。如果這不能回答你的問題,請提供一些更多的信息(比如你所得到的錯誤),並且我會試着相應地編輯我的答案。