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

Are you returning only 1 object at a time? If so, why not return the found FlightType right inside your if (on line 28) instead of return it outside the loop? Because you declare and initiate the object outside the loop, not sure what would happen if you try to update the value inside the loop...

If you want to return as a list, then you need the return type of the method to be a collection as well. Then do NOT declare newJourney outside the loop but rather do it inside the loop (i.e. move line 18 to be inside the if on line 28) but have a collection variable outside the loop. Add each found FlightType to the List inside the loop. Replace the return null to return the collection variable you initiated. On the other side (client), check if the return value is empty (not check for null).

Edited 2 Years Ago by Taywin

Some advanced techniques that I highly recomend are such libraries as XPath, XQuery, even XQL and LUX if you need them. However, for your situation, XPath should work perfectly. You'll need to read a bit about it here: http://www.w3schools.com/xpath/ , and I think it is the simplest answer to your problem. Give it a try, I'm sure we'd all like to hear your progress.

This article has been dead for over six months. Start a new discussion instead.