stax xml 验证

发布于 2024-11-04 01:54:20 字数 54 浏览 0 评论 0原文

我知道当我使用 sax 时我可以验证 xml 文件。但是我可以在使用 Stax 时进行验证吗?

I know I can validate xml-file when I use sax. But can I validate when I use Stax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过度放纵 2024-11-11 01:54:20

使用 SAX 和 DOM 可以通过两种方式进行 XML 验证:

  1. 单独验证 - 通过 Validator.validate()
  2. 在解析期间验证 - 通过 >DocumentBuilderFactory.setSchema()SAXParserFactory.setSchema()

使用 StAX,验证是可能的,但这只是第一种方法。

你可以尝试这样的事情:

import javax.xml.validation.*;
import javax.xml.transform.stax.*;
import javax.xml.stream.*;
import javax.xml.*;
import java.io.*;

public class StaxValidation {

    public static void main (String args[]) throws Exception {

        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream("test.xml"));

        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File("test.xsd"));

        Validator validator = schema.newValidator();
        validator.validate(new StAXSource(reader));

        //no exception thrown, so valid
        System.out.println("Document is valid");

    }
}

There are two ways of XML validation possible with SAX and DOM:

  1. validate alone - via Validator.validate()
  2. validate during parsing - via DocumentBuilderFactory.setSchema() and SAXParserFactory.setSchema()

With StAX, validation is possible, but only the first way of doing it.

You can try something like this:

import javax.xml.validation.*;
import javax.xml.transform.stax.*;
import javax.xml.stream.*;
import javax.xml.*;
import java.io.*;

public class StaxValidation {

    public static void main (String args[]) throws Exception {

        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream("test.xml"));

        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File("test.xsd"));

        Validator validator = schema.newValidator();
        validator.validate(new StAXSource(reader));

        //no exception thrown, so valid
        System.out.println("Document is valid");

    }
}
静待花开 2024-11-11 01:54:20

您可以使用 StAX 一次性解析和验证。使用 javax.xml.stream.util.StreamReaderDelegate:

 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream  ("test.xml"));

 reader = new StreamReaderDelegate(reader) {
     public int next() throws XMLStreamException {
          int n = super.next();

          // process event

          return n;
     }};

 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 Schema schema = factory.newSchema(new File("test.xsd"));
 Validator validator = schema.newValidator();
 validator.validate(new StAXSource(reader));

验证器调用 reader.next() 读取 test.xml,并且您照常处理解析事件。

You can parse and validate with StAX in one pass. Use javax.xml.stream.util.StreamReaderDelegate:

 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream  ("test.xml"));

 reader = new StreamReaderDelegate(reader) {
     public int next() throws XMLStreamException {
          int n = super.next();

          // process event

          return n;
     }};

 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 Schema schema = factory.newSchema(new File("test.xsd"));
 Validator validator = schema.newValidator();
 validator.validate(new StAXSource(reader));

Validator reads test.xml calling reader.next() and you process parsing events as usual.

小嗲 2024-11-11 01:54:20

没有标准方法可以做到这一点。不过,有一个名为 StAX2 的 API 扩展,它支持使用 Sun 的 MSV(多模式验证)进行验证。我建议使用 Woodstox StAX2 实现。

http://woodstox.codehaus.org/

There is no standard way to do this. However, there's an API extension called StAX2 which support validation using Sun's MSV (multi schema validation). I would recommend to use the Woodstox StAX2 implementation.

http://woodstox.codehaus.org/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文