Reputation: 7218
I am trying to follow the guide here to unmarshall an XML document to a DynamicEntity. However I'm encountering a ClassCastException when I unmarshall my XML.
My code is as follows:
DynamicJAXBContext context = DynamicJAXBContextFactory.createContextFromXSD(DocumentGenerator.class.getClassLoader().getResourceAsStream("myXSD.xsd"), null, null, null);
FileInputStream xmlInputStream = new FileInputStream("C:\\Users\\alexba\\myXML.xml");
Unmarshaller unmarshaller = context.createUnmarshaller();
DynamicEntity statement = (DynamicEntity) unmarshaller.unmarshal(xmlInputStream);
The error is:
javax.xml.bind.JAXBElement cannot be cast to org.eclipse.persistence.dynamic.DynamicEntity
My pom dependencies are:
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.persistence</groupId>
<artifactId>commonj.sdo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<repositories>
I've been reading the code and trying to understand how Unmarshall can ever return a DynamicEntity.
Upvotes: 1
Views: 515
Reputation: 149037
MOXy will wrap the unmarshalled object in a JAXBElement
for the same reasons that it does for a static model. This is usually the case when the object corresponds to a named complex type.
You can either unmarshal the object as a JAXBElement<DynamicEntity>
:
JAXBElement<DynamicEntity> element = (JAXBElement<DynamicEntity>) unmarshaller.unmarshal(xmlInputStream);
DynamicEntity statement = element.getValue();
Or leverage JAXBIntrospector
:
DynamicEntity statement = (DynamicEntity) JAXBIntrospector.getValue( unmarshaller.unmarshal(xmlInputStream));
Upvotes: 1