我正在生成一些xml文件,需要符合xsd文件给我。我该如何验证它们是否一致?


当前回答

我们使用ant构建我们的项目,所以我们可以使用schemvalidate任务来检查我们的配置文件:

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

现在淘气的配置文件将失败我们的构建!

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

其他回答

还有一个答案:既然您说过需要验证正在生成(写入)的文件,那么您可能希望在写入时验证内容,而不是先写入,然后再读取以进行验证。如果你使用的是基于sax的writer,你也可以使用JDK API进行Xml验证:如果是这样,只需调用' validator '来链接验证器。Validate (source, result)',其中source来自你的作者,result是输出需要去的地方。

或者,如果使用Stax来编写内容(或者使用或可以使用Stax的库),Woodstox也可以在使用XMLStreamWriter时直接支持验证。下面是一篇博客文章,展示了如何做到这一点:

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

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) {}

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

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

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

我最终使用了LiquidXML Studio 2008 (v6),它更容易使用,也更熟悉(UI与我经常使用的Visual Basic 2008 Express非常相似)。缺点:免费版没有验证功能,所以我不得不使用30天的试用期。

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

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

Castor和JAXB是其他Java库,其功能与XMLBeans类似。

对于JAXB,您可以使用下面的代码:

    @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
    }
}