mavi mavi - 1 year ago 80
Java Question

Error in JAXB unMarshaller?

i am getting the following while reading from my .xml is the error

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"teacher"). Expected elements are (none)

And here is my file.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Teacher xmlns="" Id="0001" />

main class

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class convertorDemo {
public static void main(String[] args) {

try {

File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Teacher.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Teacher mrS= (Teacher ) jaxbUnmarshaller.unmarshal(file);

} catch (JAXBException e) {


and here is some other details

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "Teacher", propOrder = {
public class Teacher{

@XmlAttribute(name = "Id")
protected String Id;

public String getId() {
return Id;

public void setId(String value) {
this.Id = value;


I don't know what my problem is and i am new to JAXB.please help!!

Answer Source

Annotate your Tana class with XmlRootElement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download