Hi I wonder if anyone can help please. I am stuck after unmarsheling complex type from JAXB in my web service operation.
I can successfully unmarshal and loop through it which is fine however i am performing a search and want to return all the elements that match the search.
Eg. I have a root element called BookingFlights with an arrayList of FlightType FlightType which are the elements of the xml. Can anyone suggest how to return the values that match the search please? Ps i can display them on the output window but when i try to save it onto another list i keep getting Null exception.
Thank you.
the webservice is:
@WebService(serviceName = "FlightsWS")
public class FlightsWS {
//method to book a seat
@WebMethod(operationName = "searchJourney")
public FlightType searchJourney(String origin, String destination) {
//declare the list of flights
FlightBooking myFlights = new FlightBooking();
//unmarshal
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(myFlights.getClass().getPackage().getName());
javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
myFlights = (FlightBooking) unmarshaller.unmarshal(new java.io.File("flights.xml")); //NOI18N
//get the list of flights in the xml
List<FlightType> flightJourney = myFlights.getFlightTrips();
FlightType newJourney = new FlightType();
//iterates through the list of flights from XML file
Iterator itr = flightJourney.iterator();
while (itr.hasNext()) {
newJourney = (FlightType) itr.next();
//checks if city matches
if (newJourney.getOriginCity().isEmpty()) {
System.out.println("Please enter origin city");
} else {
if (origin.equalsIgnoreCase(newJourney.getOriginCity())
|| destination.equalsIgnoreCase(newJourney.getDestinationCity())) {
//get newJourney and put into a list of flights returned
System.out.println(newJourney.getFlightID());
System.out.println(newJourney.getOriginCity());
System.out.println(newJourney.getDestinationCity());
System.out.println(newJourney.getAirline());
//This is where i have to create a list and add newJpurney into it
//This is where i have to create a list and add newJpurney into it
//This is where i have to create a list and add newJpurney into it
}//ends if statement found city
}
}//ends while loop
return newJourney;//this only return one match and not all of them
} catch (javax.xml.bind.JAXBException ex) {
// XXXTODO Handle exception
java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
}
return null;
}//ends public flight search journey method
}//ends the class