[英]XML to Java JaxB
我試圖使用JAXB將XML轉換為Java的嘗試無法正常工作。 周圍還有許多其他類似的問題,但是我研究過的建議解決方案似乎都沒有幫助。
下面是我的豆
@XmlRootElement(name = "ListingResponse", namespace = "http://www.random.com")
@XmlType(propOrder = {"success", "listingId", "description"})
public class ListingResponse {
private String success;
private String listingId;
private String description;
public String getSuccess() {
return success;
}
@XmlElement(name = "Success")
public void setSuccess(String success) {
this.success = success;
}
public String getListingId() {
return listingId;
}
@XmlElement(name = "ListingId")
public void setListingId(String listingId) {
this.listingId = listingId;
}
public String getDescription() {
return description;
}
@XmlElement(name = "Description")
public void setDescription(String description) {
this.description = description;
}
以下是我嘗試解組的嘗試
ListingResponse response = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(ListingResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
response = (ListingResponse) jaxbUnmarshaller.unmarshal(new File("response.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
最后是我的response.xml內容
<ListingResponse xmlns="http://www.random.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Success>true</Success>
<Description>ListingId 123 created.</Description>
<ListingId>123</ListingId>
</ListingResponse>
response
'不為空。 @XMLEelement
批注的@XmlAccessorType(XmlAccessType.FIELD / PROPERTY)
, @XMLEelement
似乎也無濟於事。 但是,響應始終為“空”,且未初始化任何字段。
你們能在這里發現問題嗎?
當前,您僅為根元素指定了正確的名稱空間限定。 您需要使用包級別@XmlSchema
批注來映射模型的名稱空間限定。
package-info.java
@XmlSchema(
namespace = "http://www.random.com",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
欲獲得更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.