在jar文件中引用XSD架构 [英] Referencing a XSD schema within jar file

查看:404
本文介绍了在jar文件中引用XSD架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模式文件,一个从另一个导入。当在Eclipse模式中执行代码时,但是找不到jar模式文件中的代码时

I have two schema files one is imported from the other. When executing the code in Eclipse schemas are found but when executing the code from jar schema files are not found

这里是代码

SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);

        SchemaFactory schemaFactory = SchemaFactory
                .newInstance("http://www.w3.org/2001/XMLSchema");
        try {
            factory.setSchema(schemaFactory.newSchema(new Source[] {
                    new StreamSource(getClass().getResource("Liso.xsd")
                            .getFile()),
                    new StreamSource(getClass().getResource("LisoXml.xsd")
                            .getFile()) }));
                this.saxParser = factory.newSAXParser();
        } catch (SAXException se) {
            System.out.println("SCHEMA : " + se.getMessage()); // problem in the XSD itself
        }

这是我得到的错误

SCHEMA : schema_reference.4: Failed to read schema document 'file:/C:/Tools/lib/LisoTools.jar!/com/xerox/liso/xml/Liso.xsd', because 1) could not find the do
cument; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

谢谢

推荐答案

如果 Liso.xsd 正在导入 LisoXml.xsd ,那么定义<$就足够了c $ c> Liso.xsd 到架构工厂,如下所示。 api足够聪明地加载导入/包含的模式。

If Liso.xsd is importing LisoXml.xsd, then it is just sufficient to define Liso.xsd to the schema factory as shown below. The api is smart enough the load the imported/included schema(s).

SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema")
Schema compiledSchema = schemaFactory.newSchema(getClass().getClassLoader().getResource("Liso.xsd"))

我证实这适用于1.5和1.6。在1.6上,您可能会遇到此问题如果使用DOM。

I verified this worked on both 1.5 and 1.6. On 1.6, you might hit in to this issue as well if using DOM.

这篇关于在jar文件中引用XSD架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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