问题 在jar文件中引用XSD架构


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

这是代码

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>.

谢谢


9400
2018-02-14 16:11


起源

我记得两年前我有一个类似的问题: stackoverflow.com/questions/2065868/...  - getRessource() 使用java 1.4.2但不适用于java 1.6 ... - Andreas_D
验证罐子有没有 /com/xerox/liso/xml/Liso.xsd 包括,它看起来像一个xsd,以及:) - rogerdpack


答案:


如果 Liso.xsd 进口 LisoXml.xsd,那就足以定义了 Liso.xsd 到架构工厂,如下所示。 api足够智能地加载导入/包含的模式。

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也是如此。


11
2018-02-14 16:36



以上两个链接都已死亡。 :-( - Bowi
您好Aravind,我遇到了同样的问题。 p - bluelurker