javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"TestSubject"). Expected elements are <{}Test>
java, jaxb, xml
Solution
It seems like you aren't showing us the real XML you are trying to unmarshal. The error you are seeing would happen if your XML was in the form
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testSubject>
<firstName>test1</firstName>
<lastNAme>lastname</lastNAme>
<ssn>123456</ssn>
</testSubject>
instead of
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Test>
<testSubject>
<firstName>test1</firstName>
<lastNAme>lastname</lastNAme>
<ssn>123456</ssn>
</testSubject>
</Test>
Simply correct it.
As the stacktrace says
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"TestSubject"). Expected elements are <{}Test>
Your are getting a `<testSubject>` node where you are expecting a `<Test>` node. Since `<Test>` is meant to be the root node, that's where it occurs.
Problem
There are several help topics concerning the issue but I haven't found a solution that solved my issue. I appreciate guidance in solving the issue. I tried generating xml thru Marshal and below xml is what is generated from code. I have to work with xml structure below: ``` <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Test> <testSubject> <firstName>test1</firstName>`enter code here` <lastNAme>lastname</lastNAme> <ssn>123456</ssn> </testSubject> </Test> ``` code ``` @XmlRootElement(name = "Test") public class Test { public Test() { testSubject = new ArrayList<TestSubject>(); } List<TestSubject> testSubject; @XmlElement(name = "testSubject", type = TestSubject.class) public List<TestSubject> getTestSubject() { return testSubject; } public void setTestSubject(List<TestSubject> testSubject) { this.testSubject = testSubject; } ``` TestSubject class ``` public class TestSubject { String firstName; String lastNAme; int ssn; //getters and setters } ``` //unmarshall code in my main class ``` JAXBContext jc = JAXBContext.newInstance(Test.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("c://testSubjects.xml"); Test tests = (Test) unmarshaller.unmarshal(xml); ``` Exception ``` javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"TestSubject"). Expected elements are <{}Test> at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662) ```