users@jaxb.java.net

Question on JAXB 2.0 ValidationEventHandler

From: Srikanth R Chinthala <csr_1978_at_yahoo.com>
Date: Mon, 29 Mar 2010 17:52:20 -0700 (PDT)

Hi,

I am unmarshalling an xml using JAXB 2.0 as below:


JAXBContext jaxbContext = JAXBContext.newInstance("mypackage");
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
SchemaFactory schemaFactory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = schemaFactory.newSchema(new File("schemaxsd"));
unMarshaller.setSchema(schema);
unMarshaller.setEventHandler(
new ValidationEventHandler() {
// allow unmarshalling to continue even if there are errors
public boolean handleEvent(ValidationEvent ve) {
// ignore warnings
if (ve.getSeverity() != ValidationEvent.WARNING) {
ValidationEventLocator vel = ve.getLocator();
System.out.println("Line:Col[" + vel.getLineNumber() +
":" + vel.getColumnNumber() +
"]:" + ve.getMessage());
}
return true;
}
}
);
JAXBElement<CatalogType> catalogElement = (JAXBElement<CatalogType>)
unMarshaller.unmarshal(xmlDocument);

If the input xml is invalid it shows the exception message saying:

Invalid content was found starting with element 'name'. No child element is
expected at this point.

This exception says that the name element is not expected which is fine. If
this is the case then i need to the parent node of the element where the
exception occurred and should perform some business logic. I am not able to
get the parent node. I tried this:

validationEvent.getLocator().getNode();

but, I get null. How do i get the parent node?

Could anyone please help me out.

Thanks,
csr
-- 
View this message in context: http://old.nabble.com/Question-on-JAXB-2.0-ValidationEventHandler-tp28076972p28076972.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.