简体   繁体   English

在jar文件中引用XSD架构

[英]Referencing a XSD schema within jar file

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 在Eclipse模式中执行代码时,但是找不到jar模式文件中的代码

here is the code 这是代码

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
        }

and here is the error I get 这是我得到的错误

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

Thanks 谢谢

If Liso.xsd is importing LisoXml.xsd , then it is just sufficient to define Liso.xsd to the schema factory as shown below. 如果Liso.xsd是进口LisoXml.xsd ,那么它仅仅是足以定义Liso.xsd ,如下所示的模式工厂。 The api is smart enough the load the imported/included schema(s). api足够聪明地加载导入/包含的模式。

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

I verified this worked on both 1.5 and 1.6. 我证实这适用于1.5和1.6。 On 1.6, you might hit in to this issue as well if using DOM. 在1.6上,如果使用DOM,您可能会遇到此问题

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

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