在Oracle的Java源代码中验证xml文档 [英] validating xml document in oracle's java source
问题描述
试图做主题.
我正在尝试使用文件(schemasource = 1)和clob(schemasource = 0)中的xsd. 我有两个xsd模式common_types.xsd和migom.xsd.第二包括第一. 问题是,当我使用文件中的common_types模式时,出现错误
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调用因未捕获的Java异常而终止:oracle.xml.parser.v2.XMLParseException:发生内部错误情况.
ORA-29532: Java call terminated by uncaught Java exception: oracle.xml.parser.v2.XMLParseException: An internal error condition occurred.
并且当我仅从Clob中读取了针对第一个架构的xml验证时,我获得了成功,但是当我添加第二个xsd时,出现了相同的错误,什么也没说.
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);
}
}
}
从java stacktrace获取的其他信息:
additional information got from 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
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
我的oracle版本是Oracle Database 10g企业版10.2.0.1.0版-产品 但是我的目的是使其在从9开始的所有版本上都可以使用
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
推荐答案
根据您的堆栈跟踪,我看到正在使用NonValidatingParser.即使您没有提到这是一个问题,这也是出乎意料的.我确实知道xmlparserv2有一个验证解析器,所以我看了xmlparserv2.jar中反编译的XMLParser(自从我在OC4J上工作以来就随身带了它.)
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).
反编译的源代码如下.如您所见,构造函数默认情况下采用非验证解析器.使用setProperty应该将其切换到ValidatingParser,但是由于那没有发生.
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();
}
在反编译的代码中找不到setProperty方法.那是不寻常的,但我还没有研究.为了启用xml验证,我相信您将不得不使用其他API方法.我相信setAttribute方法会做您想要的.
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.
公共无效setAttribute(String s,Object obj)抛出IllegalArgumentException {
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);
}
我已经在OC4J上部署的应用程序中使用了它,我知道它正在使用相同的解析器. 代码示例如下所示
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工厂= DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setNamespaceAware(true);
factory.setValidating(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/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", ClassUtil.getResourceAsStream(schemaSourceLocation));
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", ClassUtil.getResourceAsStream(schemaSourceLocation));
希望这会有所帮助.
这篇关于在Oracle的Java源代码中验证xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!