我正在尝试针对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);)中,它会抛出异常“从基类型无效派生”缺少派生“。
XSD流是从同一个xsd文件生成的,为什么它在第二个位置失败。建立XMLSchema的意思是什么,说“从基类型无效推导”是什么意思?
请帮助我理解第二种情况下出了什么问题。非常感谢任何快速回复。