2014-02-18 49 views
1

當我向Web服務發送請求時,將收到以下響應。我正在使用下面的代碼將響應XML解組到對象中,但似乎沒有任何東西會放入對象中。無法將(具有名稱空間的XML)輸入流解組到對象中

服務器的響應

<?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> 

代碼

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()); 

結果在控制檯(因爲有9所導致結果的標籤,這個數字應該是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 

} 

結果

@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 

} 

結果

@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 

} 

外向

@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 

} 

入站

@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 

} 

出發

@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 

} 

到達

@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 

} 

回答

2

由於XML文檔的名稱空間限定,你將需要使用封裝級@XmlSchema註釋映射命名空間的資格。

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

import javax.xml.bind.annotation.XmlNsForm; 
import javax.xml.bind.annotation.XmlSchema; 
+0

你對子級請爲我提供我從來沒有使用 – J888

+0

@ J888在此之前註釋的例子 - 你嘗試使用Google呢? :-) –

+0

@ J888 - 我已經添加了一個例子。 –

相關問題