Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析的SOAP响应与接收到的响应不同_Java_Soap_Jaxb_Marshalling_Unmarshalling - Fatal编程技术网

Java 解析的SOAP响应与接收到的响应不同

Java 解析的SOAP响应与接收到的响应不同,java,soap,jaxb,marshalling,unmarshalling,Java,Soap,Jaxb,Marshalling,Unmarshalling,我正在尝试解析SOAP响应。我可以使用以下行显示完整的响应 System.out.println("Response: " + out.toString()); 但是,当我解析响应并对解析后的响应进行marshall处理时,它显示了一个错误的响应 package info.java @XmlSchema( namespace = "http://v3.hotel.wsapi.ean.com/", elementFormDefault = XmlNsForm.QUALIF

我正在尝试解析SOAP响应。我可以使用以下行显示完整的响应

  System.out.println("Response: " + out.toString()); 
但是,当我解析响应并对解析后的响应进行marshall处理时,它显示了一个错误的响应

package info.java

@XmlSchema( 
    namespace = "http://v3.hotel.wsapi.ean.com/",
    elementFormDefault = XmlNsForm.QUALIFIED) 
package com.ean;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
我的代码

@XmlRootElement(name="getListResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class GetListResponse {
    @XmlElement(name="HotelListResponse")
    private HotelListResponse hotelListResponse;

    public GetListResponse() {
        this.hotelListResponse = new HotelListResponse();
    }
    …getter and setter
}

@XmlRootElement(name ="HotelListResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class HotelListResponse {
    @XmlElement(name = "customerSessionId")
    String customerSessionId;
    @XmlElement(name = "numberOfRoomsRequested")
    int numberOfRoomsRequested;
    @XmlElement(name = "moreResultsAvailable")
    boolean moreResultsAvailable;
    @XmlElement(name = "cacheKey")
    String cacheKey;
    @XmlElement(name="cacheLocation")
    String cachLocation;
    @XmlElement(name = "HotelList")
    HotelList hotelList;

    public HotelListResponse() {
        this.hotelList = new HotelList();
    }
    … getters and setters…
}

@XmlRootElement(name ="HotelList")
@XmlAccessorType(XmlAccessType.FIELD)
public class HotelList {
    @XmlAttribute(name = "size")
    int size;
    @XmlAttribute(name = "activePropertyCount")
    int activePropertyCount;
    @XmlElement(name = "HotelSummary")
    List <HotelSummary> hotelSummaries;

    public HotelList() {
        this.hotelSummaries = new ArrayList();
    }
    … getters and setters…
}

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

    @XmlAttribute(name = "order")
    int order;
    @XmlElement(name = "hotelId")
    int hotelId;
    @XmlElement(name = "name")
    String name;
    @XmlElement(name = "address1")
    String address1;
    @XmlElement(name = "city")
    String city;
    @XmlElement(name = "stateProvinceCode")
    String stateProvinceCode;
    @XmlElement(name = "postalCode")
    int postalCode;
    @XmlElement(name = "countryCode")
    String countryCode;
    @XmlElement(name = "airportCode")
    String airportCode;
    @XmlElement(name = "supplierType")
    String supplierType;
    @XmlElement(name = "propertyCategory")
    int propertyCategory;
    @XmlElement(name = "hotelRating")
    float hotelRating;
    @XmlElement(name = "confidenceRating")
    int confidenceRating;
    @XmlElement(name = "amenityMask")
    int amenityMask;
    @XmlElement(name = "tripAdvisorRating")
    float tripAdvisorRating;
    @XmlElement(name = "locationDescription")
    String locationDescription;
    @XmlElement(name = "shortDescription")
    String shortDescriptionl; //change amp to &
    @XmlElement(name = "highRate")
    String highRate;
    @XmlElement(name = "lowRate")
    float lowRate;
    @XmlElement(name = "rateCurrencyCode")
    String rateCurrencyCode;
    @XmlElement(name = "latitude")
    float latitude;
    @XmlElement(name = "longitude")
    float longitude;
    @XmlElement(name = "proximityDistance")
    float proximityDistance;
    @XmlElement(name = "proximityUnit")
    String proximityUnit;
    @XmlElement(name = "hotelInDestination")
    boolean hotelInDestination;
    @XmlElement(name = "thumbNailUrl")
    String thumbNailUrl;
    @XmlElement(name = "deepLink")
    String deepLink; 
    @XmlElement(name = "RoomRateDetailsList")
    RoomRateDetailsList roomRateDetailsList;

    public HotelSummary() {
        this.roomRateDetailsList = new RoomRateDetailsList();
    } 
    … getters and setters…
}

@XmlRootElement(name = "RoomRateDetailsList")
@XmlAccessorType(XmlAccessType.FIELD)
public class RoomRateDetailsList {
    @XmlElement(name="RoomRateDetails")
    List<RoomRateDetails> roomRateDetails;

    public RoomRateDetailsList() {
        this.roomRateDetails = new ArrayList();
    }
    .. getter and setter..
}
@XmlRootElement(name = "RoomRateDetails")
@XmlAccessorType(XmlAccessType.FIELD)
public class RoomRateDetails {
    @XmlElement(name="roomTypeCode")
    int roomTypeCode;
    @XmlElement(name="rateCode")
    int rateCode;
    @XmlElement(name="maxRoomOccupancy")
    int maxRoomOccupancy;
    @XmlElement(name="quotedRoomOccupancy")
    int quotedRoomOccupancy;
    @XmlElement(name="minGuestAge")
    int minGuestAge;
    @XmlElement(name="roomDescription")
    String roomDescription;
    @XmlElement(name="promoId")
    int promoId;
    @XmlElement(name="promoDescription")
    String promoDescription;
    @XmlElement(name="currentAllotment")
    int currentAllotment;
    @XmlElement(name="propertyAvailable")
    boolean propertyAvailable;
    @XmlElement(name="propertyRestricted")
    boolean propertyRestricted;
    @XmlElement(name="expediaPropertyId")
    int expediaPropertyId;
    @XmlElement(name="rateKey")
    String rateKey;
    @XmlElement(name="RateInfo")
    RateInfo rateInfo;
    @XmlElement(name="ValueAdds")
    ValueAdds valueAdds;
    public RoomRateDetails() {
        this.rateInfo = new RateInfo();
        this.valueAdds = new ValueAdds();
    }
    … getters and setters…
}
@XmlRootElement(name = "RoomInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class RateInfo {
    @XmlAttribute(name="priceBreakdown")
     boolean priceBreakdown;
    @XmlAttribute(name="promo")
     boolean promo;
    @XmlAttribute(name="rateChange")
     boolean rateChange;
    @XmlElement(name="ChargeableRateInfo")
     ChargeableRateInfo chargeableRateInfo;

    public RateInfo() {
        this.chargeableRateInfo = new ChargeableRateInfo();
    }
    .. getters and setters…
}
@XmlRootElement(name = "ChargeableRateInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class ChargeableRateInfo {
    @XmlAttribute(name="averageBaseRate")
    float averageBaseRate;
    @XmlAttribute(name="averageRate")
    float averageRate;
    @XmlAttribute(name="commissionableUsdTotal")
    float commissionableUsdTotal;
    @XmlAttribute(name="currencyCode")
    String currencyCode;
    @XmlAttribute(name="maxNightlyRate")
    float maxNightlyRate;
    @XmlAttribute(name="nightlyRateTotal")
    float nightlyRateTotal;
    @XmlAttribute(name="total")
    float total;
    @XmlElement(name="NightlyRatesPerRoom")
    NightlyRatesPerRoom nightlyRatesPerRoom;
    public ChargeableRateInfo() {
        this.nightlyRatesPerRoom = new NightlyRatesPerRoom();
    }
    … getters and setters…
}
@XmlRootElement(name = "NightlyRatesPerRoom")
@XmlAccessorType(XmlAccessType.FIELD)
public class NightlyRatesPerRoom {
    @XmlAttribute(name="size")
     int size;
    @XmlElement(name="NightlyRate")
     NightlyRate nightlyRate;

    public NightlyRatesPerRoom() {
        this.nightlyRate = new NightlyRate();
    }
    … getters and setters…
}
@XmlRootElement(name = "NightlyRate")
@XmlAccessorType(XmlAccessType.FIELD)
public class NightlyRate {
    @XmlAttribute(name="baseRate")
   float baseRate;
    @XmlAttribute(name="rate")
   float rate;
    @XmlAttribute(name="promo")
   float promo;

    public NightlyRate() {
    }
    … getters and setters…
}

@XmlRootElement(name = "ValueAdds")
@XmlAccessorType(XmlAccessType.FIELD)
public class ValueAdds {
    @XmlAttribute(name="size")
    private int size;
    @XmlElement(name="ValueAdd")
    private List<ValueAdd> valueAdd;

    public ValueAdds() {
        this.valueAdd = new ArrayList();
    }
       … getters and setters…

    }

@XmlRootElement(name = "ValueAdd")
@XmlAccessorType(XmlAccessType.FIELD)
public class ValueAdd {
    @XmlAttribute(name="id")
    private int id;
    @XmlElement(name="description")
    private String description;

    public ValueAdd() {
    }
    … getters and setters…

}
try {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection connection = soapConnectionFactory.createConnection();
            SOAPFactory soapFactory = SOAPFactory.newInstance();

            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage();

            SOAPHeader header = message.getSOAPHeader();
            header.detachNode();

            SOAPBody body = message.getSOAPBody();

            Name bodyName;
            bodyName = soapFactory.createName("getList",
                    "v3", "http://v3.hotel.wsapi.ean.com/");

            SOAPBodyElement getList
                    = body.addBodyElement(bodyName);

            Name childName = soapFactory.createName("HotelListRequest");
            SOAPElement HotelListRequest = getList.addChildElement(childName);

             ……Here, I add child nodes of the request…...

            message.writeTo(System.out); //show message details

            URL endpoint = new URL("http://dev.api.ean.com/ean-services/ws/hotel/v3");
            SOAPMessage response = connection.call(message, endpoint);

            connection.close();

                    SOAPMessage sm = response;
        System.err.println("Response:");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        sm.writeTo(out);

      System.err.println(">>>" + out.toString());

        System.err.println(">>>>>>>>>>>parse:");
        SOAPBody sb = response.getSOAPBody();
        DOMSource source = new DOMSource(sb);
        HotelListResponse results = new HotelListResponse();
        results = (HotelListResponse) JAXB.unmarshal(source, HotelListResponse.class);
        JAXBContext context = JAXBContext.newInstance(HotelListResponse.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        System.err.println("Response *******");
        m.marshal(results, System.out);
        System.err.println("116872:" + results.getHotelList().getHotelSummaries().get(0).getHotelId());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
 Response:
Response:>>><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <ns2:getListResponse xmlns:ns2="http://v3.hotel.wsapi.ean.com/">
        <HotelListResponse>
            <customerSessionId>0ABAAA95-28B2-5914-7952-0331AC9069EA</customerSessionId>
            <numberOfRoomsRequested>1</numberOfRoomsRequested>
            <moreResultsAvailable>true</moreResultsAvailable>
            <cacheKey>-41118b29:146950321ac:-69e1</cacheKey>
            <cacheLocation>10.176.160.143:7300</cacheLocation>
            <HotelList size="25" activePropertyCount="119">
                <HotelSummary order="0">
                    <hotelId>150241</hotelId>
                    <name>Rydges World Square</name>
                    <address1>389 Pitt Street</address1>
                    <city>Sydney</city>
                    <stateProvinceCode>NW</stateProvinceCode>
                    <postalCode>2000</postalCode>
                    <countryCode>AU</countryCode>
                    <airportCode>SYD</airportCode>
                    <supplierType>E</supplierType>
                    <propertyCategory>1</propertyCategory>
                    <hotelRating>4.5</hotelRating>
                    <confidenceRating>52</confidenceRating>
                    <amenityMask>1343491</amenityMask>
                    <tripAdvisorRating>3.5</tripAdvisorRating>
                    <locationDescription>Near Darling Harbour</locationDescription>
                    <shortDescription>&amp;lt;p&amp;gt;&amp;lt;b&amp;gt;Property Location&amp;lt;/b&amp;gt; &amp;lt;br /&amp;gt;With a stay at Rydges World Square, you&amp;apos;ll be centrally located in Sydney, steps from World Square Shopping Centre and minutes from Capitol Theatre. This 4.5-star</shortDescription>
                    <highRate>218.64</highRate>
                    <lowRate>218.64</lowRate>
                    <rateCurrencyCode>USD</rateCurrencyCode>
                    <latitude>-33.8766</latitude>
                    <longitude>151.20752</longitude>
                    <proximityDistance>0.5492472</proximityDistance>
                    <proximityUnit>MI</proximityUnit>
                    <hotelInDestination>true</hotelInDestination>
                    <thumbNailUrl>/hotels/1000000/570000/565000/564969/564969_91_t.jpg</thumbNailUrl>
                    <deepLink>http://travel.ian.com/index.jsp?pageName=hotAvail&amp;amp;cid=55505&amp;amp;hotelID=150241&amp;amp;mode=2&amp;amp;numberOfRooms=1&amp;amp;room-0-adult-total=2&amp;amp;room-0-child-total=0&amp;amp;arrivalMonth=5&amp;amp;arrivalDay=18&amp;amp;departureMonth=5&amp;amp;departureDay=19&amp;amp;showInfo=true&amp;amp;locale=en_US&amp;amp;currencyCode=USD</deepLink>
                    <RoomRateDetailsList>
                        <RoomRateDetails>
                            <roomTypeCode>200156055</roomTypeCode>
                            <rateCode>202768754</rateCode>
                            <maxRoomOccupancy>2</maxRoomOccupancy>
                            <quotedRoomOccupancy>2</quotedRoomOccupancy>
                            <minGuestAge>0</minGuestAge>
                            <roomDescription>Special: Deluxe King - Check-In from 8:30pm</roomDescription>
                            <promoId>201528081</promoId>
                            <promoDescription>Sale! Save 25% on this Stay.</promoDescription>
                            <currentAllotment>10</currentAllotment>
                            <propertyAvailable>true</propertyAvailable>
                            <propertyRestricted>false</propertyRestricted>
                            <expediaPropertyId>564969</expediaPropertyId>
                            <rateKey>0ABAAA85-18B2-9914-6952-0351AC9069DF</rateKey>
                            <RateInfo priceBreakdown="true" promo="true" rateChange="false">
                                <ChargeableRateInfo averageBaseRate="218.64" averageRate="218.64" commissionableUsdTotal="218.64" currencyCode="USD" maxNightlyRate="218.64" nightlyRateTotal="218.64" total="218.64">
                                    <NightlyRatesPerRoom size="1">
                                        <NightlyRate baseRate="218.64" rate="218.64" promo="false"/>
                                    </NightlyRatesPerRoom>
                                </ChargeableRateInfo>
                            </RateInfo>
                            <ValueAdds size="1">
                                <ValueAdd id="2048">
                                    <description>Free Wireless Internet</description>
                                </ValueAdd>
                            </ValueAdds>
                        </RoomRateDetails>
                        <RoomRateDetails>
                            ...
    >>>>>>>>>>>parse:
Response *******
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<HotelListResponse xmlns="http://v3.hotel.wsapi.ean.com/">
    <numberOfRoomsRequested>0</numberOfRoomsRequested>
    <moreResultsAvailable>false</moreResultsAvailable>
    <HotelList size="0" activePropertyCount="0"/>
</HotelListResponse>
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.expedia.Engine.retrieveHotels(Engine.java:135)
at com.hotelsdotcom.App.main(App.java:17)
实际响应

@XmlRootElement(name="getListResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class GetListResponse {
    @XmlElement(name="HotelListResponse")
    private HotelListResponse hotelListResponse;

    public GetListResponse() {
        this.hotelListResponse = new HotelListResponse();
    }
    …getter and setter
}

@XmlRootElement(name ="HotelListResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class HotelListResponse {
    @XmlElement(name = "customerSessionId")
    String customerSessionId;
    @XmlElement(name = "numberOfRoomsRequested")
    int numberOfRoomsRequested;
    @XmlElement(name = "moreResultsAvailable")
    boolean moreResultsAvailable;
    @XmlElement(name = "cacheKey")
    String cacheKey;
    @XmlElement(name="cacheLocation")
    String cachLocation;
    @XmlElement(name = "HotelList")
    HotelList hotelList;

    public HotelListResponse() {
        this.hotelList = new HotelList();
    }
    … getters and setters…
}

@XmlRootElement(name ="HotelList")
@XmlAccessorType(XmlAccessType.FIELD)
public class HotelList {
    @XmlAttribute(name = "size")
    int size;
    @XmlAttribute(name = "activePropertyCount")
    int activePropertyCount;
    @XmlElement(name = "HotelSummary")
    List <HotelSummary> hotelSummaries;

    public HotelList() {
        this.hotelSummaries = new ArrayList();
    }
    … getters and setters…
}

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

    @XmlAttribute(name = "order")
    int order;
    @XmlElement(name = "hotelId")
    int hotelId;
    @XmlElement(name = "name")
    String name;
    @XmlElement(name = "address1")
    String address1;
    @XmlElement(name = "city")
    String city;
    @XmlElement(name = "stateProvinceCode")
    String stateProvinceCode;
    @XmlElement(name = "postalCode")
    int postalCode;
    @XmlElement(name = "countryCode")
    String countryCode;
    @XmlElement(name = "airportCode")
    String airportCode;
    @XmlElement(name = "supplierType")
    String supplierType;
    @XmlElement(name = "propertyCategory")
    int propertyCategory;
    @XmlElement(name = "hotelRating")
    float hotelRating;
    @XmlElement(name = "confidenceRating")
    int confidenceRating;
    @XmlElement(name = "amenityMask")
    int amenityMask;
    @XmlElement(name = "tripAdvisorRating")
    float tripAdvisorRating;
    @XmlElement(name = "locationDescription")
    String locationDescription;
    @XmlElement(name = "shortDescription")
    String shortDescriptionl; //change amp to &
    @XmlElement(name = "highRate")
    String highRate;
    @XmlElement(name = "lowRate")
    float lowRate;
    @XmlElement(name = "rateCurrencyCode")
    String rateCurrencyCode;
    @XmlElement(name = "latitude")
    float latitude;
    @XmlElement(name = "longitude")
    float longitude;
    @XmlElement(name = "proximityDistance")
    float proximityDistance;
    @XmlElement(name = "proximityUnit")
    String proximityUnit;
    @XmlElement(name = "hotelInDestination")
    boolean hotelInDestination;
    @XmlElement(name = "thumbNailUrl")
    String thumbNailUrl;
    @XmlElement(name = "deepLink")
    String deepLink; 
    @XmlElement(name = "RoomRateDetailsList")
    RoomRateDetailsList roomRateDetailsList;

    public HotelSummary() {
        this.roomRateDetailsList = new RoomRateDetailsList();
    } 
    … getters and setters…
}

@XmlRootElement(name = "RoomRateDetailsList")
@XmlAccessorType(XmlAccessType.FIELD)
public class RoomRateDetailsList {
    @XmlElement(name="RoomRateDetails")
    List<RoomRateDetails> roomRateDetails;

    public RoomRateDetailsList() {
        this.roomRateDetails = new ArrayList();
    }
    .. getter and setter..
}
@XmlRootElement(name = "RoomRateDetails")
@XmlAccessorType(XmlAccessType.FIELD)
public class RoomRateDetails {
    @XmlElement(name="roomTypeCode")
    int roomTypeCode;
    @XmlElement(name="rateCode")
    int rateCode;
    @XmlElement(name="maxRoomOccupancy")
    int maxRoomOccupancy;
    @XmlElement(name="quotedRoomOccupancy")
    int quotedRoomOccupancy;
    @XmlElement(name="minGuestAge")
    int minGuestAge;
    @XmlElement(name="roomDescription")
    String roomDescription;
    @XmlElement(name="promoId")
    int promoId;
    @XmlElement(name="promoDescription")
    String promoDescription;
    @XmlElement(name="currentAllotment")
    int currentAllotment;
    @XmlElement(name="propertyAvailable")
    boolean propertyAvailable;
    @XmlElement(name="propertyRestricted")
    boolean propertyRestricted;
    @XmlElement(name="expediaPropertyId")
    int expediaPropertyId;
    @XmlElement(name="rateKey")
    String rateKey;
    @XmlElement(name="RateInfo")
    RateInfo rateInfo;
    @XmlElement(name="ValueAdds")
    ValueAdds valueAdds;
    public RoomRateDetails() {
        this.rateInfo = new RateInfo();
        this.valueAdds = new ValueAdds();
    }
    … getters and setters…
}
@XmlRootElement(name = "RoomInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class RateInfo {
    @XmlAttribute(name="priceBreakdown")
     boolean priceBreakdown;
    @XmlAttribute(name="promo")
     boolean promo;
    @XmlAttribute(name="rateChange")
     boolean rateChange;
    @XmlElement(name="ChargeableRateInfo")
     ChargeableRateInfo chargeableRateInfo;

    public RateInfo() {
        this.chargeableRateInfo = new ChargeableRateInfo();
    }
    .. getters and setters…
}
@XmlRootElement(name = "ChargeableRateInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class ChargeableRateInfo {
    @XmlAttribute(name="averageBaseRate")
    float averageBaseRate;
    @XmlAttribute(name="averageRate")
    float averageRate;
    @XmlAttribute(name="commissionableUsdTotal")
    float commissionableUsdTotal;
    @XmlAttribute(name="currencyCode")
    String currencyCode;
    @XmlAttribute(name="maxNightlyRate")
    float maxNightlyRate;
    @XmlAttribute(name="nightlyRateTotal")
    float nightlyRateTotal;
    @XmlAttribute(name="total")
    float total;
    @XmlElement(name="NightlyRatesPerRoom")
    NightlyRatesPerRoom nightlyRatesPerRoom;
    public ChargeableRateInfo() {
        this.nightlyRatesPerRoom = new NightlyRatesPerRoom();
    }
    … getters and setters…
}
@XmlRootElement(name = "NightlyRatesPerRoom")
@XmlAccessorType(XmlAccessType.FIELD)
public class NightlyRatesPerRoom {
    @XmlAttribute(name="size")
     int size;
    @XmlElement(name="NightlyRate")
     NightlyRate nightlyRate;

    public NightlyRatesPerRoom() {
        this.nightlyRate = new NightlyRate();
    }
    … getters and setters…
}
@XmlRootElement(name = "NightlyRate")
@XmlAccessorType(XmlAccessType.FIELD)
public class NightlyRate {
    @XmlAttribute(name="baseRate")
   float baseRate;
    @XmlAttribute(name="rate")
   float rate;
    @XmlAttribute(name="promo")
   float promo;

    public NightlyRate() {
    }
    … getters and setters…
}

@XmlRootElement(name = "ValueAdds")
@XmlAccessorType(XmlAccessType.FIELD)
public class ValueAdds {
    @XmlAttribute(name="size")
    private int size;
    @XmlElement(name="ValueAdd")
    private List<ValueAdd> valueAdd;

    public ValueAdds() {
        this.valueAdd = new ArrayList();
    }
       … getters and setters…

    }

@XmlRootElement(name = "ValueAdd")
@XmlAccessorType(XmlAccessType.FIELD)
public class ValueAdd {
    @XmlAttribute(name="id")
    private int id;
    @XmlElement(name="description")
    private String description;

    public ValueAdd() {
    }
    … getters and setters…

}
try {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection connection = soapConnectionFactory.createConnection();
            SOAPFactory soapFactory = SOAPFactory.newInstance();

            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage();

            SOAPHeader header = message.getSOAPHeader();
            header.detachNode();

            SOAPBody body = message.getSOAPBody();

            Name bodyName;
            bodyName = soapFactory.createName("getList",
                    "v3", "http://v3.hotel.wsapi.ean.com/");

            SOAPBodyElement getList
                    = body.addBodyElement(bodyName);

            Name childName = soapFactory.createName("HotelListRequest");
            SOAPElement HotelListRequest = getList.addChildElement(childName);

             ……Here, I add child nodes of the request…...

            message.writeTo(System.out); //show message details

            URL endpoint = new URL("http://dev.api.ean.com/ean-services/ws/hotel/v3");
            SOAPMessage response = connection.call(message, endpoint);

            connection.close();

                    SOAPMessage sm = response;
        System.err.println("Response:");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        sm.writeTo(out);

      System.err.println(">>>" + out.toString());

        System.err.println(">>>>>>>>>>>parse:");
        SOAPBody sb = response.getSOAPBody();
        DOMSource source = new DOMSource(sb);
        HotelListResponse results = new HotelListResponse();
        results = (HotelListResponse) JAXB.unmarshal(source, HotelListResponse.class);
        JAXBContext context = JAXBContext.newInstance(HotelListResponse.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        System.err.println("Response *******");
        m.marshal(results, System.out);
        System.err.println("116872:" + results.getHotelList().getHotelSummaries().get(0).getHotelId());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
 Response:
Response:>>><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <ns2:getListResponse xmlns:ns2="http://v3.hotel.wsapi.ean.com/">
        <HotelListResponse>
            <customerSessionId>0ABAAA95-28B2-5914-7952-0331AC9069EA</customerSessionId>
            <numberOfRoomsRequested>1</numberOfRoomsRequested>
            <moreResultsAvailable>true</moreResultsAvailable>
            <cacheKey>-41118b29:146950321ac:-69e1</cacheKey>
            <cacheLocation>10.176.160.143:7300</cacheLocation>
            <HotelList size="25" activePropertyCount="119">
                <HotelSummary order="0">
                    <hotelId>150241</hotelId>
                    <name>Rydges World Square</name>
                    <address1>389 Pitt Street</address1>
                    <city>Sydney</city>
                    <stateProvinceCode>NW</stateProvinceCode>
                    <postalCode>2000</postalCode>
                    <countryCode>AU</countryCode>
                    <airportCode>SYD</airportCode>
                    <supplierType>E</supplierType>
                    <propertyCategory>1</propertyCategory>
                    <hotelRating>4.5</hotelRating>
                    <confidenceRating>52</confidenceRating>
                    <amenityMask>1343491</amenityMask>
                    <tripAdvisorRating>3.5</tripAdvisorRating>
                    <locationDescription>Near Darling Harbour</locationDescription>
                    <shortDescription>&amp;lt;p&amp;gt;&amp;lt;b&amp;gt;Property Location&amp;lt;/b&amp;gt; &amp;lt;br /&amp;gt;With a stay at Rydges World Square, you&amp;apos;ll be centrally located in Sydney, steps from World Square Shopping Centre and minutes from Capitol Theatre. This 4.5-star</shortDescription>
                    <highRate>218.64</highRate>
                    <lowRate>218.64</lowRate>
                    <rateCurrencyCode>USD</rateCurrencyCode>
                    <latitude>-33.8766</latitude>
                    <longitude>151.20752</longitude>
                    <proximityDistance>0.5492472</proximityDistance>
                    <proximityUnit>MI</proximityUnit>
                    <hotelInDestination>true</hotelInDestination>
                    <thumbNailUrl>/hotels/1000000/570000/565000/564969/564969_91_t.jpg</thumbNailUrl>
                    <deepLink>http://travel.ian.com/index.jsp?pageName=hotAvail&amp;amp;cid=55505&amp;amp;hotelID=150241&amp;amp;mode=2&amp;amp;numberOfRooms=1&amp;amp;room-0-adult-total=2&amp;amp;room-0-child-total=0&amp;amp;arrivalMonth=5&amp;amp;arrivalDay=18&amp;amp;departureMonth=5&amp;amp;departureDay=19&amp;amp;showInfo=true&amp;amp;locale=en_US&amp;amp;currencyCode=USD</deepLink>
                    <RoomRateDetailsList>
                        <RoomRateDetails>
                            <roomTypeCode>200156055</roomTypeCode>
                            <rateCode>202768754</rateCode>
                            <maxRoomOccupancy>2</maxRoomOccupancy>
                            <quotedRoomOccupancy>2</quotedRoomOccupancy>
                            <minGuestAge>0</minGuestAge>
                            <roomDescription>Special: Deluxe King - Check-In from 8:30pm</roomDescription>
                            <promoId>201528081</promoId>
                            <promoDescription>Sale! Save 25% on this Stay.</promoDescription>
                            <currentAllotment>10</currentAllotment>
                            <propertyAvailable>true</propertyAvailable>
                            <propertyRestricted>false</propertyRestricted>
                            <expediaPropertyId>564969</expediaPropertyId>
                            <rateKey>0ABAAA85-18B2-9914-6952-0351AC9069DF</rateKey>
                            <RateInfo priceBreakdown="true" promo="true" rateChange="false">
                                <ChargeableRateInfo averageBaseRate="218.64" averageRate="218.64" commissionableUsdTotal="218.64" currencyCode="USD" maxNightlyRate="218.64" nightlyRateTotal="218.64" total="218.64">
                                    <NightlyRatesPerRoom size="1">
                                        <NightlyRate baseRate="218.64" rate="218.64" promo="false"/>
                                    </NightlyRatesPerRoom>
                                </ChargeableRateInfo>
                            </RateInfo>
                            <ValueAdds size="1">
                                <ValueAdd id="2048">
                                    <description>Free Wireless Internet</description>
                                </ValueAdd>
                            </ValueAdds>
                        </RoomRateDetails>
                        <RoomRateDetails>
                            ...
    >>>>>>>>>>>parse:
Response *******
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<HotelListResponse xmlns="http://v3.hotel.wsapi.ean.com/">
    <numberOfRoomsRequested>0</numberOfRoomsRequested>
    <moreResultsAvailable>false</moreResultsAvailable>
    <HotelList size="0" activePropertyCount="0"/>
</HotelListResponse>
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.expedia.Engine.retrieveHotels(Engine.java:135)
at com.hotelsdotcom.App.main(App.java:17)
响应:
答复:>>>
0ABAA95-28B2-5914-7952-0331AC9069EA
1.
真的
-41118b29:146950321ac:-69e1
10.176.160.143:7300
150241
里吉斯世界广场酒店
皮特街389号
悉尼
西北
2000
金
西德
E
1.
4.5
52
1343491
3.5
达令港附近
&;书信电报;宝洁;燃气轮机&;书信电报;百安居酒店;燃气轮机;物业位置及;lt/百安居酒店;燃气轮机&;书信电报;br/&;燃气轮机;入住莱吉斯世界广场,您和;载脂蛋白;我们将位于悉尼市中心,距世界广场购物中心几步之遥,距国会大厦剧院几分钟车程。这是4.5星
218.64
218.64
美元
-33.8766
151.20752
0.5492472
医疗保险
真的
/酒店/1000000/570000/565000/564969/564969_91_t.jpg
http://travel.ian.com/index.jsp?pageName=hotAvail&amp;cid=55505&;amp;hotelID=150241&;amp;模式=2&;amp;房间数=1&;amp;房间-0-成人-total=2&;amp;房间-0-儿童-total=0&;amp;到达月=5&;amp;到达日期=18&;amp;出发月份=5&;amp;出发日=19&;amp;showInfo=true&;amp;语言环境=en_US&;amp;货币代码=美元
200156055
202768754
2.
2.
0
特别:豪华大床-晚上8:30入住
201528081
出售!这次住宿可节省25%。
10
真的
假的
564969
0ABAA85-18B2-9914-6952-0351AC9069DF
免费无线互联网
...
>>>>>>>>>>>解析:
回应*******
0
假的
java.lang.IndexOutOfBoundsException:索引:0,大小:0
位于java.util.ArrayList.rangeCheck(ArrayList.java:635)
获取(ArrayList.java:411)
位于com.expedia.Engine.retrieveHotels(Engine.java:135)
位于com.hotelsdotcom.App.main(App.java:17)
如何解决问题 在正确级别解组

当前,您正在解组对应于
soap:Body
元素的XML节点,而需要向下导航到
hotellistrense
元素并解组该节点

您可以尝试以下方法:

DOMSource source = new DOMSource(sb.getFirstChild().getFirstChild());
results = (HotelListResponse) JAXB.unmarshal(source, HotelListResponse.class);
注意您的名称空间限定

在已封送的XML中,所有元素都用
http://v3.hotel.wsapi.ean.com/
,您试图解组的文档没有此命名空间限定,因此您应该从
包信息
类中删除
@XmlSchema
注释

使用
Unmarshaller
而不是
JAXB.unmarshal

而不是执行以下操作:

DOMSource source = new DOMSource(sb.getFirstChild().getFirstChild());
results = (HotelListResponse) JAXB.unmarshal(source, HotelListResponse.class);
我建议:

JAXBContext jc = JAXBContext.newInstance(HotelListResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
results = unmarshaller.unmarshal(source, HotelListResponse.class).getValue();
现在发生了什么 当您使用接受class参数的
unmarshal
方法时,您是在告诉JAXB XML对应的类,而不是由根元素自动确定它

results = (HotelListResponse) JAXB.unmarshal(source, HotelListResponse.class)
因为XML文档的级别是错误的,所以JAXB无法将任何XML文档与您的域模型相匹配。因此,只填充默认值。这就是为什么结果中充满了
0
false
的值

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<HotelListResponse xmlns="http://v3.hotel.wsapi.ean.com/">
    <numberOfRoomsRequested>0</numberOfRoomsRequested>
    <moreResultsAvailable>false</moreResultsAvailable>
    <HotelList size="0" activePropertyCount="0"/>
</HotelListResponse>

0
假的

谢谢,我将删除@XmlSchema,关于我使用的方法,您推荐哪种方法?你能说得更具体一点吗?@JackMoore-你使用的
unmarshal
方法很好。您只需在对应于
的DOM
元素上通过创建
DOMSource
。我按照这个问题尝试创建DOMSource,但没有成功。@JackMoore-您尝试了什么,发生了什么?不确定发生了什么,它遗漏了其中一个字段,并按照问题的解释显示为零。什么都没有改变。