BATMAN_2008
BATMAN_2008

Reputation: 3520

How to validate XML against XSD using the Moxy implementation without POJO and capture all errors?

I would like to validate the XML against XSD using the Moxy implementation so I have added the dependency for the same in pom.xmml:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.moxy</artifactId>
    <version>4.0.0</version>
</dependency>

I have created custom ErrorHandler to handle all the errors:

import jakarta.xml.bind.ValidationEvent;
import jakarta.xml.bind.ValidationEventHandler;

public class MyValidationEventHandler implements ValidationEventHandler {

    public boolean handleEvent(ValidationEvent event) {
        System.out.println("EVENT");
        System.out.println("SEVERITY: " + event.getSeverity());
        System.out.println("MESSAGE: " + event.getMessage());
        System.out.println("LINKED EXCEPTION: " + event.getLinkedException());
        System.out.println("LOCATOR");
        System.out.println("LINE NUMBER: " + event.getLocator().getLineNumber());
        System.out.println("COLUMN NUMBER: " + event.getLocator().getColumnNumber());
        System.out.println("OFFSET: " + event.getLocator().getOffset());
        System.out.println("OBJECT: " + event.getLocator().getObject());
        System.out.println("NODE: " + event.getLocator().getNode());
        System.out.println("URL: " + event.getLocator().getURL());
        System.out.println("\n \n");
        return true;
    }
}

Following is my Main class to validate the XML:

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Unmarshaller;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.InputStream;

public class ValidateXmlWithXsd {
    public static void main(String[] args) throws Exception {
        // Create a SchemaFactory object
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        // Load the XSD schema file
        final InputStream xsdDocument = TestMain.class.getResourceAsStream("/xsd/myXSD.xsd");
        final Source xsdSource = new StreamSource(xsdDocument);
        Schema schema = sf.newSchema(xsdSource);

        // Create a JAXBContext
        JAXBContext jc = JAXBContext.newInstance();

        // Create an Unmarshaller object
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new MyValidationEventHandler());

        // Unmarshal the XML document, using the schema for validation
        final InputStream xmlDocument = TestMain.class.getResourceAsStream( "/myXML.xml");
        unmarshaller.unmarshal(xmlDocument);
    }
}

I am facing few issues:

  1. I am unable to add the JAXBContext from Moxy then I try to add something like this then I get the error:
// Create a JAXBContext
org.eclipse.persistence.jaxb.JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContext.newInstance();

Then I get the error:

Required type: org.eclipse.persistence.jaxb.JAXBContext
Provided: jakarta.xml.bind.JAXBContext
  1. When I use the jakarta.xml.bind.JAXBContext and validate my XML then I for some reason get all the errors for all elements which should not happen because my XSD does not require all elements.

I would like to know how to validate a XML against XSD using the Moxy without needing to use the POJO class and how to capture all the errors not just a single error and throw exception.

Upvotes: 1

Views: 440

Answers (0)

Related Questions