问题描述
我已经通过Maven安装了Xerces:
I have installed Xerces through Maven:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
然后,我从,根据1.1版中的模式验证XML文件。这是我的代码:
I then tried the code given in this example from the Xerces FAQ to validate a XML file against a schema in version 1.1. This is my code:
private static void validateFile(File xmlFile, File xsdFile) throws SAXException, IOException
{
// 1. Lookup a factory for the W3C XML Schema language
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
// 2. Compile the schema.
File schemaLocation = xsdFile;
Schema schema = factory.newSchema(schemaLocation);
// 3. Get a validator from the schema.
Validator validator = schema.newValidator();
// 4. Parse the document you want to check.
Source source = new StreamSource(xmlFile);
// 5. Check the document
try
{
validator.validate(source);
System.out.println(xmlFile.getName() + " is valid.");
}
catch (SAXException ex)
{
System.out.println(xmlFile.getName() + " is not valid because ");
System.out.println(ex.getMessage());
}
}
代码只会产生此异常:
java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://www.w3.org/XML/XMLSchema/v1.1 could be loaded
at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:204)
at example.xml.XSDValidator.validateFile(XSDValidator.java:65)
似乎我未能正确配置/安装Xerces。请帮我搞定,XML文件强迫我使用1.1中的模式,我有一个正常的1.0运行验证器,但我有很大的问题。我很欣赏每一个提示!
Seems like I failed to configure/install Xerces correctly. Please help me get this working, the XML files force me to use the schema in 1.1, I got a normal validator for 1.0 running but I have huge problems with this. I appreciate every hint!
推荐答案
看起来你需要Xerces2 Java 2.11.0(XML Schema 1.1)(Beta)版本,它不在maven存储库中。您可以从网站下载它,并将其安装到您当地的maven存储库:
mvn install:install-file -Dfile = xercesImpl.jar -DgroupId = xerces -DartifactId = xercesImpl -Dversion = 2.11.0.beta -Dpackaging = jar
那么你将能够将它包含在您的Maven项目依赖项中:
It looks that you need Xerces2 Java 2.11.0 (XML Schema 1.1) (Beta) version, which isn't in maven repository. You can download it from Xerces website, and install it to your local maven repository:mvn install:install-file -Dfile=xercesImpl.jar -DgroupId=xerces -DartifactId=xercesImpl -Dversion=2.11.0.beta -Dpackaging=jar
Then you will be able to include it in your Maven project dependencies:
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0.beta</version>
</dependency>
这篇关于使用Java中的Xerces对XSD 1.1进行XML验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!