PHP 访问复杂的 xml 元素
PHP access complex xml elements
我有以下 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>
我想从 'Flight Segment' 元素中检索值
Xpath 应该可以工作:
$soap = new DOMDocument();
$soap -> preserveWhiteSpace = FALSE;
$soap ->load([source url);
$query = "//FlightSegment";
$xPathObject = new DOMXPath($soap);
$searchQuery = $xPathObject->query($query);
$query = "//FlightSegment"
: //
从文档中的任何位置选择节点。 FlightSegment
是一个 node
,应使用 xpath
.
从文档中检索
变量$searchQuery
中将存储三个FlightSegment
个节点。您可以使用 $searchQuery->item(#)
访问它们,其中 #
是项目索引。在本例中为 0、1 或 2。
我有以下 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>
我想从 'Flight Segment' 元素中检索值
Xpath 应该可以工作:
$soap = new DOMDocument();
$soap -> preserveWhiteSpace = FALSE;
$soap ->load([source url);
$query = "//FlightSegment";
$xPathObject = new DOMXPath($soap);
$searchQuery = $xPathObject->query($query);
$query = "//FlightSegment"
: //
从文档中的任何位置选择节点。 FlightSegment
是一个 node
,应使用 xpath
.
变量$searchQuery
中将存储三个FlightSegment
个节点。您可以使用 $searchQuery->item(#)
访问它们,其中 #
是项目索引。在本例中为 0、1 或 2。