通过php解析xml响应

时间:2014-07-01 16:05:49

标签: php xml parsing

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响应。请帮助我。

我正在使用下面的代码,但它会给我错误代码并且不会在变量中获取所有值。

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

1 个答案:

答案 0 :(得分: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>

看起来您的问题是每个FareClass元素中有多个FlightOption元素。这将需要在第一个循环内部嵌套循环。以下代码:

<?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

注意:我在这里做了一些假设,因为我没有足够的代表来发表评论,要求提供更多信息。如果这不能回答您的问题,请提供更多信息(例如您收到的错误),我会尝试相应地修改我的答案。