简体   繁体   English

如何在Xerces中使用语法验证文档

[英]How to validate a document using a grammar in Xerces

I have following situation 我有以下情况
- I create XML-documents (DocumentImpl) on the fly (using data). -我即时创建XML文档(DocumentImpl)(使用数据)。 So the XML is never written to disc. 因此,XML永远不会写入磁盘。
- I create XSD-schemas on the fly (using data-definitions), these also are never written to disc. -我即时创建XSD方案(使用数据定义),这些也从未写入磁盘。 The grammars are complex with assertions, so they need to be used as XMLSchema v1.1 语法带有断言很复杂,因此需要将其用作XMLSchema v1.1
- I store the grammars (SchemaGrammar) from the XSD-schemas in a hashmap, this is because the same grammars are often used more times. -我将XSD方案中的语法(SchemaGrammar)存储在哈希图中,这是因为相同的语法经常使用多次。

Now my question, 现在我的问题是

I want to validate the documents against a grammar. 我想根据语法验证文档。 I know which grammar to use. 我知道该使用哪种语法。 They are found by the according data-definition-name. 通过相应的data-definition-name找到它们。

My problem is that I cannot find example code how to do this, because all the examples seem to work from streams or files, while I have the objects ready. 我的问题是我找不到如何执行此操作的示例代码,因为在准备好对象的同时,所有示例似乎都可以在流或文件中运行。

I think, it works like this: 我认为它的工作方式如下:

` `

XMLGrammarPoolImpl pool = new XMLGrammarPoolImpl();
pool.putGrammar(grammar);

XMLSchema11Factory factory = new XMLSchema11Factory();
Schema schema = factory.newSchema(pool);

Validator validator = schema.newValidator();

DOMSource source = new DOMSource(document);
validator.validate(source);

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM