Skip to main content

unmarshalling/namespace problem

1 reply [Last post]
tomber
Offline
Joined: 2005-09-26

Hi,

my XML schema(s) contain a namespace definition and I can correctly compile it with JAXB 2.0. I need to unmarshall XML streams which come along either with or without namespace information. While unmarshalling XML streams with namespace information is not a problem, I get a SAXParseException when unmarshalling XML streams without a namespace definition, e.g.:

Caused by: org.xml.sax.SAXParseException: unexpected root element (uri:"", local:"SystemStatus"). Expected elements are … <{http://www.ecma-international.org/standards/ecma-323/csta/ed3}SystemStatus>, …

I tried to use

inFact = XMLInputFactory.newInstance();
inFact.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);

and also

JAXBContext jc = JAXBContext.newInstance(contextPath);
unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(null);

but nothing helped.

Any ideas what I could do?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09

If your document comes without namespace information, that's not a valid document wrt the schema. It's so invalid that JAXB can't even unmarshal, which is what you are observing.

To make things work, you need to fix up namespaces before you feed it to JAXB.

One way to do it is to write a SAX XMLFilter and set the namespace URI field. You can then build a pipeline of the form:

SAX parser -> XMLFilter -> JAXB UnmarshallerHandler

to achieve the result you want.