Question

When I send a request to web service the following response will be received. I am using the following code to unmarshall the response XML to an object, but it seems nothing will be put into the object.

Server's response

    <?xml version="1.0" encoding="utf-8"?>
    <AvailabilityResponse DateTime="2014-02-18 08:32:04" DurationSecs="1.32" xmlns="example.com">
        <Success></Success>
        <Results ResultCount="9">
            <Result>
                <Outbound>
                    <FlightSegment>
                        <Departure>
                            <Airport CodeContext="IATA">DUS</Airport>
                            <DateTime>2014-12-10T09:55:00</DateTime>
                        </Departure>
                        <Arrival>
                            <Airport CodeContext="IATA">ZRH</Airport>
                            <DateTime>2014-12-10T11:10:00</DateTime>
                        </Arrival>
                        <FlightNumber CodeContext="IATA">LX1017</FlightNumber>
                    </FlightSegment>
                </Outbound>
                <Inbound>
                    <FlightSegment>
                        <Departure>
                            <Airport CodeContext="IATA">ZRH</Airport>
                            <DateTime>2014-12-15T07:25:00</DateTime>
                        </Departure>
                        <Arrival>
                            <Airport CodeContext="IATA">DUS</Airport>
                            <DateTime>2014-12-15T08:50:00</DateTime>
                        </Arrival>
                        <FlightNumber CodeContext="IATA">LX1016</FlightNumber>
                    </FlightSegment>
                </Inbound>
                <FareInfos CurrencyCode="EUR">
                    <FareReference>Y,Y</FareReference>
                    <BaseAmount>30</BaseAmount>
                    <TaxAmount>58</TaxAmount>
                    <TotalFare>126</TotalFare>
                </FareInfos>
                <Url>http://myexample.com</Url>
            </Result>
            <Result>....</Result>
            <Result>....</Result>
            <Result>....</Result>
            <Result>....</Result>
            <Result>....</Result>
            <Result>....</Result>
            <Result>....</Result>
            <Result>....</Result>
     </Results>
    </AvailabilityResponse>

Code

    String queryString = "http://example.com";

            try {
                URL page = new URL(queryString);

                HttpURLConnection conn = (HttpURLConnection) page.openConnection();
                conn.connect();

                System.err.println("Results ");
                this.results = (AvailabilityResponse) JAXB.unmarshal(conn.getInputStream(), 
                                                                     AvailabilityResponse.class);
                System.err.println("Output:" + this.results.getResults().getResult().size());

Result in console (as there are 9 results in the Results tag, this number should be 9

Results 
Output:0

AvailabilityResponse

@XmlRootElement(name = "AvailabilityResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class AvailabilityResponse {

    @XmlElement(name = "Results")
    private Results results = null;
    @XmlElement (name = "Success")
    private String success;

    public AvailabilityResponse() {
        this.results = new Results();
    }

    getters & setters go here

}

Results

@XmlRootElement(name = "Results")
@XmlAccessorType(XmlAccessType.FIELD)
public class Results {

    @XmlElement(name = "Result")
    private List<Result> result = null;

    public Results() {
        this.result = new ArrayList();
    }

    getters & setters go here

}

Result

@XmlRootElement(name = "Result")
@XmlAccessorType(XmlAccessType.FIELD)
public class Result {

    @XmlElement(name = "Outbound")
    private Outbound outbound = null;
    @XmlElement(name = "Inbound")
    private Inbound inbound = null;
    @XmlElement(name = "FareInfos")
    private FareInfos fareInfos = null;
    @XmlElement (name = "Url")
    private String Url;

    public Result() {
        this.outbound = new Outbound();
        this.inbound = new Inbound();
        this.fareInfos = new FareInfos();
    }

    getters & setters go here

}

OutBound

@XmlRootElement(name = "Outbound")
@XmlAccessorType(XmlAccessType.FIELD)
public class Outbound {

    @XmlElement(name = "FlightSegment")
    private FlightSegment flightSegment = null;

    public Outbound() {
        this.flightSegment = new FlightSegment();
    }

    getter & setter go here

}

InBound

@XmlRootElement(name = "Inbound")
@XmlAccessorType(XmlAccessType.FIELD)
public class Inbound {

    @XmlElement(name = "FlightSegment")
    private FlightSegment flightSegment = null;

    public Inbound() {
        this.flightSegment = new FlightSegment();
    }

    getter & setter go here
}

FlightSegment

@XmlRootElement(name = "FlightSegment")
@XmlAccessorType(XmlAccessType.FIELD)
public class FlightSegment {

    @XmlElement(name = "Departure")
    private Departure departure = null;
    @XmlElement(name = "Arrival")
    private Arrival arrival = null;
    @XmlElement(name = "FlightNumber")
    private String flightNumber;

    public FlightSegment() {
        this.departure = new Departure();
        this.arrival = new Arrival();
    }

    getters & setters go here

}

Departure

@XmlRootElement (name = "Departure")
@XmlAccessorType (XmlAccessType.FIELD)
public class Departure {

    @XmlElement (name = "Airport")
    private String Airport;
    @XmlElement (name = "DateTime")
    private String DateTime;

    getters & setters go here

}

Arrival

@XmlRootElement(name = "Arrival")
@XmlAccessorType(XmlAccessType.FIELD)
public class Arrival {

    @XmlElement(name = "Airport")
    private String Airport;
    @XmlElement(name = "DateTime")
    private String DateTime;

    getters & setters go here

}

FareInfos

@XmlRootElement (name = "FareInfos")
@XmlAccessorType(XmlAccessType.FIELD)
public class FareInfos {

    @XmlElement (name = "FareReference")
    private String fareReference;
    @XmlElement (name = "BaseAmount")
    private String baseAmount;
    @XmlElement (name ="TaxAmount")
    private String taxAmount;
    @XmlElement (name = "TotalFare" )
    private String totalFare;

    getters & setters go here

}
Was it helpful?

Solution

Since your XML document is namespace qualified you will need to use the package level @XmlSchema annotation to map the namespace qualification.

@XmlSchema( 
    namespace = "example.com", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top