我正在生成一些 xml 文件,这些文件需要符合给我的 xsd 文件。验证它们是否符合要求的最佳方法是什么?

有帮助吗?

解决方案

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 验证 WAR 部署描述符 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd 但您可以轻松地根据本地文件进行验证。

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

其他提示

以下是如何使用 练习2. 。一个关于这个的教程, 这里 (要求。报名)。

原文归属:公然抄袭自 这里:

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 任务来检查我们的配置文件:

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

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

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

由于这是一个流行的问题,我将指出 java 还可以针对“引用”xsd 进行验证,例如,如果 .xml 文件本身在标头中指定了 XSD,则使用 xsi:SchemaLocation 或者 xsi:noNamespaceSchemaLocation (或 xsi 对于特定命名空间) 前任:

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

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

<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 文件中的名称空间匹配,那就好了。但有时能够进行自定义会很方便 解析器...

来自javadoc:“如果您创建一个模式而不指定 URL、文件或源,那么 Java 语言会创建一个在正在验证的文档中查找它应该使用的模式的模式。例如:”

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

这适用于多个命名空间等。这种方法的问题在于 xmlsns:xsi 可能是一个网络位置,因此默认情况下它会在每次验证时都会出去并访问网络,这并不总是最佳的。

下面是一个根据它引用的任何 XSD 验证 XML 文件的示例(即使它必须从网络中提取它们):

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

即使 xml 文件引用了 url,您也可以通过手动指定 xsd(请参阅此处的其他一些答案)或使用“XML 目录”来避免从网络中提取引用的 XSD 样式解析器. 。春天显然也 可以拦截 URL 请求提供本地文件以进行验证。或者您可以通过设置自己的 设置资源解析器, , 前任:

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,您可以按照中提供的文档进行操作 包装说明.

// 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。我发现这非常有用。

SAXCount -f -s -n my.xml

它根据 dtd 和 xsd 进行验证。50MB 文件需要 5 秒。

在 debian scrap 中,它位于“libxerces-c-samples”包中。

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

还有一个答案:既然你说你需要验证你的文件 生成 (写作),您可能希望在写作时验证内容,而不是先写,然后读回进行验证。如果您使用基于 SAX 的编写器,您可能可以使用用于 Xml 验证的 JDK API 来做到这一点:如果是这样,只需通过调用“Validator.validate(source, result)”链接到验证器,其中源来自您的编写器,结果是输出需要去的地方。

或者,如果您使用 Stax 来编写内容(或使用或可以使用 Stax 的库), 伍德斯托克斯 使用XMLStreamWriter时还可以直接支持验证。这是一个 博客条目 展示这是如何完成的:

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

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

蓖麻联合航空航天局 是与 XMLBeans 具有类似用途的其他 Java 库。

您在寻找工具或库吗?

就图书馆而言,事实上的标准几乎是 练习2 两者都有 C++爪哇 版本。

但请注意,这是一个重量级解决方案。但话又说回来,根据 XSD 文件验证 XML 是一个相当繁重的问题。

至于为您执行此操作的工具, XML福克斯 似乎是一个不错的免费软件解决方案,但我个人没有使用过它,所以不能肯定。

对于 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
    }
}

根据在线模式进行验证

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 验证

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

我最终使用了 LiquidXML Studio 2008 (v6),它更容易使用并且更容易熟悉(UI 与我经常使用的 Visual Basic 2008 Express 非常相似)。缺点:免费版本不提供验证功能,因此我必须使用 30 天的试用版。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top