如何在 Java 中针对 XSD 1.1 验证 XML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20807066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate XML against XSD 1.1 in Java?
提问by Vogon Jeltz
What is the best way to validate XML files against XML Schema 1.1 in Java?
在 Java 中针对 XML Schema 1.1 验证 XML 文件的最佳方法是什么?
I took the code from this tutorialand changed the line where it looks up the factory to use XML Schema 1.1 as I have seen in this code example from the Xerces FAQ.
我从本教程中获取了代码,并将查找工厂的行更改为使用 XML Schema 1.1,正如我在Xerces FAQ 中的代码示例中看到的那样。
This is my code:
这是我的代码:
import java.io.File;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
public class XSDValidator {
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");
// SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// 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());
}
}
}
The code throws this exception:
代码抛出这个异常:
java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://www.w3.org/XML/XMLSchema/v1.1 could be loaded
As I see it I have exactly the same imports as the code snippet in the Xerces FAQ. I even tried to add Xerces to my Maven dependencies but that didn't help either.
在我看来,我的导入与 Xerces 常见问题解答中的代码片段完全相同。我什至尝试将 Xerces 添加到我的 Maven 依赖项中,但这也无济于事。
Cheers :)
干杯:)
采纳答案by Michael Kay
I don't think you can use the JAXP service mechanism to search for an XSD 1.1 processor. Load Saxon or Xerces in the normal way, and then enable XSD 1.1 processing. For Saxon this is done using
我认为您不能使用 JAXP 服务机制来搜索 XSD 1.1 处理器。以正常方式加载 Saxon 或 Xerces,然后启用 XSD 1.1 处理。对于撒克逊人,这是使用
SchemaFactory.setProperty("http://saxon.sf.net/feature/xsd-version", "1.1")
回答by Mustafa
Unfortunately, neither the JDK bundled version (as of Java 8) nor the latest official version from maven central (2.11.0) contains XSD 1.1 implementation.
不幸的是,JDK 捆绑版本(从 Java 8 开始)和 maven central (2.11.0) 的最新官方版本都不包含 XSD 1.1 实现。
You actually need the 2.11.0-xml-schema-1.1-beta
version of Xerces to able to run the example in the FAQ you have linked.
您实际上需要2.11.0-xml-schema-1.1-beta
Xerces 版本才能运行您链接的常见问题解答中的示例。
You may do one of the following.
您可以执行以下操作之一。
Download the
Xerces2 Java 2.11.0 (XML Schema 1.1) (Beta)
binaries from Xerces website and manually add jars to the classpath (or install locally via Maven). Link: http://xerces.apache.org/mirrors.cgi. You need at least the following:cupv10k-runtime.jar org.eclipse.wst.xml.xpath2.processor_1.1.0.jar xercesImpl.jar xml-apis.jar
Use the following unofficial maven dependency.
<dependency> <groupId>org.opengis.cite.xerces</groupId> <artifactId>xercesImpl-xsd11</artifactId> <version>2.12-beta-r1667115</version> </dependency>
Xerces2 Java 2.11.0 (XML Schema 1.1) (Beta)
从 Xerces 网站下载二进制文件并手动将 jar 添加到类路径(或通过 Maven 在本地安装)。链接:http: //xerces.apache.org/mirrors.cgi。您至少需要以下内容:cupv10k-runtime.jar org.eclipse.wst.xml.xpath2.processor_1.1.0.jar xercesImpl.jar xml-apis.jar
使用以下非官方的 maven 依赖项。
<dependency> <groupId>org.opengis.cite.xerces</groupId> <artifactId>xercesImpl-xsd11</artifactId> <version>2.12-beta-r1667115</version> </dependency>
回答by Seva Safris
There is a generic XML validator that works with XML Schema v1.1, which uses xercesImpl-xsd11
2.12-beta-r1667115
. The validator is available here as a Maven plugin, and here as an embeddable library.
有一个适用于 XML Schema v1.1 的通用 XML 验证器,它使用xercesImpl-xsd11
2.12-beta-r1667115
. 验证器可在此处作为 Maven 插件使用,在此处作为可嵌入库使用。