简体   繁体   中英

jaxb unMarshaller failure ClassCastException because two xml elements are the same name. Why?

I am having an issue with my unmarshaller. I have a file that looks like the following:

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

The problem is the root element and the list of elements are the same name "Employee". When I go to unmarshal I get a 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;
}

I am able to use this code to marshal a file that looks exactly like the file I need to unmarshal. So I am confused. What is missing so when I unmarshal, the unmarshaller does not give me the following exception:

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

Unable to reproduce (tested on Java 1.8.0_65).

Since you didn't provide an MCVE (Minimal, Complete, and Verifiable example), here is one that works.

Only known difference is that namespace was removed for simple testing.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM