users@jaxb.java.net

Re: java 5 enum type serialization problem

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 16 Oct 2006 17:34:07 +0200

Hi.

> I have generated a model using xjc, based on following schema:
>
> <xs:simpleType name="enumType">
> <xs:restriction base="xs:string">
> <xs:enumeration value="ENGINEER"/>
> <xs:enumeration value="CUSTOMER"/>
> <xs:enumeration value="ADMIN"/>
> </xs:restriction>
> </xs:simpleType>
>
> The generated enum class is as follows:
>
> @XmlEnum
> public enum EnumType {
>
> ADMIN,
> CUSTOMER,
> ENGINEER;
>
> public String value() {
> return name();
> }
>
> public static EnumType fromValue(String v) {
> return valueOf(v);
> }
>
> }
>
> but if I try to serialize it using this:
>
> try {
> JAXBContext ctx = JAXBContext.newInstance("generated");
> Marshaller mar = ctx.createMarshaller();
> try {
> mar.marshal(EnumType.ADMIN, new
> FileOutputStream("c:\\test.xml"));
> } catch (FileNotFoundException e) {
> e.printStackTrace();
> }
> } catch (JAXBException e) {
> e.printStackTrace();
> }
>
> I get :
>
> javax.xml.bind.JAXBException: test.EnumType nor any of its super class
> is known to this context
>
> If I add jaxb.index with this class name, it will result in error
> message sayign that I have to mark my class with XmlRootElement annotation.
> Indeed, after doing this, it works. But I do not want to do such
> additional manipulation , I want to have my model automatically
> generated and usefull without any changes.
> Is it possible in this particular case??
>
> I would appreciate any hints


Add <xs:element name="MyEnum" type="enumType"/> to your schema.
Your enumType by itself is just a type. You can only marshal an element.

Bye.
/lexi