如何使用RELAX NG模式和JAXP验证XML文档? [英] How to validate an XML document using a RELAX NG schema and JAXP?

查看:108
本文介绍了如何使用RELAX NG模式和JAXP验证XML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用RELAX NG schemata验证XML文档,我想使用 JAXP验证API



从谷歌搜索,似乎我可以使用 Jing ISO RELAX JARV到JAXP Bridge 。不幸的是,在将两者都添加到我的类路径后,我无法让它工作。 SchemaFactory 只要它试图实例化一个工厂就抛出 IllegalArgumentException - 我看着里面 SchemaFactory ,显然 SchemaFactoryFinder 返回null结果。



所以我想感谢这两个问题的答案:




  • 如何使用Jing和这座桥来完成这项工作?

  • 我应该尝试更好/不同的库集吗?



我需要这个库来使用Java 5和Java 6。 / p>

谢谢!

解决方案

参见Stefan Bodewig在3月7日写的博客, 2008年题为 XMLUnit中的RELAX NG验证


自昨晚以来,XMLUnit的trunk包含一个新的Validator类,它基于javax.xml.validation,它是JAXP 1.3的一部分(i .e.Java5 +)。


...


据我所知,没有JAXP实现支持RELAX NG开箱即用。 Sun自己的JAXP 1.4(Java6 +)肯定没有。一些搜索带我到 Kohsuke Kawaguchi的博客,应该知道,因为他在JAXP上的工作,Sun的多模式验证器,isorelax和其他东西。



使用他的 isorelax-bridge Jing 没有让我在Java6上的任何地方。我回到了Kohsuke Kawaguchi的文章并阅读了评论:由于他们更改了SchemaFactory查找算法,因此桥不能与Java6一起使用。好了,尝试了Java5 - 进步,我现在在Jing内部的某个地方得到一个NullPointerException,所以至少它正在加载工厂。接下来我用MSV取代了Jing(现在是这里,无论有多少链接引导你到Sun的WebServices堆栈页面,以及好的URL永远不会改变),实际上,我的简单测试通过了。



所以你可能不得不跳过一些箍到在你的JAXP设置中获得RELAX NG支持 - 在我的例子中,Java5,MSV和Kawaguchi的桥接工作,但是注释表明它应该也可以用Java6完成 - 但是一旦你设法正确配置了所有内容,XMLUnit现在就可以让你在单元测试中断言您的文档的有效性。但它似乎不适用于紧凑语法。


要阅读Kohsuke Kawaguchi博客上的评论,你必须使用存档.org 因为它们现在都不见了:


Java 5将服务提供商
文件解释为列表键值/值对,
这违反了Java 5& 6
JAR文件规范,但碰巧
符合您的示例。



Java 6按指定解析服务提供者
文件,即。作为
完全限定类名的列表,但因此
无法实例化适配器的
SchemaFactory,因为服务提供者
文件的内容无效。



要兼容Java 5和
Java 6而不必更改
JAXP-JARV适配器JAR文件,可以只需添加另一个JAR文件

a更正
javax.xml.validation.SchemaFactory
服务提供商文件。



I would like to validate XML documents using RELAX NG schemata, and I would like to use the JAXP validation API.

From Googling around, it appeared that I could use Jing and the ISO RELAX JARV to JAXP Bridge. Unfortunately, after adding both to my classpath, I can't get it to work. SchemaFactory is just throwing an IllegalArgumentException as soon as it tries to instantiate a factory — I looked inside SchemaFactory, apparently SchemaFactoryFinder is returning a null result.

So I'd appreciate answers to either question:

  • How can I make this work with Jing and this bridge?
  • Is there a better/different set of libraries I should try?

I need this to work with Java 5 and Java 6.

Thanks!

解决方案

See Stefan Bodewig's Weblog written on March 7, 2008 titled RELAX NG Validation in XMLUnit:

Since last night XMLUnit's trunk contains a new Validator class that is based on javax.xml.validation which is part of JAXP 1.3 (i.e. Java5+).

...

To the best of my knowledge there is no JAXP implementation that supported RELAX NG out of the box. Sun's own JAXP 1.4 (Java6+) certainly doesn't. Some searching around brought me to Kohsuke Kawaguchi's Blog who should know, given his work on JAXP, Sun's Multi Schema Validator, isorelax and other stuff.

Using his isorelax-bridge and Jing didn't get me anywhere on Java6. I went back to Kohsuke Kawaguchi's article and read the comments: the bridge doesn't work with Java6 since they changed the SchemaFactory lookup algorithm. OK, tried Java5 instead - progress, I now get a NullPointerException somewhere inside of Jing, so at least it is loading the factory. Next I replaced Jing with MSV (which is here now, no matter how many links out there lead you to the WebServices stack page at Sun, so much for "good URLs never change") and really, my simplistic tests pass.

So you may have to jump through some hoops to get RELAX NG support into your JAXP setup - in my case Java5, MSV and Kawaguchi's bridge worked, but the comments indicate it should be doable with Java6 as well - but once you manage to configure everything correctly, XMLUnit will now be there to let you assert your document's validity in Unit tests. It seems that it doesn't work for compact syntax, though.

To read the comments on Kohsuke Kawaguchi's blog, you have to use archive.org because somehow they are all gone now:

Java 5 interprets the Service Provider file as a list of key/value pairs, which is a violation to the Java 5 & 6 JAR file specification but happens to match your example.

Java 6 parses the Service Provider file as specified, ie. as a list of fully qualified class names, but thus fails to instantiate your adapter's SchemaFactory as the Service Provider file's contents are invalid.

To be compatible with both Java 5 and Java 6 without having to change the JAXP-JARV-adapter JAR file, one can simply add another JAR file containing a correct javax.xml.validation.SchemaFactory Service Provider file.

这篇关于如何使用RELAX NG模式和JAXP验证XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆