File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes JAXB Child only parsing issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JAXB Child only parsing issue" Watch "JAXB Child only parsing issue" New topic

JAXB Child only parsing issue

Vipul Bhara

Joined: Aug 12, 2009
Posts: 22

I am parsing a very big file using JAXB and trying to avoid unnecessary tags which I dont need, I am trying to just parse some of the grandChildren of the root element, and providing only those classes while creating JAXBContext. When I provide the grandChild class in classes to bound It throws exception, however If I provide the root class in the classes to bound it works.
if I provide this
JAXBContext context = JAXBContext.newInstance(new Class[] {com.parent.class});
It works fine.

but when I try this.
JAXBContext context = JAXBContext.newInstance(new Class[] {com.parent.child.grandChild.class});

It throws following exception

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"grandChild"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(

I have generated all classes with XJC. I do have the ObjectFactory in the same place as I have my generated class.
ANy clue, what is going wrong here?
I agree. Here's the link:
subject: JAXB Child only parsing issue
It's not a secret anymore!