【问题标题】:Unable to Unmarshal XML response using JAXB classes无法使用 JAXB 类解组 XML 响应
【发布时间】:2018-12-12 08:52:00
【问题描述】:

例外:

javax.xml.bind.UnmarshalException:意外元素(uri:“http://www.cleartrip.com/hotel/hotel-search-response”,本地:“hotel-search-response”)。预期的元素是(无)


MyTestFile.txt(XML):

<hotel-search-response xmlns="http://www.cleartrip.com/hotel/hotel-search-response" xmlns:hotel-info="http://www.cleartrip.com/places/hotel-info" xmlns:common="http://www.cleartrip.com/hotel/common">
<search-criteria>
    <booking-date>2018-12-12+05:30</booking-date>
    <check-in-date>2018-12-14+05:30</check-in-date>
    <check-out-date>2018-12-16+05:30</check-out-date>
    <number-of-rooms>1</number-of-rooms>
    <number-of-nights>2</number-of-nights>
    <number-of-room-nights>2</number-of-room-nights>
    <city>Bangalore</city>
    <country>IN</country>
</search-criteria>
<currency>INR</currency>

JAXB 类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "hotel-search-response", propOrder = {
"searchCriteria",
"additionalCurrency",
"currency",
"baseUrl",
"hotels",
"pgfeeJson",
"pgchargeJson"
})
public class HotelSearchResponse {

@XmlElement(name = "search-criteria", required = false)
protected SearchCriteria searchCriteria;
@XmlElement(name = "additional-currency",required= false)
protected AdditionalCurrency additionalCurrency;
@XmlElement(required = false)
protected String currency;
@XmlElement(name = "base-url", required = false)
protected String baseUrl;
@XmlElement(required = false)
protected Hotels hotels;
@XmlElement(name = "pgfee-json", required = false)
protected String pgfeeJson;
@XmlElement(name = "pgcharge-json", required = false)
protected String pgchargeJson;
}

Java 主类:

JAXBContext jc= JAXBContext.newInstance(HotelSearchResponse.class);
        javax.xml.bind.Unmarshaller ums = jc.createUnmarshaller();
        HotelSearchResponse emp=(HotelSearchResponse) ums.unmarshal(new    File("E:/MyTestFile.txt"));

【问题讨论】:

  • 嗨,现在它给出了以下 ecveption:
  • com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 个计数 IllegalAnnotationExceptions 属性 searchCriteria 存在但未在 @XmlType.propOrder 中指定此问题与以下位置有关:在protected org.tg.service.jaxb.hoteladditionalinfo.classes.SearchCriteria org.tg.service.jaxb.hoteladditionalinfo.classes.HotelSearchResponse.searchCriteria
  • SearchCriteria 是 HotelSearchResponse 的子类。是否有必要在子类中提及“命名空间”?

标签: java jaxb unmarshalling


【解决方案1】:

首先,您需要有一个@XmlRootElement。其次,您需要指定命名空间(因为您使用命名空间)。

@XmlRootElement(name = "hotel-search-response", namespace = "http://www.cleartrip.com/hotel/hotel-search-response")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "hotel-search-response", propOrder = { "currency", "baseUrl",
"pgfeeJson", "pgchargeJson" }, namespace = "http://www.cleartrip.com/hotel/hotel-search-response")
public class HotelSearchResponse

上面,propOrder 被简化了。

【讨论】:

  • 不,实际上 XML 很大,我只发布了 10-20n 行,xml 仍然存在
  • 您好,现在它给出了以下 ecveption:– abhinandan abhinandan 18 小时前 com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 个 IllegalAnnotationExceptions 计数属性 searchCriteria 存在但未在@XmlType.propOrder 这个问题与以下位置有关:在受保护的 org.tg.service.jaxb.hoteladditionalinfo.classes.SearchCriteria org.tg.service.jaxb.hoteladditionalinfo.classes.HotelSearchResponse.searchCriteria – abhinandan abhinandan
  • SearchCriteria 是 HotelSearchResponse 的子类。是否有必要在子类中提及“命名空间”?
  • @abhinandanabhinandan propOrder 必须按所需顺序包含所有字段。在我的示例中,propOrder 被简化了。你看完我的回答了吗?
猜你喜欢
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 2013-01-17
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
相关资源
最近更新 更多