Skip to main content

Problem using JAXB: classes generated by JAXB not recognised in context

6 replies [Last post]
jannywong
Offline
Joined: 2008-11-26

What I'm trying to do is to have servlets handling XML files. So I've created a web application project in NetBeans. I then added the JAXB bindings by specifying the XSD files stored locally on my PC. The binding seems to be fine, and I can see the generated classes. There are five bindings, all with different names, and they all have ObjectFactory class in their folder (as this could be one of the possible causes of problem from what I saw online).

However when I'm using the unmarshaller, it doesn't work. I then traced it down to the problem that it seems only 2 of the classes (generated.MessageEntry, generated.MessageList) in the generated package are included in the JAXBContext.

NetBeans does recognise the generated classes as 'import generated.EventList' works. When using this:

EventList el = new EventList();
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(el.getClass().getPackage().getName());

It gives:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"event_list"). Expected elements are <{}message_list>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:523)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:199)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:194)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:71)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:920)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:366)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:347)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3084)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:912)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:167)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
at test.Test.main(Test.java:32)

I understand it's because the EventList class not in the context.

Then I tried javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance("generated.EventList");

output:
javax.xml.bind.JAXBException: "generated.EventList" doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:132)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:299)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
at test.Test.main(Test.java:25)

Then I tried using another PC replicating the whole project using the same NetBeans 6.1, with the same XSD files. This time when I checked the context.toString(), there are 4 classes included: generated.CoverageType, generated.MatchEntry, generated.MatchList, generated.PlaystateType. I'm just very confused how come the classes included in the context seem to be rather random.

I did read on this link (https://jaxb.dev.java.net/guide/Unmarshalling_is_not_working__Help_.html) that "If you noticed that a class is missing, explicitly specify that to JAXBContext.newInstance. If you are binding classes that are generated from XJC, then the easiest way to include all the classes is to specify the generated ObjectFactory class(es)." But I'm not sure how to do this.

I've been bugged by this problem for days now. I'm looking forward to any suggestions. Thanks in advance!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jannywong
Offline
Joined: 2008-11-26

With help from a colleague, it seems the problem lies with the java class loader, where at run time it wasn't able to locate the EventList class from the 'generated' package. Because in my case, there are 5 XSDs, and the generated classes may not be put under on big folder for some reason (although the compiler can find the class). So one way of working around this is to copy the generated classes to a folder and use the classes in this folder instead the ones in 'generated'. Obviously if the XSDs change, then the classes need to be updated.
It's not a perfect solution, but it works so far.

Felipe Gaúcho

The namespaces should split the classes in their packages naturally..
there is no need to split by hand... other, you should never force the
package of the generated classes by hand, leave it to xjc .. it will
map the classes to its namespace folders ...

On Fri, Nov 28, 2008 at 11:01 AM, wrote:
> With help from a colleague, it seems the problem lies with the java class loader, where at run time it wasn't able to locate the EventList class from the 'generated' package. Because in my case, there are 5 XSDs, and the generated classes may not be put under on big folder for some reason (although the compiler can find the class). So one way of working around this is to copy the generated classes to a folder and use the classes in this folder instead the ones in 'generated'. Obviously if the XSDs change, then the classes need to be updated.
> It's not a perfect solution, but it works so far.
> [Message sent by forum member 'jannywong' (jannywong)]
>
> http://forums.java.net/jive/thread.jspa?messageID=319099
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
> For additional commands, e-mail: users-help@metro.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
For additional commands, e-mail: users-help@metro.dev.java.net

jannywong
Offline
Joined: 2008-11-26

Yes, the classes are split into different packages, but all under the 'generated' structure. Now the problem seems to be that the class loader just randomly finds the first set of classes from 'generated' then stop searching, which gives the class not recognised problem.
It's not a good practice to change the package names for xjc generated classes, I know. But is there a better way of solving the problem? I'd love to hear about it.

techiesaf
Offline
Joined: 2008-08-14

From what i understand your generated classes go under the "generated" package? In this package along with EventList you'll have some generated classes like ObjectFactory etc?
If that is the case in this statement

>
> Then I tried javax.xml.bind.JAXBContext jaxbCtx =
> javax.xml.bind.JAXBContext.newInstance("generated.Even
> tList");

You should change it to:
javax.xml.bind.JAXBContext jaxbCtx javax.xml.bind.JAXBContext.newInstance("generated");

The ObjectFactory has to be in this package basically ....

S

jannywong
Offline
Joined: 2008-11-26

I've tried
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance("generated");

Still got the same unexpected element error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"event_list"). Expected elements are <{}message_list>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:523)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:199)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:194)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:71)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:920)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:366)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:347)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3084)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:912)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:167)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
at test.Test.main(Test.java:32)

Any ideas?

kuoweiwang
Offline
Joined: 2007-05-25

Hi

Its been so many yrs. I am encountering this problem. Is there a fix somewhere? I have similar problem.

Here is my file structure:

package jaxb_conainer_class;
package loader_class;

Main.java imports both loader_class and jaxb_conainer_class;

In the loader_class, a call to
JAXBContext context = JAXBContext.newInstance(xxxx.class);

gives me the crash above

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
There's no ObjectFactory with an @XmlElementDecl for the element {}default.