mavi mavi - 5 months ago 17
Java Question

Error in JAXB unMarshaller?

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

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.dJohn.com/teacher", local:"teacher"). Expected elements are (none)


And here is my file.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Teacher xmlns="http://www.dJohn.com/teacher" Id="0001" />


main class

import java.io.File;
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) {
e.printStackTrace();
}
}

}


and here is Teacher.java..leaving 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;



@XmlAccessorType(XmlAccessType.FIELD)
@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

Annotate your Tana class with XmlRootElement.

Comments