首页 文章

jaxb unMarshaller失败ClassCastException,因为两个xml元素的名称相同 . 为什么?

提问于
浏览
0

我和我的unmarshaller有问题 . 我有一个如下所示的文件:

<Employee xmlns="namespace here">
<Employee>
    <Id>2</Id>
    <Name>idk</Name>
</Employee>
</Employee>

问题是根元素,元素列表的名称是“Employee” . 当我去解组时,我得到了一个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;
}

我能够使用此代码来编组一个看起来与我需要解组的文件完全相同的文件 . 所以我很困惑 . 什么是遗失所以当我解组时,unmarshaller没有给我以下例外:

java.lang.ClassCastException: XXXX.EmployeeInformationElement cannot be cast to XXXX.EmployeeInformation

1 回答

  • 0

    无法重现(在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;
        }
    }
    

相关问题