users@jaxb.java.net

Re: Detect errors in xml files

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Fri, 9 Jan 2009 13:54:34 +0100

We've had the first part a short time ago. I'm adding the validation
event collector stuff so you can see how to use that.

If you want to validate your document before it is unmarshalled,
JAXB lets you request validation by passing an object of the
class javax.xml.validation.Schema to the Unmarshaller object.
First, you create this schema object by setting up a schema factory
for the schema language of your choice. Then you create the Schema
object by calling the factory's method newSchema:

Schema mySchema;
SchemaFactory sf =
    SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
try {
    mySchema = sf.newSchema( file );
} catch( SAXException saxe ){
    // ...(error handling)
    mySchema = null;
}

After the Unmarshaller object has been established, you pass it the schema.

JAXBContext jc = JAXBContext.newInstance( sb.toString() );
Unmarshaller u = jc.createUnmarshaller();
u.setSchema( mySchema );

Basically that's all there is to it. If the XML data validation fails,
an UnmarshalException (from javax.xml.bind) is thrown. Make sure to let
the user of your program see the exception message so that the problem
can be fixed. If you'd like to create your own error messages, you can
pass a ValidationEventCollector to the unmarshaller which will store
validation events into it so that you can retrieve an event and query
its individual attributes. Insert these lines before you call the
unmarshal method:

ValidationEventCollector = new ValidationEventCollector();
u.setEventHandler( vec );

The best place for checking the event collector is in the finally phrase
of the try statement wrapping all of this:

if( vec != null && vec.hasEvents() ){
    for( ValidationEvent ve: vec.getEvents() ){
        String msg = ve.getMessage();
        ValidationEventLocator vel = ve.getLocator();
        int line = vel.getLineNumber();
        int column = vel.getColumnNumber();
        System.err.println( origin + ": " + line + "." +
                            column + ": " + msg );
    }
}

Now this looks as if the validation process would be kind enough to
present you with all the errors in your XML document, or at least as
many as possible but, alas, it appears that the validation process
throws an exception as soon as the first deviation is detected.
This is what JAXB (or, rather, Apache's Xerces, which is used by JAXB)
does in JAXB 2.1.

-W



On Fri, Jan 9, 2009 at 1:01 PM, mimoune <lhafi_at_yahoo.fr> wrote:

>
> thank you for your answer..
>
> by only validating i can't treat the error to indicate where exactly has
> occured, and about the listner i have no idea how it works, if you can give
> an example..
>
>
>
> Felipe Gaucho wrote:
> >
> > use the xsd schema to validate the xml ..
> >
> > that solves the types and ranges problems...
> > f you need also business validation on the xml data, use a listener of
> > the JAXB parser to validate these cases...
> >
> > if this solution fits your requirements, ask for an example
> >
> > On Fri, Jan 9, 2009 at 12:37 PM, mimoune <lhafi_at_yahoo.fr> wrote:
> >>
> >> Hi,
> >>
> >> i need to get data from xml, my problem is that i need to know the xml
> >> file
> >> errors (like elements missing or data type) to report the errors to
> other
> >> users..
> >>
> >> what i was trying to do is unmarshal the xml file and with get methods
> to
> >> check if i have errors with try catch, bt i don't get any errors..
> >>
> >> example
> >>
> >> try{
> >> double valor = getInvoiceSeriesCode();
> >> } catch(Exception e){
> >> error = true;
> >> e.printStackTrace();
> >> }
> >>
> >> but that way i have no message erros..
> >>
> >> some help? and sorry for my english
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/Detect-errors-in-xml-files-tp21370693p21370693.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
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> > For additional commands, e-mail: users-help_at_jaxb.dev.java.net
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Detect-errors-in-xml-files-tp21370693p21370979.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
>
>