关于Java:针对XSD文件验证XML文件的最佳方法是什么?

关于Java:针对XSD文件验证XML文件的最佳方法是什么?

What's the best way to validate an XML file against an XSD file?

我正在生成一些需要符合给我的xsd文件的xml文件。 验证其符合性的最佳方法是什么?


Java运行时库支持验证。上次我检查的是幕后的Apache Xerces解析器。您可能应该使用javax.xml.validation.Validator。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import java.net.URL;
import org.xml.sax.SAXException;
//import java.io.File; // if you use File
import java.io.IOException;
...
URL schemaFile = new URL("http://host:port/filename.xsd");
// webapp example xsd:
// URL schemaFile = new URL("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd");
// local file example:
// File schemaFile = new File("/location/to/localfile.xsd"); // etc.
Source xmlFile = new StreamSource(new File("web.xml"));
SchemaFactory schemaFactory = SchemaFactory
    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
  Schema schema = schemaFactory.newSchema(schemaFile);
  Validator validator = schema.newValidator();
  validator.validate(xmlFile);
  System.out.println(xmlFile.getSystemId() +" is valid");
} catch (SAXException e) {
  System.out.println(xmlFile.getSystemId() +" is NOT valid reason:" + e);
} catch (IOException e) {}

架构工厂常量是定义XSD的字符串http://www.w3.org/2001/XMLSchema。上面的代码针对URL http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd验证了WAR部署描述符,但是您也可以轻松针对本地文件进行验证。

您不应使用DOMParser来验证文档(除非您的目标仍然是创建文档对象模型)。这将在解析文档时开始创建DOM对象-如果您不打算使用它们,那将很浪费。


这是使用Xerces2的方法。此处的教程(要求注册)。

原始出处:从此处公然复制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.apache.xerces.parsers.DOMParser;
import java.io.File;
import org.w3c.dom.Document;

public class SchemaTest {
  public static void main (String args[]) {
      File docFile = new File("memory.xml");
      try {
        DOMParser parser = new DOMParser();
        parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setProperty(
            "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
            "memory.xsd");
        ErrorChecker errors = new ErrorChecker();
        parser.setErrorHandler(errors);
        parser.parse("memory.xml");
     } catch (Exception e) {
        System.out.print("Problem parsing the file.");
     }
  }
}


我们使用ant构建项目,因此可以使用schemavalidate任务来检查配置文件:

1
2
3
<schemavalidate>
    <fileset dir="${configdir}" includes="**/*.xml" />
</schemavalidate>

现在,顽皮的配置文件将使我们的构建失败!

http://ant.apache.org/manual/Tasks/schemavalidate.html


由于这是一个很普遍的问题,我将指出java也可以针对"所引用的" xsd进行验证,例如,如果.xml文件本身在标头中使用xsi:SchemaLocationxsi:noNamespaceSchemaLocation(或xsi来指定)指定XSD命名空间)例如:

1
2
3
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="http://www.example.com/document.xsd">
  ...

或SchemaLocation(始终是名称空间到xsd映射的列表)

1
2
3
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:SchemaLocation="http://www.example.com/my_namespace http://www.example.com/document.xsd">
  ...

其他答案在这里也起作用,因为.xsd文件"映射"到.xml文件中声明的名称空间,因为它们声明了名称空间,并且如果与.xml文件中的名称空间匹配,那就很好。但是有时候拥有自定义解析器会很方便...

在javadocs中:"如果创建模式时未指定URL,文件或源,那么Java语言会创建一种模式,该模式在正在验证的文档中查找以找到其应使用的模式。例如:"

1
2
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema();

这适用于多个名称空间等。
这种方法的问题在于xmlsns:xsi可能是网络位置,因此默认情况下它将通过每次验证出门并进入网络,而并非总是最佳状态。

这是一个示例,该示例针对XML文件所引用的任何XSD(即使必须将其从网络中拉出)也进行了验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  public static void verifyValidatesInternalXsd(String filename) throws Exception {
    InputStream xmlStream = new new FileInputStream(filename);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                "http://www.w3.org/2001/XMLSchema");
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new RaiseOnErrorHandler());
    builder.parse(new InputSource(xmlStream));
    xmlStream.close();
  }

  public static class RaiseOnErrorHandler implements ErrorHandler {
    public void warning(SAXParseException e) throws SAXException {
      throw new RuntimeException(e);
    }
    public void error(SAXParseException e) throws SAXException {
      throw new RuntimeException(e);
    }
    public void fatalError(SAXParseException e) throws SAXException {
      throw new RuntimeException(e);
    }
  }

通过手动指定xsd(请参阅此处的其他答案)或使用" XML目录"样式的解析器,即使xml文件引用的是url,也可以避免从网络中提取引用的XSD。 Spring显然也??可以拦截URL请求以提供本地文件进行验证。或者,您可以通过setResourceResolver设置自己的属性,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Source xmlFile = new StreamSource(xmlFileLocation);
SchemaFactory schemaFactory = SchemaFactory
                                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema();
Validator validator = schema.newValidator();
validator.setResourceResolver(new LSResourceResolver() {
  @Override
  public LSInput resolveResource(String type, String namespaceURI,
                                 String publicId, String systemId, String baseURI) {
    InputSource is = new InputSource(
                           getClass().getResourceAsStream(
                         "some_local_file_in_the_jar.xsd"));
                          // or lookup by URI, etc...
    return new Input(is); // for class Input see
                          // https://stackoverflow.com/a/2342859/32453
  }
});
validator.validate(xmlFile);

另请参见此处以获取其他教程。

我相信默认值是使用DOM解析,您可以使用与SAX解析器相似的方法进行验证,同时也可以进行saxReader.setEntityResolver(your_resolver_here);


使用Java 7,您可以遵循软件包说明中提供的文档。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("instance.xml"));

// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(new File("mySchema.xsd"));
Schema schema = factory.newSchema(schemaFile);

// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();

// validate the DOM tree
try {
    validator.validate(new DOMSource(document));
} catch (SAXException e) {
    // instance document is invalid!
}

如果您拥有Linux机器,则可以使用免费的命令行工具SAXCount。我觉得这很有用。

1
SAXCount -f -s -n my.xml

它针对dtd和xsd进行验证。
50MB文件5秒。

在debian squeeze中,它位于软件包" libxerces-c-samples"中。

dtd和xsd的定义必须在xml中!您不能单独配置它们。


另一个答案:由于您说需要验证正在生成(写入)的文件,因此您可能想在写入时验证内容,而不是先写入然后再读回以进行验证。如果您使用基于SAX的writer,则可以使用JDK API进行Xml验证来实现此目的:如果是这样,只需调用" Validator.validate(source,result)"链接验证器,其中source来自您的writer,结果是需要输出的地方。

另外,如果您使用Stax编写内容(或使用或可以使用stax的库),则在使用XMLStreamWriter时,Woodstox还可以直接支持验证。这是一个博客文章,显示了如何完成此操作:


如果要以编程方式生成XML文件,则可能需要查看XMLBeans库。使用命令行工具,XMLBeans将基于XSD自动生成并打包一组Java对象。然后,您可以使用这些对象基于此架构构建XML文档。

它具有对模式验证的内置支持,并且可以将Java对象转换为XML文档,反之亦然。

Castor和JAXB是其他Java库,其目的类似于XMLBeans。


使用Woodstox,配置StAX解析器以针对您的架构进行验证并解析XML。

如果捕获到异常,那么XML无效,否则有效:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// create the XSD schema from your schema file
XMLValidationSchemaFactory schemaFactory = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
XMLValidationSchema validationSchema = schemaFactory.createSchema(schemaInputStream);

// create the XML reader for your XML file
WstxInputFactory inputFactory = new WstxInputFactory();
XMLStreamReader2 xmlReader = (XMLStreamReader2) inputFactory.createXMLStreamReader(xmlInputStream);

try {
    // configure the reader to validate against the schema
    xmlReader.validateAgainst(validationSchema);

    // parse the XML
    while (xmlReader.hasNext()) {
        xmlReader.next();
    }

    // no exceptions, the XML is valid

} catch (XMLStreamException e) {

    // exceptions, the XML is not valid

} finally {
    xmlReader.close();
}

注意:如果需要验证多个文件,则应尝试重用XMLInputFactoryXMLValidationSchema,以使性能最大化。


根据在线模式进行验证

1
2
3
4
5
Source xmlFile = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("your.xml"));
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(Thread.currentThread().getContextClassLoader().getResource("your.xsd"));
Validator validator = schema.newValidator();
validator.validate(xmlFile);

根据本地架构进行验证

Java的离线XML验证


使用JAXB,您可以使用以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    @Test
public void testCheckXmlIsValidAgainstSchema() {
    logger.info("Validating an XML file against the latest schema...");

    MyValidationEventCollector vec = new MyValidationEventCollector();

    validateXmlAgainstSchema(vec, inputXmlFileName, inputXmlSchemaName, inputXmlRootClass);

    assertThat(vec.getValidationErrors().isEmpty(), is(expectedValidationResult));
}

private void validateXmlAgainstSchema(final MyValidationEventCollector vec, final String xmlFileName, final String xsdSchemaName, final Class< ? > rootClass) {
    try (InputStream xmlFileIs = Thread.currentThread().getContextClassLoader().getResourceAsStream(xmlFileName);) {
        final JAXBContext jContext = JAXBContext.newInstance(rootClass);
        // Unmarshal the data from InputStream
        final Unmarshaller unmarshaller = jContext.createUnmarshaller();

        final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final InputStream schemaAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(xsdSchemaName);
        unmarshaller.setSchema(sf.newSchema(new StreamSource(schemaAsStream)));

        unmarshaller.setEventHandler(vec);

        unmarshaller.unmarshal(new StreamSource(xmlFileIs), rootClass).getValue(); // The Document class is the root object in the XML file you want to validate

        for (String validationError : vec.getValidationErrors()) {
            logger.trace(validationError);
        }
    } catch (final Exception e) {
        logger.error("The validation of the XML file" + xmlFileName +" failed:", e);
    }
}

class MyValidationEventCollector implements ValidationEventHandler {
    private final List<String> validationErrors;

    public MyValidationEventCollector() {
        validationErrors = new ArrayList<>();
    }

    public List<String> getValidationErrors() {
        return Collections.unmodifiableList(validationErrors);
    }

    @Override
    public boolean handleEvent(final ValidationEvent event) {
        String pattern ="line {0}, column {1}, error message {2}";
        String errorMessage = MessageFormat.format(pattern, event.getLocator().getLineNumber(), event.getLocator().getColumnNumber(),
                event.getMessage());
        if (event.getSeverity() == ValidationEvent.FATAL_ERROR) {
            validationErrors.add(errorMessage);
        }
        return true; // you collect the validation errors in a List and handle them later
    }
}


您在寻找工具还是库?

就库而言,事实上的标准是Xerces2,它同时具有C ++和Java版本。

不过请注意,这是一个沉重的解决方案。但是话又说回来,针对XSD文件验证XML是一个相当沉重的问题。

至于为您执行此操作的工具,XMLFox似乎是一个不错的免费软件解决方案,但我不能肯定地说自己没有亲自使用过它。


我只需要一次针对XSD验证XML,所以尝试了XMLFox。我发现它非常令人困惑和奇怪。帮助说明似乎与界面不匹配。

我最终使用了LiquidXML Studio 2008(v6),它更易于使用并且更加直接(UI与我经常使用的Visual Basic 2008 Express非常相似)。缺点:验证功能不是免费版本,因此我不得不使用30天试用版。


推荐阅读