简体   繁体   English

使用Oracle XDK SAX / DOM解析器进行XML验证

[英]XML Validation using Oracle XDK SAX/DOM Parser

I am trying to validate xml against xsd using SAX/DOM parser, both the files are stored as CLOB column in the database. 我正在尝试使用SAX / DOM解析器针对xsd验证xml,两个文件均作为CLOB列存储在数据库中。 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 ie getNumMessages() (XMLParseException) never returns value greater than 100 and thus limits the output of the validation errors. 该代码可以正常工作,但对于大文件,它永远不会超出一定数量的错误,即getNumMessages()(XMLParseException)永远不会返回大于100的值,从而限制了验证错误的输出。 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 Datebase版本 :Oracle Database 11g企业版11.2.0.2.0版-64位生产

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 从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. 谢谢。

Have you considered using SAXParser.setErrorHandler to register callback code that can accumulate the errors as they are encountered? 您是否考虑过使用SAXParser.setErrorHandler注册可以在遇到错误时累积错误的回调代码?

http://docs.oracle.com/cd/E11882_01/appdev.112/e10769/oracle/xml/parser/v2/XMLParser.html#setErrorHandler_org_xml_sax_ErrorHandler_ http://docs.oracle.com/cd/E11882_01/appdev.112/e10769/oracle/xml/parser/v2/XMLParser.html#setErrorHandler_org_xml_sax_ErrorHandler_

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM