给出以下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>
当我需要基于具有MultiLegOutboundFlight =“ true”属性的子元素Flight提取 all 所有ItineraryIds列表时,
如何编写XmlPath来实现此目的?我正在使用RestAssurred并正在尝试,但是发现很难在线查找XmlPath语法的资源,因此摸不着头脑:
如果我执行以下操作:
public List<String> getMultiLegFlightItineraryIdList() {
XmlPath xmlPath = new XmlPath(response.asString()); if(xmlPath.getNode("Holiday.Flight").getAttribute("MultiLegOutboundFlight").equals("true")) {
String itineraryId = xmlPath.getNode("Holiday").getAttribute("ItineraryId");
System.out.println(itineraryId);
}
return itineraryIDList;
}
然后我收到以下错误:
[Fatal Error] :2:802: The markup in the document following the root element must be well-formed.
ERROR: 'The markup in the document following the root element must be well-formed.'
java.lang.IllegalArgumentException: Failed to convert XML to Java Object. If
you're trying convert to a list then use the getList method instead.
最初我有以下内容:
allMultiLegPackages = response.xmlPath().getList("Holiday.Flight.findAll{it.@MultiLegOutboundFlight=='true'}//parent::Holiday");
这将返回条件为true的所有Holidays的列表,但是如何获取ItineraryID的属性作为列表?
答案 0 :(得分:0)
这是您的父节点的Xpath
//flight[@multilegoutboundflight='true']//parent::holiday
您需要额外添加的是将检索内部属性的方法。就像通过getAttribute('ItineraryId')
答案 1 :(得分:0)
下面的代码段可能会对您有所帮助。
String itineraryId = "";
XmlPath xmlpath = new XmlPath(responseXML);
if(xmlpath.getNode("Holiday").getNode("Flight").getAttribute("MultiLegOutboundFlight").equals("true")) {
itineraryId = xmlpath.getNode("Holiday").getAttribute("ItineraryId");
System.out.println("Itinerary ID ::: "+itineraryId);
}
导入:
import com.jayway.restassured.path.xml.XmlPath;