小编Vog*_*ltz的帖子

如何在Java中针对XSD 1.1验证XML?

在Java中针对XML Schema 1.1验证XML文件的最佳方法是什么?

我从本教程中获取了代码并更改了它在工厂中查找的行以使用XML Schema 1.1,正如我在Xerces FAQ的代码示例中看到的那样.

这是我的代码:

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 …
Run Code Online (Sandbox Code Playgroud)

java xml validation xsd xsd-1.1

16
推荐指数
2
解决办法
1万
查看次数

使用Java中的Xerces对XSD 1.1进行XML验证

我已经通过Maven安装了Xerces:

<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>
Run Code Online (Sandbox Code Playgroud)

然后,我在Xerces FAQ中尝试了此示例中给出的代码,以针对1.1版中的模式验证XML文件.这是我的代码:

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 …
Run Code Online (Sandbox Code Playgroud)

java xml validation xerces xsd-1.1

9
推荐指数
2
解决办法
8244
查看次数

标签 统计

java ×2

validation ×2

xml ×2

xsd-1.1 ×2

xerces ×1

xsd ×1