验证oracle的java源中的xml文档

发布于 2024-12-10 05:50:01 字数 5115 浏览 0 评论 0原文

正在努力做主题。

我正在尝试使用文件(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 开始的所有版本

Trying to do subject.

I'm trying to use xsd from file(schemasource = 1) and from clob (schemasource = 0).
I have two xsd schemas common_types.xsd and migom.xsd. second includes first.
The problem is that when I'm using common_types schema from file I get error

ORA-29532: Java call terminated by uncaught Java exception: oracle.xml.parser.v2.XMLParseException: An internal error condition occurred.

and when I validate xml against only first schema has being read from clob I get success, but when I add second xsd, i get the same error, which says nothing at all.

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);    
    }           


    }

}

additional information got from java stacktrace:

file name: XMLError.java, class name: oracle.xml.parser.v2.XMLError, method name: flushErrors1, line : 320
file name: NonValidatingParser.java, class name: oracle.xml.parser.v2.NonValidatingParser, method name: parseDocument, line : 300
file name: XMLParser.java, class name: oracle.xml.parser.v2.XMLParser, method name: parse, line : 200
file name: XMLTOOLS, class name: XmlValidator, method name: ValidateDocument, line : 86

my oracle version is Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
But my aim is to make it work on all versions starting from 9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春花秋月 2024-12-17 05:50:01

更新:

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

UPDATE:

so, you loaded these files into the database as CLOBs. did you respect their xml encoding when you inserted them into the database?

牵你手 2024-12-17 05:50:01

根据您的堆栈跟踪,我看到正在使用 NonValidatingParser。尽管你没有提到这个问题,但这还是出乎意料的。我确实知道 xmlparserv2 有一个验证解析器,因此我查看了 xmlparserv2.jar 中反编译的 XMLParser(自从我使用 OC4J 以来,我一直带着它)。

反编译后的源码如下。正如您所看到的,构造函数默认假设一个非验证解析器。使用 setProperty 应该将其切换到 ValidatingParser 但因为这没有发生。

XMLParser()
{
    解析器 = new NonValidatingParser();
}

我在反编译的代码中找不到setProperty方法。这很不寻常,但我还没有研究过。要启用 xml 验证,我相信您必须使用不同的 API 方法。我相信 setAttribute 方法会做你想要的。

public void setAttribute(String s, Object obj) 抛出 IllegalArgumentException
{

<前><代码>............
if(s == "http://java.sun.com/xml/jaxp/properties/schemaSource")
schemaSource = obj;
别的
if(s == "http://java.sun.com/xml/jaxp/properties/schemaLanguage")
{
if(((String)obj).equals("http://www.w3.org/2001/XMLSchema"))
设置验证模式(3);
getSchemaValidator().setJAXP(true);
}
......................
属性.put(s, obj);
}

我已经在部署在 OC4J 上的应用程序中使用了它,我知道它正在使用相同的解析器。
代码示例如下所示

DocumentBuilderFactory 工厂 = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);

factory.setValidating(true);

factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
“http://www.w3.org/2001/XMLSchema”);

factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
ClassUtil.getResourceAsStream(schemaSourceLocation));

希望这有帮助。

As per your stacktrace, I saw that the NonValidatingParser was being used. Even though you had not mentioned that as an issue, it was unexpected. I do know that xmlparserv2 has a validating parser, so I had a look at the decompiled XMLParser in the xmlparserv2.jar (I have it with me since I work on OC4J).

The decompiled source is below. As you can see, the constructor assumes a non validation parser by default. The use of setProperty should switch it to a ValidatingParser but since that is not happening.

XMLParser()
{
    parser = new NonValidatingParser();
}

I could not find a setProperty method in the decompiled code. That is unusual but I haven't looked into it. To enable the xml validation I believe you will have to use a different API method. I believe the setAttribute method will do what you want.

public void setAttribute(String s, Object obj) throws IllegalArgumentException
{

    ............
    if(s == "http://java.sun.com/xml/jaxp/properties/schemaSource")
        schemaSource = obj;
    else
    if(s == "http://java.sun.com/xml/jaxp/properties/schemaLanguage")
    {
        if(((String)obj).equals("http://www.w3.org/2001/XMLSchema"))
            setValidationMode(3);
        getSchemaValidator().setJAXP(true);
    }
    .......................
    attributes.put(s, obj);
}

I have used it in an application deployed on OC4J which I know is using the same parser.
The code example is shown below

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);

factory.setValidating(true);

factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");

factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
ClassUtil.getResourceAsStream(schemaSourceLocation));

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文