Bharat Nanwani
Bharat Nanwani

Reputation: 703

Unmarshalling XML to Object with a List Returns Null Object

I am desperately looking for help on below. I've been on this for over a week now. Thus, posting this code in detail for descriptive answers. When I am unmarshalling the XML to the Object. It Returns Null for RatePlans tag.

Below is the XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OTA_HotelRatePlanRS EchoToken="fromDB" xmlns="http://www.opentravel.org/OTA/2003/05">
    <Success/>
    <RatePlans HotelCode="00000004" HotelName="Lemon Tree Hotel, Udyog Vihar, Gurugram">
        <RatePlan Start="01 Jan 15" End="31 Mar 19" RatePlanType="Business Single Room With Breakfast(01 Jan 15 ~ 31 Mar 19)" RatePlanID="0000212624" RatePlanQualifier="false" PromotionVendorCode="" RatePlanCategory="B2C">
            <BookingRules>
                <InventoryInfo InvCode="0000000085" InvType="Business Room Single"/>
            </BookingRules>
            <RatePlanLevelFee>
                <Fee>
                    <Taxes>
                        <Tax Code="35" Percent="0.0" ChargeUnit="N"/>
                    </Taxes>
                </Fee>
            </RatePlanLevelFee>
            <Commission StatusType="P">
                <CommissionableAmount Amount="25.00"/>
                <TPA_Extensions>
                    <ExtraGuest commissionable="Y" type="ExtraAdult1"/>
                    <ExtraGuest commissionable="Y" type="ExtraAdult2"/>
                    <ExtraGuest commissionable="Y" type="ExtraAdult3"/>
                    <ExtraGuest commissionable="Y" type="ExtraChild1"/>
                    <ExtraGuest commissionable="Y" type="ExtraChild2"/>
                    <ExtraGuest commissionable="Y" type="ExtraChild3"/>
                </TPA_Extensions>
            </Commission>
        </RatePlan>
    </RatePlans>
</OTA_HotelRatePlanRS>

Below is the OTA_HotelRatePlanRS Java Object

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="OTA_HotelRatePlanRS", namespace="http://www.opentravel.org/OTA/2003/05")
public class OTA_HotelRatePlanRS {

    @XmlAttribute(name="EchoToken")
    private String EchoToken;

    @XmlElement(name="Success")
    private Success success;

    @XmlElement(name="RatePlans")
    private RatePlans rateplans;

    @Override
    public String toString() {
        return "OTA_HotelRatePlanRS [EchoToken=" + EchoToken + ", success=" + success + ", rateplans=" + rateplans
                + "]";
    }
}

Below is the Success Java Object

@XmlRootElement(name="Success")
public class Success {

    @Override
    public String toString() {
        return "Success []";
    }

}

Below is the RatePlans Java Object

@XmlAccessorType(XmlAccessType.FIELD)
public class RatePlans {

    @XmlElement(name="RatePlan")
    private ArrayList<RatePlan> rateplan;

    public ArrayList<RatePlan> getRateplan() {
        return rateplan;
    }

    public void setRateplan(ArrayList<RatePlan> rateplan) {
        this.rateplan = rateplan;
    }

    @Override
    public String toString() {
        return "RatePlans [rateplan=" + rateplan + "]";
    }
}

Below is the RatePlan Java Object.

@XmlAccessorType(XmlAccessType.FIELD)
public class RatePlan {

    @XmlAttribute(name="Start")
    private String Start;
    @XmlAttribute(name="End")
    private String End;
    @XmlAttribute(name="RatePlanType")
    private String RatePlanType;
    @XmlAttribute(name="RatePlanID")
    private String RatePlanID;
    @XmlAttribute(name="RatePlanCategory")
    private String RatePlanCategory;

    @Override
    public String toString() {
        return "RatePlan [Start=" + Start + ", End=" + End + ", RatePlanType=" + RatePlanType + ", RatePlanID="
                + RatePlanID + ", RatePlanCategory=" + RatePlanCategory + "]";
    }
}

Below is the Main Method.

public static void main(String[] args) {

        try {
            File file = new File("hotelres.xml");
            JAXBContext jContext = JAXBContext.newInstance(OTA_HotelRatePlanRS.class);
            Unmarshaller unmarshallerObj = jContext.createUnmarshaller();
            OTA_HotelRatePlanRS ob = (OTA_HotelRatePlanRS) unmarshallerObj.unmarshal(file);
            System.out.println(ob);
        } catch (Exception e) {
            e.printStackTrace();
        }       

    }

Console Response OTA_HotelRatePlanRS [EchoToken=fromDB, success=null, rateplans=null]

Upvotes: 0

Views: 889

Answers (1)

Bharat Nanwani
Bharat Nanwani

Reputation: 703

I don’t believe this. I had been hovering on this query for over a week and it got resolved within few minutes after posting it here.

All I did from remove xmlns attribute from the XML message.

Upvotes: 1

Related Questions