我正在使用 UTF-16 编码的 XML 文件。当我尝试使用模式验证来验证此文件时,会引发以下异常:
org.xml.sax.SAXParseException: Content is not allowed in prolog.
at
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.
createSAXParseException(Unknown Source)
我的代码如下。
SchemaFactor schemaFactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
/** load a WXS schema, represented by a Schema instance */
Source schemaFile = new StreamSource(new File("Sample.xsd"));
try {
Schema schema = schemaFactory.newSchema(schemaFile);
javax.xml.validation.Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File("Testing.xml")));
System.out.println("Validation Successs");
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}