unexpected element (uri:"", local:"Group"). Expected elements are <{}group>
Meet an exception when unmarshalling from xml
JAXBContext jc = JAXBContext.newInstance(Group.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Group group = (User)unmarshaller.unmarshal(new File("group.xml"));
Group class has no any annotation and group.xml just contains data.
Anything can be the cause?
Luckily, the package-info class isn't required. I was able to fix mine problem with iowatiger08 solution.
Here is my fix showing the error message to help join the dots for some.
Error message
Code before fix
Code after fix
You can see the namespace added to @XmlRootElement as indicated in the error message.
I had the same problem.. It helped me, I'm specify the same field names of my classes as the tag names in the xml file (the file comes from an external system).
For example:
My xml file:
My Response class:
My ESList class:
My Item class:
My RegionList class:
My DemoUnmarshalling class:
It gives:
You need to put package-info.java in your generated jaxb package. Its content should be something like that
This is a fix for a pretty niche use case but it gets me each time. If you are using the Eclipse Jaxb generator it creates a file called package-info.
If you delete this file it will allow a more generic xml to be parsed. Give it a try!
After looking more, the root element has to be associated with a schema-namespace as Blaise is noting. Yet, I didnt have a package-info java. So without using the @XMLSchema annotation, I was able to correct this issue by using
Hope this helps!
It looks like your XML document has the root element "Group" instead of "group". You can: