JAXB: javax.xml.bind.UnmarshalException: unexpected element

JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:"http://mynamespace/", local:"entity"). Expected elements are ({}entity)]

The following exception broadly means that an element encountered during unmarshallization isn’t found in the JAXB Context.
The details, however point us elsewhere – the element IS in the context, but it exists there without a namespace.
The first thing to do is check whether our @XmlType / @XmlRootElement define a namespace. And they do.
Finally, there is @XmlSchema annotation that defines an attribute called elementFormDefault. In order to put the elements together with their namespaces, this atribute must be set to javax.xml.bind.annotation.XmlNsForm.QUALIFIED. This annotation should be defined in package-info.java


27 Responses to “JAXB: javax.xml.bind.UnmarshalException: unexpected element”

  1. Rob Says:

    >> This annotation should be defined in package-info.java

    Thanks for documenting this issue. I hit this same problem when the package-info.java went missing.

    For the record my exception looked like:

    unexpected element (uri:”http://foo/1.0″, local:”group”). Expected elements are
    javax.xml.bind.UnmarshalException: unexpected element (uri:”http://foo/1.0″, local:”group”). Expected elements are
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:631)

    I added back the missing package-info.java with the appropriate annotations and that fixed it.

    Thanks, Rob.

  2. Cor Says:

    I have tried any configuration possible including UNSET, QUALIFIED and UNQUALIFIED. I still get the dreaded unexpected element error… :
    (uri:”http://foo/QueryResponses”, local:”bar”). Expected elements are

  3. senthil Says:

    Thank you. I wasted a lot of time because I forgot to look at the package-info. It worked after that.

  4. William Antônio Siqueira Says:


    same that senthil. I wasted some time trying to make it works. Thank you so much for this post.

  5. Andre Says:


    when using ANT 1.7.1 to build, e.g., the war-File, the package-info.class file won’t be compiled due to a bug in this ANT version. Hence, the files WEB-INF/classes/…/package-info.class are missing, which produces UnmarshalExceptions. Work around: Touch the java-files before compiling them with

  6. Andre Says:


    <fileset dir=”src” includes=”**/package-info.java” />

  7. zuni Says:


    javax.xml.bind.UnmarshalException: unexpected element (uri:”http://www.w3.org/1999/xhtml”, local:”Enrollment”).

    Kindly help me Out please 😦
    where i am going wrong.

  8. netzgewitter Says:

    Thank you so much!!! We have a special deployment method and it was not copying package-info.class, because it doesn’t contain an actual class.

    You probably saved me from troubleshooting countless more hours.

  9. smeebond Says:

    this post really saved me hours of work!! Thanks a lot!

  10. Bhavesh Says:

    where can i find packege-info

  11. satya Says:

    Hi, when I execute XJC, it is not generating package-info.java. What could be the problem? I am not using -npa option. To which location is this file generated by default?

  12. SinSerg Says:

    c:\Program Files\Java\jdk1.6.0_XX\bin\generated\

  13. ekutor Says:

    Thanks a lot ….only i have to modify tha package-info.java class

  14. Debopam Says:

    Thanks it really helped a lot.

  15. JavaUser Says:

    Thanks for the tip. If you use eclipse, it does not generate package-info.class and that causes javax.xml.bind.UnmarshalException: unexpected element. Compiling package-info.java ensures the exception goes away.

  16. shailesh Says:

    thanks bro ….. taken me back from hell 🙂

  17. naddy26 Says:

    I tried so many things still i am facing this problem. Please help

  18. Thiruppathi Says:

    Thanks for your support. and also i have the scenarios above u mentioned.but still i have the same issue.i cant find the exact reason for the issue..plz help me thanks in advance

  19. ishkee Says:

    Were you able to resolve this?
    I have tried all this but nothing works for me.

  20. tattoo Says:

    From the Island of Borneo comes the myth of a
    dragon named Kinabalu, who lived at the summit of
    a mountain of the same name, and possessed a fabled pearl of immense size.
    To grow to be a good tattoo artist, you need to practice your skill.

    Rates are competitive at these businesses and the tattoo artists are very talented.

  21. android apps Says:

    Hello! Do you use Twitter? I’d like to follow you if that would be ok. I’m absolutely enjoying your blog and look forward
    to new posts.

  22. Fotograni.net Says:

    Hey very cool blog!! Guy .. Beautiful .. Superb .. I will bookmark your
    site and take the feeds additionally? I’m satisfied to search out numerous useful info here within the submit, we’d
    like decelop more techniques in this regard, thank you
    for sharing. . . . . .

  23. dealbreaker.com Says:

    I’m not sure exactly why but this website is loading very slow for me.
    Is anyone else having this issue or is it a problem on my end?
    I’ll check back later on and see if the problem still exists.

  24. green coffee beans Says:

    Hi, I do think this is a great blog. I stumbledupon it 😉 I am going to return yet again since I bookmarked it.
    Money and freedom is the greatest way to change, may you be rich and continue to guide others.

  25. Frankie Says:

    My spouse and I absolutely love your blog and find almost all of your
    post’s to be precisely what I’m looking for. Would you offer guest writers to write content in
    your case? I wouldn’t mind writing a post or elaborating on a lot of the subjects you write about here.
    Again, awesome blog!

  26. Marco Says:

    Thanks for this solution : 1 hour of google search safe! 🙂

  27. best pillow for side sleepers Says:

    Pretty component of content. I just stumbled upon your web site and in accession capital
    to claim that I get in fact enjoyed account
    your blog posts. Any way I will be subscribing in your augment and even I success you get
    right of entry to constantly quickly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: