Validating an xml file from within Natural

Hi,

After creating an xml file from within Natural, we would like to have the possibility to check this file for wellformedness and also - if possible - to validate it against a given xml schema.
We use the external SAX parser, not the simple XML parser.
I understand that a basic check of our xml output string could be done by just using the PARSE XML statement.

For validation, my idea would be to create actually 2 xml strings:

  • One with the ‘original’ url for the schema, this is the xml I want to return or to write in to a binary file
  • and one with a local url, i.e. one that could be validated against a xsd file stored in a directory available at runtime. This could be theoretically used to validate the xml without having to change the content of the original xml.
    Now I’m looking for a way to validate the local xml against an xsd.
    Is there any user exit or function available to do this from within Natural?
    What do others do? Call an external function, a webservice, or what else?

Kind regards
Eva Grill

Hi Eva,

I had the same problem and ended up writing a simple piece of Java code that validates an XML file against a given schema. I call the Java program from within Natural via User Exit USR1052L on Linux.

Definitely not the best solution, but I found no other way of validating XML in Natural.

Best regards,
Stefan

Hi Stefan,

thanks for your reply.
When running a Natural program in batch, and calling your Java code with the user exit, what requirements are there?
I assume the java module would need to have access to the internet if you validate an xml against a schema with a remote url?
Or do you have local copies of the schemas that you regularly need, and validate the xml against these local schemas?

Thanks very much in advance for any description and info you can share.

Kind regards
Eva

Hi Eva,

the Java program takes both the paths to an XML file and the corresponding schema file as arguments. I only validate XML files against my own schemas which are availabe via the file system. But I don’t think it would be a problem to validate against online schemas as long as the host has internet access.

The Java tool is really simple and only returns a different integer result according to the possible outcomes of the validation, which is returned to Natural via the user exit.

If you would like to take a look at my solution, I could provide the source code.

Best regards,
Stefan

Hi Stefan,

This would indeed be very helpful
Thanks very much in advance!

Kind regards
Eva

Hi Eva,

I’ve posted the source code here: Validating an XML file against a schema with Natural and Java

You can download the complete source code in a ZIP file at the bottom of the article.

Best regards,
Stefan

Thanks very much, Stefan!

Kind regards

Eva Grill