users@jaxb.java.net

Re: Elements following unexpected elements are ignored

From: Kohsuke Kawaguchi <kohsuke.kawaguchi_at_sun.com>
Date: Tue, 06 Dec 2005 10:01:32 -0800

Gregor Koukkoullis wrote:
> Hi...
>
> I'm a happy JAXB since a few years, and I have to thank the team for this
> great work.

Thank you.

> I like to read a XML file that contains elements I like to ignore. Main
> reason is that a newer version of the server software contains new elements
> but older client versions dont understand them and should skip and ignore
> these elements.
> I was always receiving a unexpected element error and parsing stopped. I
> searched this mailing list and read that I should implemented a
> ValidationEventHandler that returns true on handleEvent() for this error. I
> did this.

Generally speaking, JAXB RI 1.0.x has a difficulty recovering from
unexpected element errors (JAXB RI 2.0 does a much better job.) The
behavior you described sounds like typical.

> The result is that this element gets now ignored and parsing continues
> (YES!) but all the following valid and known elements on the same level also
> get ignored and are called to be unexpected (oh no..).
>
> Is there any way I can get it to work to just skip the unknown elements and
> cleanly continue parsing the following known elements.

I wish we had resource to fix this, but we probably won't be able to fix
this in a timely fashion. (That said, ignoring an element shouldn't be
that difficult, and you just need to 'freeze' the state machine, so any
volunteer who wants to look into the code is more than welcome!)

If you need to continue to use JAXB 1.0, another approach is to
implement a SAX filter, use JAXP 1.3 validator to do a validation, and
then trim invalid elements on the fly so that JAXB won't see them.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com