users@jaxb.java.net

Re: JAXB Bug [?]: Unknown enum values are unmarhalled as NULLs

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Mon, 29 Sep 2008 11:42:36 +0200

The JAXB specification explicilty states that JAXB's unmarshalling process
does not, by default, include an XML document's validation beyond what is
absolutely necessary to come to grips with the instance document and to
produce a content tree (like traditional DOM and SAX parsers).

Hence, simply dropping a value that cannot be bound to a Java enum constant
is the simple solution for the conflict you describe.

To validate against an XML schema, use the technique described in the
javadoc.

Cheers
Wolfgang


On Thu, Sep 25, 2008 at 10:14 PM, Saloucious <saloucious_at_gmail.com> wrote:

>
> I have annotating my Classes and I encoutered the same problem.
>
> Actually I've a MyRootObject with a List of MyObject.
>
> MyObject has an attribute : MyEnum (with ENUM1, ENUM2)...
>
> When I run unmarshall method, everything works.
>
> Unfortunately suppose with have an XML file like :
>
> <MyRootObject>
> <MyObject>
> <MyEnum>ENUM1</MyEnum>
> </MyObject>
> <MyObject>
> <MyEnum>ENUM3</MyEnum>
> </MyObject>
> </MyRootObject >
>
> Unmarshall method will return a MyRootObject with a MyObject List composed
> by 2 instances
> 0 ==> MyObject [ENUM1]
> 1 ==> MyObject [null]
>
>
> JAXB could not find valueOf ENUM3 in MyEnum and simply ignore it.
>
> Is it possible to had a listener to log this problem and prevent
> MyObject[null] to be inserted in List.
>
>
> I've added @XmlJavaTypeAdapter and i'm able to log unmarshalling problem
> in
> my Adapter but can find how to prevent build object to be added in List.
>
> Any ideas ?
>
> Thanks.
>
>
>
> Andy Malakov wrote:
> >
> > It seems that JAXB silently ignores incorrect values of enum types. The
> > following short program demonstrates this issue. Instead of producing a
> > parsing mistake like "Cannot parse 'WRONG' as a value of enum type
> > 'Color'" JAXB unmarshaller simply sets Color to null.
> >
> >
> > public class JAXBTest {
> >
> > @XmlRootElement
> > public enum Color {
> > Red, Green
> > }
> >
> > public static void main(String[] args) throws Exception {
> > JAXBContext context = JAXBContext.newInstance(Color.class);
> >
> > printColor(context, "<color>Red</color>");
> > printColor(context, "<color>Green</color>");
> > printColor(context, "<color>WRONG</color>");
> > }
> >
> > private static void printColor(JAXBContext context, String xml)
> > throws JAXBException {
> > Unmarshaller um = context.createUnmarshaller();
> > Color color = (Color) um.unmarshal(new StringReader(xml));
> > System.out.println("Color:" + color);
> > }
> > }
> >
> > Best Regards,
> > Andy Malakov
> >
>
> --
> View this message in context:
> http://www.nabble.com/JAXB-Bug----%3A-Unknown-enum-values-are-unmarhalled-as-NULLs-tp15344297p19677405.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>