java - 验证 oracle 的 java 源中的 xml 文档

标签 java xml oracle validation xml-validation

尝试做主题。

我正在尝试使用来自文件 (schemasource = 1) 和来自 clob (schemasource = 0) 的 xsd。 我有两个 xsd 模式 common_types.xsd 和 migom.xsd。第二包括第一。 问题是当我使用文件中的 common_types 模式时出现错误

ORA-29532:Java 调用被未捕获的 Java 异常终止:oracle.xml.parser.v2.XMLParseException:发生内部错误情况。

当我仅针对从 clob 读取的第一个模式验证 xml 时,我获得了成功,但是当我添加第二个 xsd 时,我得到了同样的错误,它什么也没说。

create or replace and compile java source named XmlTools AS
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;

import org.xml.sax.InputSource;
import oracle.sql.CLOB;
import java.io.IOException;
import org.xml.sax.SAXException;
import java.sql.SQLException;
import java.lang.IllegalArgumentException;
import oracle.xml.parser.v2.XMLParseException;
import javax.xml.parsers.ParserConfigurationException;

import java.io.*;

public class XmlValidator
{

  static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; 
  static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";


  public static void ValidateDocument(int schemasource, oracle.sql.CLOB schemadoc, oracle.sql.CLOB schemadoc1, oracle.sql.CLOB xmldoc) throws SAXException, IOException, SQLException, ParserConfigurationException, XMLParseException, IllegalArgumentException {


    try
          {              

            File myfile = new File(".//XML//common_types.xsd");
            if (myfile.exists())
            {
                Serv.log("ValidateDocument", "file size" + Long.toString(myfile.length()));
            }
            /*else
            {
                Serv.log("ValidateDocument", "file doesn't exists" );
            }*/

            Serv.log("ValidateDocument", "1" );
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(true);
            factory.setNamespaceAware(true);

            Serv.log("ValidateDocument", "2" );        
            SAXParser saxParser = factory.newSAXParser();
            saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);

            if (schemasource == 0)
            {
                InputSource schemaIs = new InputSource(schemadoc.getCharacterStream());        
                InputSource schemaIs1 = new InputSource(schemadoc1.getCharacterStream());  

                InputSource[] schemas = {schemaIs, schemaIs1};

                //saxParser.setProperty(JAXP_SCHEMA_SOURCE,   schemaIs); 
                saxParser.setProperty(JAXP_SCHEMA_SOURCE,   schemas); 
            }
            else
            {            
                saxParser.setProperty(JAXP_SCHEMA_SOURCE,   ".//XML//common_types.xsd"); 
            }
            XMLReader reader = saxParser.getXMLReader();

            //Получаем входной XML документ
            InputSource documentIs = new InputSource(xmldoc.getCharacterStream());
            Serv.log("ValidateDocument", "3" );          
            //Запуск разбора
            reader.parse(documentIs);
            Serv.log("ValidateDocument", "4" );          
            documentIs = null;     

           }
    /*catch (SAXException e) 
    {
        Serv.log("ValidateDocument", "SAXException" );
        Serv.log("ValidateDocument", "document is not valid because ");
        Serv.log("ValidateDocument", e.getMessage());
        throw(e);
    }*/          
    catch (ParserConfigurationException e) 
    {
        Serv.log("ValidateDocument", "ParserConfigurationException" );
        throw(e);
    }
    catch (IOException e) 
    {
        Serv.log("ValidateDocument", "IOException" );        
        throw(e);
    }

    catch (XMLParseException e)
    {
        Serv.log("ValidateDocument", "XMLParseException" );        
        Serv.log("ValidateDocument", e.getMessage());
        StackTraceElement[] stack = e.getStackTrace();        
        for (int i = 0; i < stack.length; i++)
        {
         Serv.log("stacktrace element no " + Integer.toString(i), "toString: " + stack[i].toString());
         Serv.log("stacktrace element no " + Integer.toString(i), "file name: " + stack[i].getFileName() + ", class name: " + stack[i].getClassName() + ", method name: " + stack[i].getMethodName() + ", line : " + stack[i].getLineNumber());
        }

        throw(e);
    }
    catch (IllegalArgumentException e)
    {
        Serv.log("ValidateDocument", "IllegalArgumentException" );        
        Serv.log("ValidateDocument", e.getMessage());
        throw(e);    
    }           


    }

}

从 java stacktrace 获得的附加信息:

文件名:XMLError.java,类名:oracle.xml.parser.v2.XMLError,方法名:flushErrors1,行:320 文件名:NonValidatingParser.java,类名:oracle.xml.parser.v2.NonValidatingParser,方法名:parseDocument,行:300 文件名:XMLParser.java,类名:oracle.xml.parser.v2.XMLParser,方法名:parse,行:200 文件名:XMLTOOLS,类名:XmlValidator,方法名:ValidateDocument,行:86

我的 oracle 版本是 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod 但我的目标是让它适用于从 9 开始的所有版本

最佳答案

更新:

因此,您将这些文件作为 CLOB 加载到数据库中。将它们插入数据库时​​是否尊重它们的 xml 编码?

关于java - 验证 oracle 的 java 源中的 xml 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7795866/

相关文章:

sql - Oracle中表的列重新排序

java - 重写方法以从索引中删除项目并将索引中所有剩余项目数减去 1

java - 使用模拟测试时无法注入(inject)application.properties中定义的@value

python - 使用 python 获取 youtube 数据

php - 循环遍历 XML,仅在特定 ID 处循环 "look"

c# - asp.net Oracle 调整线程休眠

java - 玩! 2.0 + Spring + MyBatis Java 示例

java - 两个日期字段之间的 Spring 验证?

xml - 选择值小于某个数字的元素

oracle - Saiku 找不到用于 oracle 连接的 JDBC 驱动程序