Unmarshalling with a SAX parser won't produce DOM nodes, which would be
returned
by Locator.getNode(). You might try to create a DOM tree from the XML file
and
unmarshal from that.
It's strange that you might be able to "perform some business logic" on a
broken
XML document (except diagnose the error, of course).
-W
On Tue, Mar 30, 2010 at 2:52 AM, Srikanth R Chinthala <csr_1978_at_yahoo.com>wrote:
>
> 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.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>