使用列表将XML解组为对象返回空对象

时间:2018-04-16 10:40:04

标签: java xml jaxb unmarshalling

我正在拼命寻求下面的帮助。我已经待了一个多星期了。因此,详细发布此代码以获取描述性答案。 当我将XML解组为Object时。它为RatePlans标记返回Null。

下面是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>

以下是OTA_HotelRatePlanRS Java对象

@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
                + "]";
    }
}

以下是Success Java Object

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

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

}

以下是RatePlans Java对象

@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 + "]";
    }
}

以下是RatePlan Java对象。

@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 + "]";
    }
}

以下是主要方法。

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

    }

控制台响应 OTA_HotelRatePlanRS [EchoToken = fromDB,success = null,rateplans = null]

1 个答案:

答案 0 :(得分:1)

我不相信这一点。我一直在这个查询上盘旋超过一个星期,并在发布后在几分钟内得到解决。

我所做的就是从XML消息中删除xmlns属性。