users@jaxb.java.net

Re: How to get the element name on a validation error

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Tue, 21 Jan 2003 13:11:44 -0800

> I'm trying to create a custom ValidationEventHander that will provide more
> detail about the validation error than the DefaultValidationHandler. I'd
> like to have the handler return the name of the element that is throwing the
> error and a more human readable message detailing the cause. I also need to
> find the name of this element when using "on demand" validation. So far, I'm
> not having much luck. I have been using the following to try and get it:
>
> event.getLocator().getNode().getLocalName()
>
> But getNode() returns null in both unmarshall and on-demand. Are there any
> other ways to do what I'm trying to do? Thanks.

The getNode method is supposed to work only when you are unmarshalling
from a DOM tree (at least in case of the RI)

Also, as for obtaining element names in case of unmarshalling validation
errors, since errors cannot be always associated with elements, the RI
don't expose this information. AFAIK, no XML Schema processor expose
this information in an easily accessible way.

The only workaround that I can think of is to set up a SAX event
pipeline and monitor the current element before events are passed to
JAXB unmarshaller, but it might be too complicated if you are not
familiar with SAX.

Similarly, runtime on-demand validation cannot report element names and
so on. We modified the RI so that in the next release the error message
will have some indication of element/attribute names, but it's just
meant to be a part of error messages. If you search the archive, you
will find details of this issue.


I'm curious, what's the use cases behind the element names. Suppose you
can get the name of the element which is closest to the error, what are
you planning to do with it?

regards,
--
Kohsuke KAWAGUCHI                  408-276-7063 (x17063)
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com