PHP访问复杂的xml元素

时间:2015-01-04 16:44:13

标签: php xml soap

我有以下XML文件 我想找回一些价值。 我尝试使用x-path的问题,但它没有用。

这是xml文件:

<soap-env:Envelope>
    <soap-env:Header></soap-env:Header>
    <soap-env:Body>
        <OTA_AirAvailRS Version="2.2.0">
            <stl:ApplicationResults status="Complete"></stl:ApplicationResults>
            <OriginDestinationOptions OriginTimeZone="Z2">
                <OriginDestinationOption RPH="1">
                    <FlightSegment ArrivalDateTime="01-05T01:05" DOT_Ind="N" DepartureDateTime="01-04T22:00" FlightNumber="642" RPH="1" SmokingAllowed="false" StopQuantity="0" eTicket="true">
                        <BookingClassAvail Availability="9" RPH="1" ResBookDesigCode="J" />
                        <BookingClassAvail Availability="0" RPH="2" ResBookDesigCode="Z" />
                        <BookingClassAvail Availability="5" RPH="3" ResBookDesigCode="C" />
                        <BookingClassAvail Availability="4" RPH="4" ResBookDesigCode="D" />
                        <BookingClassAvail Availability="0" RPH="5" ResBookDesigCode="I" />
                        <BookingClassAvail Availability="9" RPH="6" ResBookDesigCode="Y" />
                        <BookingClassAvail Availability="9" RPH="7" ResBookDesigCode="B" />
                        <BookingClassAvail Availability="0" RPH="8" ResBookDesigCode="P" />
                        <BookingClassAvail Availability="9" RPH="9" ResBookDesigCode="H" />
                        <BookingClassAvail Availability="0" RPH="10" ResBookDesigCode="W" />
                        <BookingClassAvail Availability="9" RPH="11" ResBookDesigCode="K" />
                        <BookingClassAvail Availability="0" RPH="12" ResBookDesigCode="R" />
                        <BookingClassAvail Availability="9" RPH="13" ResBookDesigCode="M" />
                        <BookingClassAvail Availability="9" RPH="14" ResBookDesigCode="L" />
                        <BookingClassAvail Availability="9" RPH="15" ResBookDesigCode="V" />
                        <BookingClassAvail Availability="9" RPH="16" ResBookDesigCode="S" />
                        <BookingClassAvail Availability="9" RPH="17" ResBookDesigCode="N" />
                        <BookingClassAvail Availability="9" RPH="18" ResBookDesigCode="Q" />
                        <BookingClassAvail Availability="9" RPH="19" ResBookDesigCode="O" />
                        <DaysOfOperation>
                            <OperationSchedule>
                                <OperationTimes>
                                    <OperationTime Fri="true" Mon="false" Sat="true" Sun="true" Thur="false" Tue="false" Weds="true" />
                                </OperationTimes>
                            </OperationSchedule>
                        </DaysOfOperation>
                        <DestinationLocation LocationCode="KWI" />
                        <Equipment AirEquipType="E95" />
                        <FlightDetails Canceled="false" Charter="false" />
                        <MarketingAirline Code="RJ" FlightNumber="642" ParticipationLevel="DCA" />
                        <Meal MealCode="D" />
                        <OriginLocation LocationCode="AMM" />
                    </FlightSegment>
                    <FlightSegment ArrivalDateTime="01-05T06:10" ConnectionInd="Y" DOT_Ind="N" DepartureDateTime="01-05T03:25" FlightNumber="859" RPH="2" SmokingAllowed="false" StopQuantity="0" eTicket="true"></FlightSegment>
                    <FlightSegment ArrivalDateTime="01-05T12:30" ConnectionInd="Y" DOT_Ind="N" DepartureDateTime="01-05T10:20" FlightNumber="501" RPH="3" SmokingAllowed="false" StopQuantity="0" eTicket="true"></FlightSegment>
                </OriginDestinationOption>
                <OriginDestinationOption RPH="2"></OriginDestinationOption>
                <OriginDestinationOption RPH="3"></OriginDestinationOption>
                <OriginDestinationOption RPH="4"></OriginDestinationOption>
            </OriginDestinationOptions>
        </OTA_AirAvailRS>
    </soap-env:Body>
</soap-env:Envelope>

我想从&#39; Flight Segment&#39;中检索价值。元件

1 个答案:

答案 0 :(得分:0)

Xpath应该可以工作:

    $soap = new DOMDocument();
    $soap -> preserveWhiteSpace = FALSE;        
    $soap ->load([source url);

    $query = "//FlightSegment";
    $xPathObject = new DOMXPath($soap);
    $searchQuery = $xPathObject->query($query);

$query = "//FlightSegment"//从文档中的任何位置选择节点。 FlightSegmentnode,应使用xpath从文档中检索。

变量FlightSegment中将存储三个$searchQuery个节点。您可以使用$searchQuery->item(#)访问它们,其中#是项目索引。在这种情况下为0,1或2。