jaxb unMarshaller 失败 ClassCastException,因为两个 xml 元素同名。为什么?
jaxb unMarshaller failure ClassCastException because two xml elements are the same name. Why?
我的解组器有问题。我有一个如下所示的文件:
<Employee xmlns="namespace here">
<Employee>
<Id>2</Id>
<Name>idk</Name>
</Employee>
</Employee>
问题是根元素和元素列表同名"Employee"。当我去 unmarshal 时,我得到一个 classcastexception。
@XmlRootElement(name="Employee")
public class EmployeeInformation {
List<EmployeeInformationElement> elements;
private String errorCode;
private String errorMessage;
public List<EmployeeInformationElement> getElements() {
return elements;
}
@XmlElement(name="Employee")
public void setElements(List<EmployeeInformationElement> elements) {
this.elements = elements;
}
public String getErrorCode() {
return errorCode;
}
@XmlElement(name="ErrorCode")
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
@XmlElement(name="ErrorMessage")
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
我能够使用此代码编组一个看起来与我需要解组的文件完全一样的文件。所以我很困惑。缺少什么所以当我解组时,解组器没有给我以下异常:
java.lang.ClassCastException: XXXX.EmployeeInformationElement cannot be cast to XXXX.EmployeeInformation
无法重现(在 Java 1.8.0_65 上测试)。
由于您没有提供 MCVE(最小的、完整的和可验证的示例),这里是一个有效的示例。
唯一已知的区别是为了简单测试删除了命名空间。
import java.io.StringReader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class Test {
public static void main(String[] args) throws Exception {
String xml = "<Employee>\r\n" +
"<Employee>\r\n" +
" <Id>2</Id>\r\n" +
" <Name>idk</Name>\r\n" +
"</Employee>\r\n" +
"</Employee>\r\n";
JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeInformation.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
EmployeeInformation empInfo = (EmployeeInformation)unmarshaller.unmarshal(new StringReader(xml));
System.out.println(empInfo);
}
}
@XmlRootElement(name="Employee")
class EmployeeInformation {
private List<EmployeeInformationElement> elements;
@XmlElement(name="Employee")
public List<EmployeeInformationElement> getElements() {
return elements;
}
public void setElements(List<EmployeeInformationElement> elements) {
this.elements = elements;
}
}
class EmployeeInformationElement {
private int id;
private String name;
@XmlElement(name="Id")
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name="Name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
我的解组器有问题。我有一个如下所示的文件:
<Employee xmlns="namespace here">
<Employee>
<Id>2</Id>
<Name>idk</Name>
</Employee>
</Employee>
问题是根元素和元素列表同名"Employee"。当我去 unmarshal 时,我得到一个 classcastexception。
@XmlRootElement(name="Employee")
public class EmployeeInformation {
List<EmployeeInformationElement> elements;
private String errorCode;
private String errorMessage;
public List<EmployeeInformationElement> getElements() {
return elements;
}
@XmlElement(name="Employee")
public void setElements(List<EmployeeInformationElement> elements) {
this.elements = elements;
}
public String getErrorCode() {
return errorCode;
}
@XmlElement(name="ErrorCode")
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
@XmlElement(name="ErrorMessage")
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
我能够使用此代码编组一个看起来与我需要解组的文件完全一样的文件。所以我很困惑。缺少什么所以当我解组时,解组器没有给我以下异常:
java.lang.ClassCastException: XXXX.EmployeeInformationElement cannot be cast to XXXX.EmployeeInformation
无法重现(在 Java 1.8.0_65 上测试)。
由于您没有提供 MCVE(最小的、完整的和可验证的示例),这里是一个有效的示例。
唯一已知的区别是为了简单测试删除了命名空间。
import java.io.StringReader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class Test {
public static void main(String[] args) throws Exception {
String xml = "<Employee>\r\n" +
"<Employee>\r\n" +
" <Id>2</Id>\r\n" +
" <Name>idk</Name>\r\n" +
"</Employee>\r\n" +
"</Employee>\r\n";
JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeInformation.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
EmployeeInformation empInfo = (EmployeeInformation)unmarshaller.unmarshal(new StringReader(xml));
System.out.println(empInfo);
}
}
@XmlRootElement(name="Employee")
class EmployeeInformation {
private List<EmployeeInformationElement> elements;
@XmlElement(name="Employee")
public List<EmployeeInformationElement> getElements() {
return elements;
}
public void setElements(List<EmployeeInformationElement> elements) {
this.elements = elements;
}
}
class EmployeeInformationElement {
private int id;
private String name;
@XmlElement(name="Id")
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name="Name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}