如何使用 oracle.xml.parser.v2.DOMParser 使用 XSD InputStream 验证 XML
我正在尝试根据 XSD 文档验证 XML 文件。我正在采取两种方法。
XML 和 XSD 都是文件
XML 是一个文件,XSD 是一个流
对于 XML 和XSD 是文件,
validateXML(xmlFile, xsdFile)
{
m_domParser = new DOMParser();
String url = "file:" + new File(xml).getAbsolutePath();
String xsdUrl = "file:" + new File(xsd).getAbsolutePath();
try
{
m_domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
m_domParser.setXMLSchema(xsd);
Validator handler = new Validator();
m_domParser.setErrorHandler(handler);
m_domParser.parse(url);
m_xmlDoc = m_domParser.getDocument();
//determine what kinda utility requested
}
}
它工作正常并且验证正确。这是我编写的用于验证使用 XSD 信息作为流的代码
import org.xml.sax.InputSource;
import oracle.xml.parser.v2.DOMParser;
validateXML(String xmlFile, InputStream is)
Reader reader;
try {
m_domParser = new DOMParser();
m_domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
//get the XMLSchema from the input stream
XSDBuilder builder = null;
XMLSchema schema = null;
builder = new XSDBuilder();
Reader reader = new InputStreamReader(is,"UTF-8");
InputSource iSource = new InputSource(reader);
if(iSource != null) {
iSource.setEncoding("UTF-8");
schema = builder.build(iSource); //NOTE
}
m_domParser.setXMLSchema(schema);
Validator handler = new Validator();
m_domParser.setErrorHandler(handler);
//get the url for the xml file
String url = "file:" + new File(xmlFile).getAbsolutePath();
m_domParser.parse(url);
}
,但在构建期间的注释注释 (schema = builder.build(iSource);) 中,它抛出异常“来自基本类型的无效派生”缺少派生< /strong>”。XSD
流是从同一个 xsd 文件生成的,为什么它在第二个地方失败。在构建 XMLSchema 时,“从基本类型派生无效”是什么意思?
请帮助我理解第二种情况出了什么问题......任何快速反应都将受到高度赞赏。
I am trying to validate an XML file against a XSD document. There are two ways by which I am doing.
Both XML and XSDs are files
XML is a file and XSD is a stream
In the case of XML and XSDs are files,
validateXML(xmlFile, xsdFile)
{
m_domParser = new DOMParser();
String url = "file:" + new File(xml).getAbsolutePath();
String xsdUrl = "file:" + new File(xsd).getAbsolutePath();
try
{
m_domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
m_domParser.setXMLSchema(xsd);
Validator handler = new Validator();
m_domParser.setErrorHandler(handler);
m_domParser.parse(url);
m_xmlDoc = m_domParser.getDocument();
//determine what kinda utility requested
}
}
It works fine and validating correctly. Here is the code I have written for validating using XSD info as stream
import org.xml.sax.InputSource;
import oracle.xml.parser.v2.DOMParser;
validateXML(String xmlFile, InputStream is)
Reader reader;
try {
m_domParser = new DOMParser();
m_domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
//get the XMLSchema from the input stream
XSDBuilder builder = null;
XMLSchema schema = null;
builder = new XSDBuilder();
Reader reader = new InputStreamReader(is,"UTF-8");
InputSource iSource = new InputSource(reader);
if(iSource != null) {
iSource.setEncoding("UTF-8");
schema = builder.build(iSource); //NOTE
}
m_domParser.setXMLSchema(schema);
Validator handler = new Validator();
m_domParser.setErrorHandler(handler);
//get the url for the xml file
String url = "file:" + new File(xmlFile).getAbsolutePath();
m_domParser.parse(url);
}
but at the NOTE comment (schema = builder.build(iSource);) during building it throws out an exception "invalid derivation from base type "missing derivation".
The XSD stream is being generated from the same xsd file why is it failing in the second place. Whilce building the XMLSchema, what is it meant by saying "invalid derivation from the base type"?
Please help me in understanding what went wrong in the second case.. Any quick responses are highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论