0

We have a large number of classes using JAXB for serialization and have not employed any validations for them during marshaling or unmarshalling process. Been exploring on JAXB in built mechanism for validation using set schema method. Now since I have a large number of JAXB model classes, generating schema for bulk of classes would result in complex schema. Trying to figure out to see if there is an ideal option through which validation can be performed for a specific class only in the schema and ignore the rest ?

I have the following lines of code,

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema;

    JAXBContext jc;
    try {
        jc = JAXBContext.newInstance(MyClass.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        schema = sf.newSchema(new File("c:\\schema1.xsd"));
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                if (event == null) {
                    return true;
                }
                String line = "unknown"; //$NON-NLS-1$
                String col = "unknown"; //$NON-NLS-1$
                ValidationEventLocator locator = event.getLocator();
                if (locator != null) {
                    line = Integer.toString(locator.getLineNumber());
                    col = Integer.toString(locator.getColumnNumber());
                }
                return true;
            }
        });
        unmarshaller.unmarshal(new File("C:\\Temp\\Test\\Test.xml"));

I have auto generated schema1.xsd for MyClass.java by following the link Is it possible to generate a XSD from a JAXB-annotated class? Now, in my case there are numerous other model JAXB classes involved apart from MyClass.java, so event gets raised for all those indicating an error which I do not want. How can i want to restrict the schema for a single JAXB class only ? Is that something possible ? Thanks in advance

User
  • 81
  • 2
  • 9
  • Have you tried: JAXBContext.newInstance(YourClass.class); ? You can also use namespaces in JAXB factory method. – Miko May 14 '18 at 06:27
  • @Miko it does not help. The validation event occurs for other elements in the XML which are not a part of the define schema in schema1.xsd – User May 14 '18 at 06:43
  • Maybe you can manually update the XSD to add xsd anytype/any definitions to ignore rest of the validations. – sashwat May 14 '18 at 07:47
  • @sashwat If I will have to ignore rest of the validation still I will have to define the root element with sequence as any under them. Is there any possibility by How there can be selective or part parsing of the input XML through the schema. – User May 16 '18 at 11:55
  • As far as I know the way you want the solution would not be possible. It would be a miss from JAXB validation if the missing elements are not highlighted. Another approach if you really really want validation is extracting XML using XPath/ XSLT and validating with XSD. I believe this is a huge overhead though. – sashwat May 17 '18 at 04:34

0 Answers0