0 Replies Latest reply: May 22, 2013 1:44 AM by 1008129 RSS

    XML Validation using XDK SAX/DOM Parser

    1008129
      Hello,

      I am trying to validate xml against xsd using SAX/DOM parser, both the files are stored as CLOB column in the database. I have the requirement to report all the validation errors and based on some helpful advice/code from the earlier posts in this forum, I have used the following code to validate and report the errors.

      The code works fine but for large files it never goes beyond a certain number of errors i.e. getNumMessages() (XMLParseException) never returns value greater than 100 and thus limits the output of the validation errors. Any pointers to suggest change in code or an alternative will be extremely helpful.

      Datebase Version : Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production

      CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED <> as package <package name>;
      import java.net.*;
      import java.io.*;
      import java.util.*;
      import java.sql.SQLException;

      import oracle.sql.CLOB;
      import oracle.xml.parser.schema.*;
      import oracle.xml.parser.v2.*;

      import org.w3c.dom.*;

      public class XMLSchemaVal
      {
      public static String validate(CLOB xmlDoc, CLOB xsdDoc)
      throws Exception
      {
      //Build Schema Object

      XSDBuilder builder = new XSDBuilder();
      Reader xsdInUnicodeFormat = xsdDoc.getCharacterStream();

      XMLSchema schemadoc = (XMLSchema)builder.build(xsdInUnicodeFormat, null);

      //Build XML Object
      Reader xmlInUnicodeFormat = xmlDoc.getCharacterStream();

      // Genereate the SAX

      SAXParser saxparser_doc = new SAXParser();

      // Set Schema Object for Validation

      saxparser_doc.setXMLSchema(schemadoc);
      saxparser_doc.setValidationMode(XMLParser.SCHEMA_VALIDATION);
      saxparser_doc.setPreserveWhitespace (true);

      String returnValue;

      try {
      saxparser_doc.parse (xmlInUnicodeFormat);
      returnValue = "The input XML parsed without errors.\n";
      }

      catch (XMLParseException se) {
      returnValue = "Parser Exception: ";
      for (int i=0 ; i < se.getNumMessages(); i++)
      returnValue += "<LN: " + se.getLineNumber(i) + ">: " + se.getMessage(i);
      //returnValue = "Parser Exception: " + se.getNumMessages();
      }

      catch (Exception e) {
      returnValue = "NonParserException: " + e.getMessage();
      }
      return returnValue;
      }
      }

      Function to call the above utility from PL/SQL

      CREATE OR REPLACE FUNCTION F_XMLSCHEMAVALIDATION (P_XML IN clob ,P_XSD IN clob) RETURN VARCHAR2 IS
      LANGUAGE JAVA NAME XMLSchemaVal.validate(oracle.sql.CLOB,oracle.sql.CLOB) return java.lang.String';

      Thanks.