I am using EclipseLink dynamic moxy for jaxb. When I try to set an enum value, I get a ClassNotFound exception. Can anyone point out what I am doing wrong?
Schema:
<xs:element name="customer" type="customerType"> </xs:element>
<xs:complexType name="customerType">
<xs:sequence>
<xs:element name="number" type="numberEnum"/>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="numberEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
</xs:restriction>
</xs:simpleType>
Java code:
package uic;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.bind.JAXBException;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
public class test{
DynamicJAXBContext context;
test() {
try{
context = DynamicJAXBContextFactory.createContextFromXSD(new FileInputStream(new File("sample/NewXMLSchema1.xsd")), null, null, null);
} catch(JAXBException e) {
e.printStackTrace();
} catch(FileNotFoundException e){
e.printStackTrace();
}
DynamicEntity root = context.newDynamicEntity("CustomerType");
root.set("name", "tom");
Object enumValue = null;
try {
enumValue = context.getEnumConstant("uic.NumberEnum", "2");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
root.set("Number", enumValue);
}
public static void main(String args[]) {
new test();
}
}
Am i going wrong when I create the context using createContextFromXSD()? I followed this example: EclipseLink dynamic MOXy accessing enum values
I don’t have a problem when I am accessing other JAXB generated classes. The problem is only for the enum classes. Are they placed in a different package from the other generated classes or something?
Edit: In the main program, I receive an xml schema file as the input. I use an xsom parser to retrieve the element and type declarations, and then marshal using JAXB generated classes, to get the output, an XML file. So, any changes I need to do to the schema will have to be done dynamically.
Thanks.
I am a developer on the EclipseLink product and have been looking at your issue. A NumberEnum class was not generated because the enumeration values are all numbers, which is invalid for a Java enum. If you were to run your schema through the
xjccommand-line tool, you would also see that no enum class was generated.One solution would be to change your enum values to ONE, TWO, THREE, if you have the flexibility of changing your schema.
Otherwise, the standard way to get around this would be with the use of a JAXB bindings file, to customize the enum values:
However, we currently have a bug in EclipseLink Dynamic JAXB, and this enum customization is not being taken into account. We are working on this for our 2.4.1 release, you can track the progress of this bug here:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=383575
Thanks,
Rick