我正在尝试将XML文本转换为Java对象,但是prQueryStatus
XML属性中有一个数字。 Java字段的类型为enum
。 JAXB是否可以选择我的enum
?
Strxml:
<custom prQueryStatus="1" ></custom>
错误行:
CustAttrPrQuery custom = (CustAttrPrQuery)XmlOperations.deserializeFromXML(CustAttrPrQuery.class, strXmlCustom);
XmlOperations.deserializeFromXML():
public static Object deserializeFromXML(Class obj, String strXml) {
Object result = null;
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(obj);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(strXml);
result = unmarshaller.unmarshal(reader);
return result;
} catch (JAXBException e) {
return new String("-3 JAXB deSerialize Error");
}
}
CustAttrPrQuery:
@XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD)
@XmlRootElement(name = CustAttrPrQuery.RootElement)
public class CustAttrPrQuery {
public final static String RootElement = "custom";
@javax.xml.bind.annotation.XmlAttribute
private PrQueryStatus prQueryStatus = PrQueryStatus.NONE;
public PrQueryStatus getPrQueryStatus() {
return prQueryStatus;
}
public void setPrQueryStatus(PrQueryStatus prQueryStatus) {
this.prQueryStatus = prQueryStatus;
}
}
枚举:
public enum PrQueryStatus {
NONE,
ACIK,
TUMU
}
答案 0 :(得分:1)
您需要使用@XmlEnum
注释枚举类型
及其常数与@XmlEnumValue
,
这样JAXB就会知道如何从XML属性("0"
,"1"
,"2"
到枚举常量(NONE
,ACIK
,{{1 }}):
TUMU