XmlPath基于条件子属性将父属性列表选择为字符串列表

时间:2018-07-01 16:02:13

标签: java rest-assured

给出以下XML:

<Holiday ItineraryId="123456789" Country="ES" NumberOfNights="7" LeadInPricePerPassenger="447.3" FlightOnly="true" OneWayOnly="false" Currency="GBP" BookingChannel="WEB" PassengerIdRequired="false" PassengerIdMandatory="false" NationalityRequired="false" NationalityMandatory="false">
   <Flight OriginAirport="LGW" DestinationAirport="ALC" MultiLegOutboundFlight="true">
      <OutboundSubSegments DeparturePoint="LGW" DepartureDate="20180802" DepartureTime="0920" ArrivalPoint="OSL" ArrivalDate="20180802" ArrivalTime="1225" OperatingCarrier="DY" OperatedBy="Super Air Shuttle" FlightNumber="HY637" OriginAirportName="London Gatwick" DestinationAirportName="Oslo" />
   </Flight>
</Holiday>

<Holiday ItineraryId="9283873737" Country="ES" NumberOfNights="7" LeadInPricePerPassenger="447.3" FlightOnly="true" OneWayOnly="false" Currency="GBP" BookingChannel="WEB" PassengerIdRequired="false" PassengerIdMandatory="false" NationalityRequired="false" NationalityMandatory="false">
   <Flight OriginAirport="LGW" DestinationAirport="ALC" MultiLegOutboundFlight="true">
      <OutboundSubSegments DeparturePoint="LGW" DepartureDate="20180802" DepartureTime="0920" ArrivalPoint="OSL" ArrivalDate="20180802" ArrivalTime="1225" OperatingCarrier="DY" OperatedBy="Super Air Shuttle" FlightNumber="HY637" OriginAirportName="London Gatwick" DestinationAirportName="Oslo" />
   </Flight>
</Holiday>

<Holiday ItineraryId="894847463" Country="ES" NumberOfNights="7" LeadInPricePerPassenger="447.3" FlightOnly="true" OneWayOnly="false" Currency="GBP" BookingChannel="WEB" PassengerIdRequired="false" PassengerIdMandatory="false" NationalityRequired="false" NationalityMandatory="false">
   <Flight OriginAirport="LGW" DestinationAirport="ALC" MultiLegOutboundFlight="true">
      <OutboundSubSegments DeparturePoint="LGW" DepartureDate="20180802" DepartureTime="0920" ArrivalPoint="OSL" ArrivalDate="20180802" ArrivalTime="1225" OperatingCarrier="DY" OperatedBy="Super Air Shuttle" FlightNumber="HY637" OriginAirportName="London Gatwick" DestinationAirportName="Oslo" />
  </Flight>
</Holiday>

<Holiday ItineraryId="894847463" Country="ES" NumberOfNights="7" LeadInPricePerPassenger="447.3" FlightOnly="true" OneWayOnly="false" Currency="GBP" BookingChannel="WEB" PassengerIdRequired="false" PassengerIdMandatory="false" NationalityRequired="false" NationalityMandatory="false">
   <Flight OriginAirport="LGW" DestinationAirport="ALC" MultiLegOutboundFlight="false">
      <OutboundSubSegments DeparturePoint="LGW" DepartureDate="20180802" DepartureTime="0920" ArrivalPoint="OSL" ArrivalDate="20180802" ArrivalTime="1225" OperatingCarrier="DY" OperatedBy="Super Air Shuttle" FlightNumber="HY637" OriginAirportName="London Gatwick" DestinationAirportName="Oslo" />
  </Flight>
</Holiday>

如何在符合条件MultiLegOutboundFlight =“ true”的字符串列表中选择itineraryID的 all

我尝试过:

allMultiLegPackages = response.xmlPath().getList("Holiday.Flight{it.@MultiLegOutboundFlight=='true'}//parent::Holiday");

...,这将成功返回满足条件的节点列表。但是,如何获取ItineraryId的属性列表?

2 个答案:

答案 0 :(得分:0)

假设您的XmlPath表达式有效,我想您只需要在其末尾添加“ / @ ItineraryId”即可。

allMultiLegPackages =
response.xmlPath().getList("Holiday.Flight{it.@MultiLegOutboundFlight=='true'}//parent::Holiday/@ItineraryId");

答案 1 :(得分:0)

希望此代码段对您有所帮助。

XmlPath xmlpath = new XmlPath(responseXML);
List<Node> holidayNodes = xmlpath.getList("Holidays.Holiday.findAll{it.Flight.@MultiLegOutboundFlight=='true'}");
List<String> itineraryList = new ArrayList<>();
for(Node node: holidayNodes){
itineraryList.add(node.getAttribute("ItineraryId"));
}
System.out.println(itineraryList);

您的XML响应似乎有多个根节点Holiday。我认为我们应该只有一个根节点。在一个根节点Holidays中声明所有Holiday节点