Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026

Unmarshalling fails with: 'jakarta.xml.bind.UnmarshalException: unexpected element (uri:"http://acme.com/order", local:"order"). Expected elements are <{}order>'. What does this mean?

The message means JAXB knows a root element called order in no namespace, written <{}order>, but the document's root is order in the namespace http://acme.com/order, so as far as JAXB is concerned it is a different element. We fix the mapping by putting the namespace on @XmlRootElement or, better, in @XmlSchema in package-info.java; for xjc-generated classes we make sure ObjectFactory and package-info.java are present, or we unmarshal with unmarshal(source, Order.class), which returns a JAXBElement. Prefixes are never the problem: JAXB compares the namespace URI.

Why this question is asked

This error appears the first time a service talks to a partner whose XML uses namespaces, and it confuses people because the element name in the message looks identical on both sides: order and order. Interviewers ask it because reading the message correctly is the whole answer, and because it reveals whether we understand that an XML name is really a pair of namespace and local name. In this answer, we will decode the message, reproduce it, fix it in the three common ways, and see why namespace prefixes are a red herring.

1Reading the Error Message

In XML, the full name of an element is a pair: a namespace URI and a local name. The notation {uri}local, sometimes called Clark notation, writes that pair in one string. So <{}order> means local name order with an empty namespace, and {http://acme.com/order}order means local name order in the acme namespace. These are as different to an XML parser as order and invoice.

Java
jakarta.xml.bind.UnmarshalException: unexpected element (uri:"http://acme.com/order", local:"order").
Expected elements are <{}order>
// what the document has: namespace http://acme.com/order, local name order
// what JAXB knows: namespace "" (none), local name order

So the message reads: the document's root element is {http://acme.com/order}order, and the only root elements I know are {}order. Our Java mapping says the order root is in no namespace; the document says it is in the acme namespace. One of the two has to change, and since the partner's document is the contract, it is the mapping.

2Reproducing the Problem

Here is a minimal class and a document that looks right at first sight. The xmlns attribute on the root puts it, and its children, in the namespace.

Java
@XmlRootElement(name = "order") // no namespace given
@XmlAccessorType(XmlAccessType.FIELD)
public class Order {
private String id;
}
String xml = """
<order xmlns="http://acme.com/order">
<id>A-17</id>
</order>
""";
JAXBContext.newInstance(Order.class).createUnmarshaller().unmarshal(new StringReader(xml));
// UnmarshalException: unexpected element (uri:"http://acme.com/order", local:"order").
// Expected elements are <{}order>

Notice that the document uses a default namespace, xmlns="..." with no prefix. It is easy to overlook, because the element names have no prefix and look unqualified. But a default namespace applies to the element and all its unprefixed descendants, so even <id> is in the acme namespace.

3Fix 1: Put the Namespace in the Mapping

The quickest fix is to add the namespace to the root annotation. But that alone only moves the error one level down: the root now matches, but the child <id> is also in the acme namespace, while JAXB still expects {}id, so JAXB silently skips it and id stays null. Unknown child elements do not raise an error by default, which makes this second problem easy to miss.

Java
@XmlRootElement(name = "order", namespace = "http://acme.com/order")
public class Order {
private String id; // still expects {}id
}
// unmarshal succeeds, but order.id == null (the {http://acme.com/order}id element was ignored)
What JAXB expects after each fix
MappingRoot expectedChild expectedResult
no namespace (broken){}order{}idUnmarshalException
namespace on @XmlRootElement only{acme}order{}idroot ok, id silently null
@XmlSchema, elementFormDefault QUALIFIED{acme}order{acme}idworks

The better fix is to declare the namespace once for the whole package, in package-info.java, with elementFormDefault = QUALIFIED so child elements are in it too. Then the root and every child match, and the classes stay free of repeated namespace strings.

4Fix 2: The Package-Level Namespace

This is the mapping that matches the document. It is also exactly what xjc generates when it compiles a schema whose targetNamespace is http://acme.com/order with qualified elements.

Java
// package-info.java in the package of Order
@XmlSchema(namespace = "http://acme.com/order", elementFormDefault = XmlNsForm.QUALIFIED)
package com.acme.order.xml;
import jakarta.xml.bind.annotation.*;
Order o = (Order) ctx.createUnmarshaller().unmarshal(new StringReader(xml));
o.getId(); // "A-17"

If the classes were generated by xjc and the error still appears, the generated package-info.java or ObjectFactory has probably been lost: someone copied only some generated files, a build step excluded package-info, or the context was created with a list of classes instead of the package. Generated code relies on both files. package-info.java carries the namespace, and ObjectFactory declares the root elements. Creating the context from the package name, JAXBContext.newInstance("com.acme.order.xml"), or from ObjectFactory.class, makes sure both are found.

5Fix 3: Unmarshal With a Declared Type

Sometimes we cannot or do not want to rely on root element declarations, for example when the class has no @XmlRootElement at all, which is common for xjc classes generated from named schema types. Then we tell JAXB the type to use, and it accepts whatever root element it finds.

Java
Source source = new StreamSource(new StringReader(xml));
JAXBElement<Order> el = ctx.createUnmarshaller().unmarshal(source, Order.class);
el.getName(); // {http://acme.com/order}order
el.getValue(); // the Order, with id "A-17" if the package namespace is right

The JAXBElement wrapper carries the element's real name next to the value. Notice the condition in the last comment: the declared type removes the root check, but the children are still matched by namespace, so the package-level namespace is still needed for them.

6Why the Prefix Is Never the Problem

A common reaction is to look at prefixes: the partner writes <ns2:order> and our samples write <order>, so maybe that is the mismatch. It is not. A prefix is a local abbreviation declared in the document, and two documents can use any prefixes they like for the same URI. JAXB, like every namespace-aware parser, resolves each prefix to its URI and compares only the URI and local name. <a:order xmlns:a="http://acme.com/order"> and <order xmlns="http://acme.com/order"> are the same element.

So when this error appears, we compare URIs character by character. Small differences really do matter: http versus https, a trailing slash, a version segment such as /v2, or different capitalisation all make a different namespace.

7Follow-up questions

Can I make JAXB ignore namespaces?

Not directly, and it is usually a bad idea, because namespaces are part of the contract. If we must read documents with inconsistent namespaces, we can filter them with a SAX XMLFilter or a StAX wrapper that rewrites the namespace before JAXB sees it, but fixing the mapping or the partner's documents is better.

Why did the error list only one expected element?

Because the context knows only the root elements declared in the classes it was created with. A context created from the whole generated package lists every global element from the schema, which is also a quick way to check whether ObjectFactory was found.

How do I write the namespace correctly when marshalling?

The same mapping handles both directions. With @XmlSchema in package-info.java, marshalled output carries the namespace on the root and qualified children, and @XmlNs entries let us choose the prefix or a default namespace.

8Conclusion

In this answer, we read the error as a mismatch between two element names: the document's {http://acme.com/order}order and the {}order JAXB knows. We fixed it by declaring the namespace in package-info.java with qualified element form, so children match too, saw why a namespace on @XmlRootElement alone leaves child fields silently null, checked that xjc's package-info.java and ObjectFactory are present, and used unmarshal(source, Order.class) when no root declaration is available. Finally, we saw that JAXB compares namespace URIs, never prefixes.

What to say in the interviewAbout 80 seconds aloud

Open with

The message is telling me that the names do not match once namespaces are included. The document's root is order in the namespace http://acme.com/order, but JAXB only knows a root called order in no namespace, which is what the curly braces with nothing inside mean.

Then, if they want more

Usually the document has a default namespace, xmlns equals the acme URI, which is easy to miss because there is no prefix. I would fix the mapping rather than the document: the cleanest way is @XmlSchema in package-info.java with that namespace and elementFormDefault QUALIFIED, because the child elements are in the namespace too.

If I only put the namespace on @XmlRootElement, the root matches but children like id are silently skipped and come back null. If the classes were generated by xjc, I would check that package-info.java and ObjectFactory are on the classpath and create the context from the package or the ObjectFactory.

Another option is unmarshal with a source and Order.class, which returns a JAXBElement and does not need a root declaration, though children still need the right namespace. And I would not look at prefixes: JAXB compares the namespace URI, so I check the URIs character by character for things like https or a trailing slash.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Unmarshalling fails with: 'jakarta.xml.bind.UnmarshalException: unexpected element...": https://scrivio-interview-library.netlify.app/java/jaxb/0a8be7fb4a/