Skip to main content

JAXB Child only parsing issue

2 replies [Last post]
vipulrb29
Offline
Joined: 2012-04-19
Points: 0

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.
so
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(UnmarshallingContext.java:648)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:231)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1051)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:484)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)

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?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pokeylafarge
Offline
Joined: 2012-04-25
Points: 0

Off the shelf answer.

Is com.parent.child the package name and shouldn't the the classname be GrandChild rather than grandChild?

pokeylafarge
Offline
Joined: 2012-04-25
Points: 0

You provided just an example, off course. Never mind the answer.