users@jaxb.java.net

Re: java 5 enum type serialization problem

From: Bartek Wasko <bartek_at_consol.pl>
Date: Tue, 17 Oct 2006 08:33:19 +0200

Aleksei Valikov napisał(a):
> 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
Thanks for hint, but the result I got is the same as if I would add this
enum type declaration to my jaxb.index file, so I unfortunately got:

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.SAXException2: unable to marshal type
"generated.EnumType" as an element because it is missing an
@XmlRootElement annotation]

As I wrote, after adding this annotation, it starts working, but I do
not want to do this, especially that my model is generated every time
during building distribution.
So is there a way for doing this in an automated way, without "manual"
source modifications. ???

Thanks
bartek

-- 
**********************************************
ConSol*
Consulting & Solutions Software Poland
ul. Bronowicka 10A, 30-084 Kraków
tel. +48 (012) 6231525
bartek.wasko_at_consol.pl
http://www.consol.de
**********************************************